Menu

[r2742]: / trunk / Src / Favourites.UPersist.pas  Maximize  Restore  History

Download this file

133 lines (119 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Defines a class that can persist a list of favourites to and from a file on
* a per user basis.
}
unit Favourites.UPersist;
interface
uses
Favourites.UFavourites, UExceptions;
type
EFavouritesPersist = class(ECodeSnip);
type
TFavouritesPersist = record
strict private
const
Watermark = #$25BA + ' CodeSnip Favourites v1 ' + #$25C4;
class function FavouritesFileName: string; static;
public
class procedure Save(Favourites: TFavourites); static;
class procedure Load(Favourites: TFavourites); static;
end;
implementation
uses
SysUtils, IOUtils, Classes,
DB.UMain, UAppInfo, UConsts, UIOUtils, UIStringList, USnippetIDs, UStrUtils;
{ TFavouritesPersist }
class function TFavouritesPersist.FavouritesFileName: string;
begin
Result := IncludeTrailingPathDelimiter(TAppInfo.UserAppDir)
+ 'Favourites';
end;
class procedure TFavouritesPersist.Load(Favourites: TFavourites);
var
Lines: IStringList;
Line: string;
Fields: IStringList;
SnippetName: string;
UserDef: Boolean;
LastAccess: TDateTime;
resourcestring
sBadFormat = 'Invalid favourites file format';
begin
if not TFile.Exists(FavouritesFileName) then
Exit;
try
Lines := TIStringList.Create(
TFileIO.ReadAllLines(FavouritesFileName, TEncoding.UTF8, True)
);
except
on E: EStreamError do
raise EFavouritesPersist.Create(E);
on E: EIOUtils do
raise EFavouritesPersist.Create(E);
else
raise;
end;
Line := Lines[0];
if Line <> Watermark then
raise EFavouritesPersist.Create(sBadFormat);
Lines.Delete(0);
for Line in Lines do
begin
if StrTrim(Line) = '' then
Continue;
Fields := TIStringList.Create(Line, TAB, False, True);
if Fields.Count <> 3 then
raise EFavouritesPersist.Create(sBadFormat);
SnippetName := Fields[0];
UserDef := True; // accept any text as true excpet "false"
if StrSameText(Fields[1], 'false') then
UserDef := False;
if not TryStrToDateTime(Fields[2], LastAccess) then
raise EFavouritesPersist.Create(sBadFormat);
// only add to favourites if snippet in database
if Database.Snippets.Find(SnippetName, UserDef) <> nil then
Favourites.Add(TSnippetID.Create(SnippetName, UserDef), LastAccess);
end;
end;
class procedure TFavouritesPersist.Save(Favourites: TFavourites);
var
SB: TStringBuilder;
Fav: TFavourite;
begin
SB := TStringBuilder.Create;
try
SB.AppendLine(Watermark);
for Fav in Favourites do
begin
SB.Append(Fav.SnippetID.Name);
SB.Append(TAB);
SB.Append(BoolToStr(Fav.SnippetID.UserDefined, True));
SB.Append(TAB);
SB.Append(DateTimeToStr(Fav.LastAccessed));
SB.AppendLine;
end;
TDirectory.CreateDirectory(TPath.GetDirectoryName(FavouritesFileName));
try
TFileIO.WriteAllText(
FavouritesFileName, SB.ToString, TEncoding.UTF8, True
);
except
on E: EStreamError do
raise EFavouritesPersist.Create(E);
else
raise;
end;
finally
SB.Free;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.