source: trunk/backup.lpr

Last change on this file was 2, checked in by george, 14 years ago
  • Přidáno: Soubory programu.
File size: 5.3 KB
Line 
1program backup;
2
3{$mode objfpc}{$H+}
4
5uses
6 {$IFDEF UNIX}{$IFDEF UseCThreads}
7 cthreads,
8 {$ENDIF}{$ENDIF}
9 Classes, SysUtils, CustApp
10 { you can add units after this }, UMemoryStreamEx, math;
11
12type
13
14 { TBackup }
15
16 TBackup = class(TCustomApplication)
17 protected
18 procedure DoRun; override;
19 public
20 Counter: Integer;
21 FileCount: Integer;
22 BackupFileExtension: string;
23 BackupFileName: string;
24 ExponentBase: Double;
25 Verbose: Boolean;
26 constructor Create(TheOwner: TComponent); override;
27 destructor Destroy; override;
28 procedure WriteHelp; virtual;
29 procedure MoveFiles;
30 procedure CheckParameters;
31 procedure PrintStatTable;
32 end;
33
34{ TBackup }
35
36procedure TBackup.DoRun;
37var
38 CounterFile: TFileStream;
39 CounterFileName: string;
40begin
41 CheckParameters;
42 CounterFileName := BackupFileName + '.counter';
43 if FileExists(CounterFileName) then begin
44 CounterFile := TFileStream.Create(CounterFileName, fmOpenReadWrite);
45 Counter := StrToInt(CounterFile.ReadAnsiString);
46 CounterFile.Free;
47 end else begin
48 Counter := 0;
49 end;
50 if not FileExists(BackupFileName) then begin
51 WriteLn('ZdrojovÜ soubor "' + BackupFileName + '" nenalezen.');
52 Halt;
53 end;
54 if Verbose then WriteLn('Hodnota čítače kroků: ' + IntToStr(Counter));
55 MoveFiles;
56 CounterFile := TFileStream.Create(CounterFileName, fmCreate);
57 CounterFile.WriteAnsiString(IntToStr(Counter));
58 CounterFile.Free;
59 Terminate;
60end;
61
62constructor TBackup.Create(TheOwner: TComponent);
63begin
64 inherited Create(TheOwner);
65 StopOnException := True;
66 BackupFileExtension := '';
67 BackupFileName := '';
68 FileCount := 10;
69 ExponentBase := 1;
70 Verbose := False;
71end;
72
73destructor TBackup.Destroy;
74begin
75 inherited Destroy;
76end;
77
78procedure TBackup.WriteHelp;
79begin
80 WriteLn('PouÅŸití: backup [přepínače]');
81 WriteLn('Vytváří kopie záloÅŸního souboru s exponenciálně rostoucím stářím.');
82 WriteLn(' -f --file=SOUBOR zálohovanÜ soubor');
83 WriteLn(' -b --base=ČÍSLO základ mocniny');
84 WriteLn(' -c --count=ČÍSLO max. počet zaloÅŸních souborů');
85 WriteLn(' -e --file-extension=TEXT rozšíření názvu vytvářenÃœch souborů');
86 WriteLn(' -t --table zobrazení tabulky stáří záloh');
87 WriteLn(' -v --verbose upovídanÜ reşim');
88 WriteLn(' --version zobrazení verze');
89 WriteLn(' -h --help zobrazení nápovědy');
90end;
91
92procedure TBackup.MoveFiles;
93var
94 I: Integer;
95 NewFileName: string;
96 OldFileName: string;
97 ReducedBackupFileName: string;
98begin
99 if Copy(BackupFileName, 1 + Length(BackupFileName) - Length(BackupFileExtension), 255) = BackupFileExtension then
100 ReducedBackupFileName := Copy(BackupFileName, 1, Length(BackupFileName) - Length(BackupFileExtension))
101 else ReducedBackupFileName := BackupFileName;
102 for I := FileCount downto 0 do begin
103 if (Counter mod Trunc(Power(ExponentBase, I))) = 0 then begin
104 if I = 0 then OldFileName := BackupFileName
105 else OldFileName := ReducedBackupFileName + '-' + IntToStr(I) + BackupFileExtension;
106 NewFileName := ReducedBackupFileName + '-' + IntToStr(I + 1) + BackupFileExtension;
107 if FileExists(OldFileName) then begin
108 if Verbose then
109 WriteLn('Přesouvám "' + OldFileName + '" na "' + NewFileName + '". Perioda ' +
110 IntToStr(Trunc(Power(ExponentBase, I))) + ' kroků.');
111 if FileExists(NewFileName) then DeleteFile(PChar(NewFileName));
112 RenameFile(OldFileName, NewFileName);
113 end;
114 end;
115 end;
116
117 Inc(Counter);
118end;
119
120procedure TBackup.CheckParameters;
121//var
122// ErrorMsg: string;
123begin
124 // quick check parameters
125 (*
126 ErrorMsg := CheckOptions('h', 'help');
127 WriteLn(ErrorMsg);
128 if ErrorMsg <> '' then begin
129 ShowException(Exception.Create(ErrorMsg));
130 Halt;
131 end;
132 *)
133
134 // parse parameters
135 if HasOption('h', 'help') then begin
136 WriteHelp;
137 Halt;
138 end;
139
140 if HasOption('version') then begin
141 WriteLn('backup version 1.0');
142 Halt;
143 end;
144
145 if HasOption('c', 'count') then begin
146 FileCount := StrToInt(GetOptionValue('c', 'count'));
147 end;
148
149 if HasOption('b', 'base') then begin
150 ExponentBase := StrToFloat(GetOptionValue('b', 'base'));
151 if ExponentBase < 1 then begin
152 ShowException(Exception.Create('Základ mocniny musí bÃœt větší neÅŸ 1.'));
153 Halt;
154 end;
155 end;
156
157 if HasOption('f', 'file') then begin
158 BackupFileName := GetOptionValue('f', 'file');
159 end;
160
161 if HasOption('e', 'file-extension') then begin
162 BackupFileExtension := GetOptionValue('e', 'file-extension');
163 end;
164
165 if HasOption('t', 'table') then begin
166 PrintStatTable;
167 Halt;
168 end;
169
170 if HasOption('v', 'verbose') then begin
171 Verbose := True;
172 end;
173end;
174
175procedure TBackup.PrintStatTable;
176var
177 I: Integer;
178 Period: Int64;
179 PeriodSum: Int64;
180begin
181 PeriodSum := 0;
182 WriteLn('Pořadové číslo Perioda Interval staří');
183 for I := 0 to FileCount - 1 do begin
184 Period := Trunc(Power(ExponentBase, I));
185 PeriodSum := PeriodSum + Period;
186 WriteLn(IntToStr(I + 1) + ' ' + IntToStr(Period) +
187 ' ' + IntToStr(PeriodSum - Period + 1) + '-' + IntToStr(PeriodSum));
188 end;
189end;
190
191var
192 Application: TBackup;
193
194{$IFDEF WINDOWS}{$R backup.rc}{$ENDIF}
195
196{$R *.res}
197
198begin
199 Application:=TBackup.Create(nil);
200 Application.Run;
201 Application.Free;
202end.
203
Note: See TracBrowser for help on using the repository browser.