Menu

[r2126]: / trunk / Src / FirstRun.UUpdateIni.pas  Maximize  Restore  History

Download this file

448 lines (414 with data), 14.2 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
{
* FirstRun.UUpdateIni.pas
*
* Pascal script for use in [Code] Section of CodeSnip's Install.iss.
*
* Creates per-user and application-wide ini files in correct data folders. Can
* also create the ini files from the information stored in the single ini file
* used in versions of CodeSnip prior to version 1.9.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FirstRun.UUpdateIni.pas, formerly UpdateIni.ps.
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2007-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FirstRun.UUpdateIni;
interface
// Creates new style per user and common config files from content of single,
// old style config file used before CodeSnip v1.9.
procedure CreateIniFilesFromOldStyle;
// Copies config files of a previous installation to new installation, updating
// to Unicode format if necessary.
procedure CopyConfigFiles(InstallID: Integer);
// Deletes any highlighter preferences from new installation's user config file.
procedure DeleteHighligherPrefs;
// Gets version number of new installation's user config file.
function UserConfigFileVer: Integer;
// Adds Prefs:CodeGen section along with default data to new installation's user
// config file.
procedure CreateDefaultCodeGenEntries;
// Deletes proxt password entry from new installation's user config file.
procedure DeleteProxyPassword;
// Updates both common and user config files with correct version information.
// This records both config file versions and (common config file only) version
// number of program being installed.
procedure StampConfigFiles;
implementation
uses
SysUtils, Types, Classes, Windows,
FirstRun.UDataLocations, FirstRun.UUnicode, UAppInfo, UIOUtils;
// TODO: NOTE inserted from snippets database (duplicates FirstRun.UUpdateDBase)
procedure CopyFile(const Source, Dest: string);
var
SourceStream, DestStream: Classes.TFileStream; // source and dest file streams
begin
DestStream := nil;
// Open source and dest file streams
SourceStream := Classes.TFileStream.Create(
Source, SysUtils.fmOpenRead or SysUtils.fmShareDenyWrite
);
try
DestStream := Classes.TFileStream.Create(
Dest, Classes.fmCreate or SysUtils.fmShareExclusive
);
// Copy file from source to dest
DestStream.CopyFrom(SourceStream, SourceStream.Size);
// Set dest file's modification date to same as source file
SysUtils.FileSetDate(
DestStream.Handle, SysUtils.FileGetDate(SourceStream.Handle)
);
finally
// Close files
DestStream.Free;
SourceStream.Free;
end;
end;
// #################### FROM INNO SETUP SOURCE
{ TODO: If any of the Inno setup source is used then move it into it own unit
that meets with Inno setup license requirements }
function GetIniString(const Section, Key: String; Default: String;
const Filename: String): String;
var
BufSize, Len: Integer;
begin
{ On Windows 9x, Get*ProfileString can modify the lpDefault parameter, so
make sure it's unique and not read-only }
UniqueString(Default);
BufSize := 256;
while True do begin
SetString(Result, nil, BufSize);
if Filename <> '' then
Len := GetPrivateProfileString(PChar(Section), PChar(Key), PChar(Default),
@Result[1], BufSize, PChar(Filename))
else
Len := GetProfileString(PChar(Section), PChar(Key), PChar(Default),
@Result[1], BufSize);
{ Work around bug present on Windows NT/2000 (not 95): When lpDefault is
too long to fit in the buffer, nSize is returned (null terminator counted)
instead of nSize-1 (what it's supposed to return). So don't trust the
returned length; calculate it ourself.
Note: This also ensures the string can never include embedded nulls. }
if Len <> 0 then
Len := StrLen(PChar(Result));
{ Break if the string fits, or if it's apparently 64 KB or longer.
No point in increasing buffer size past 64 KB because the length returned by
Windows 2000 seems to be mod 65536. And Windows 95 returns 0 on values
longer than ~32 KB. Note: The docs say the function returns "nSize minus
one" if the buffer is too small, but I'm willing to bet it can be "minus
two" if the last character is double-byte. Let's just be extremely paranoid
and check for BufSize-8. }
if (Len < BufSize-8) or (BufSize >= 65536) then begin
SetLength(Result, Len);
Break;
end;
{ Otherwise double the buffer size and try again }
BufSize := BufSize * 2;
end;
end;
function GetIniInt(const Section, Key: String;
const Default, Min, Max: Longint; const Filename: String): Longint;
{ Reads a Longint from an INI file. If the Longint read is not between Min/Max
then it returns Default. If Min=Max then Min/Max are ignored }
var
S: String;
E: Integer;
begin
S := GetIniString(Section, Key, '', Filename);
if S = '' then
Result := Default
else begin
Val(S, Result, E);
if (E <> 0) or ((Min <> Max) and ((Result < Min) or (Result > Max))) then
Result := Default;
end;
end;
function SetIniString(const Section, Key, Value, Filename: String): Boolean;
begin
if Filename <> '' then
Result := WritePrivateProfileString(PChar(Section), PChar(Key),
PChar(Value), PChar(Filename))
else
Result := WriteProfileString(PChar(Section), PChar(Key),
PChar(Value));
end;
function SetIniInt(const Section, Key: String; const Value: Longint;
const Filename: String): Boolean;
begin
Result := SetIniString(Section, Key, IntToStr(Value), Filename);
end;
procedure DeleteIniSection(const Section, Filename: String);
begin
if Filename <> '' then
WritePrivateProfileString(PChar(Section), nil, nil,
PChar(Filename))
else
WriteProfileString(PChar(Section), nil, nil);
end;
// ################### END
// Reads an ANSI config file, converts content to Unicode and writes that to a
// UTF-16LE encoded Unicode config file with BOM. Does nothing if old and new
// file names are the same.
// *** Requires Unicode Inno Setup. ***
procedure CopyAnsiToUnicodeConfigFile(OldFileName, NewFileName: string);
var
Lines: TStringDynArray; // lines of text read from ANSI .ini file
begin
if CompareText(OldFileName, NewFileName) = 0 then
Exit;
// reads an ANSI file and converts contents to Unicode, using system default
// encoding
Lines := TFileIO.ReadAllLines(OldFileName, TEncoding.Default);
// LoadStringsFromFile(OldFileName, Lines);
// writes string array to UTF-16LE file
ForceDirectories(ExtractFileDir(NewFileName));
WriteStringsToUnicodeFile(NewFileName, Lines);
end;
// Makes a copy of a Unicode config file with a different name. Does nothing if
// old and new file names are the same.
procedure CopyUnicodeConfigFiles(OldFileName, NewFileName: string);
begin
if CompareText(OldFileName, NewFileName) = 0 then
Exit;
ForceDirectories(ExtractFileDir(NewFileName));
CopyFile(OldFileName, NewFileName);
end;
// Checks if a config file is ANSI. If False is returned the file is assumed to
// be Unicode.
function IsAnsiConfigFile(InstallID: Integer): Boolean;
begin
Result := InstallID <= piV3;
end;
// Gets version number of new installation's user config file.
function UserConfigFileVer: Integer;
begin
Result := GetIniInt('IniFile', 'Version', 1, 0, 0, gCurrentUserConfigFile);
end;
// Create a new empty UTF-16LE encoding config file. Used to ensure the config
// file writing routines write in Unicode. This works because built in Inno
// Setup ini functions call Windows API WritePrivateProfileString which only
// writes Unicode if the file it is writing to is detected as Unicode. If not it
// writes ANSI text.
procedure CreateUnicodeConfigFile(FileName: string);
var
NulLines: TStringDynArray; // dummy empty string array
begin
ForceDirectories(ExtractFileDir(FileName));
SetLength(NulLines, 0);
WriteStringsToUnicodeFile(FileName, NulLines);
end;
// Copies config files of a previous installation to new installation, updating
// to Unicode format if necessary.
procedure CopyConfigFiles(InstallID: Integer);
var
OldUserConfigFile: string;
begin
OldUserConfigFile := gUserConfigFiles[InstallID];
if OldUserConfigFile <> '' then
begin
if IsAnsiConfigFile(InstallID) then
CopyAnsiToUnicodeConfigFile(OldUserConfigFile, gCurrentUserConfigFile)
else
CopyUnicodeConfigFiles(OldUserConfigFile, gCurrentUserConfigFile);
end;
end;
// Updates both common and user config files with correct version information.
// This records both config file versions and (common config file only) version
// number of program being installed.
procedure StampConfigFiles;
begin
// Config file Meta data: config file version & program version
if not FileExists(gCurrentUserConfigFile) then
CreateUnicodeConfigFile(gCurrentUserConfigFile);
SetIniInt('IniFile', 'Version', 8, gCurrentUserConfigFile);
SetIniString(
'IniFile',
'ProgramVersion',
TAppInfo.ProgramReleaseVersion,
gCurrentUserConfigFile
);
end;
// Deletes any highlighter preferences from new installation's user config file.
procedure DeleteHighligherPrefs;
begin
DeleteIniSection('Prefs:Hiliter', gCurrentUserConfigFile);
end;
// Creates new style per user and common config files from content of single,
// old style config file used before CodeSnip v1.9.
procedure CreateIniFilesFromOldStyle;
var
I: Integer; // loops thru all highlight elements
begin
// Create per-user settings file:
// Copy file
CopyAnsiToUnicodeConfigFile(
gUserConfigFiles[piOriginal], gCurrentUserConfigFile
);
// Delete unwanted sections:
// - Application section: now in common config file
// - Source code output format: format lost when updating from CodeSnip pre
// 1.7 since section was SourceOutput, but format preserved from v1.7 since
// current Prefs:SourceCode section used
// - Highlighting style is deliberately lost since CodeSnip v3 & v4 have
// different default style and main display uses that style, therefore
// section's HiliteOutput (pre v1.7.5) and Prefs:Hiliter (v1.7 and later)
// deleted.
DeleteIniSection('Application', gCurrentUserConfigFile);
DeleteIniSection('SourceOutput', gCurrentUserConfigFile);
DeleteIniSection('HiliteOutput', gCurrentUserConfigFile);
for I := 0 to 11 do
DeleteIniSection('HiliteOutput:Elem' + IntToStr(I), gCurrentUserConfigFile);
DeleteHighligherPrefs;
// Main window's overview tabs changed at v3: so we reset to 0 (default)
SetIniInt('MainWindow', 'OverviewTab', 0, gCurrentUserConfigFile);
end;
// Adds Prefs:CodeGen section along with default data to new installation's user
// config file.
procedure CreateDefaultCodeGenEntries;
begin
if not FileExists(gCurrentUserConfigFile) then
CreateUnicodeConfigFile(gCurrentUserConfigFile);
SetIniInt(
'Prefs:CodeGen',
'SwitchOffWarnings',
0,
gCurrentUserConfigFile
);
SetIniInt(
'Prefs:CodeGen',
'WarningCount',
8,
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning0.Symbol',
'UNSAFE_TYPE',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning0.MinCompiler',
'15.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning1.Symbol',
'UNSAFE_CAST',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning1.MinCompiler',
'15.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning2.Symbol',
'UNSAFE_CODE',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning2.MinCompiler',
'15.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning3.Symbol',
'SYMBOL_PLATFORM',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning3.MinCompiler',
'14.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning4.Symbol',
'SYMBOL_DEPRECATED',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning4.MinCompiler',
'14.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning5.Symbol',
'SYMBOL_LIBRARY',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning5.MinCompiler',
'14.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning6.Symbol',
'IMPLICIT_STRING_CAST',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning6.MinCompiler',
'20.00',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning7.Symbol',
'EXPLICIT_STRING_CAST',
gCurrentUserConfigFile
);
SetIniString(
'Prefs:CodeGen',
'Warning7.MinCompiler',
'20.00',
gCurrentUserConfigFile
);
end;
// Deletes proxt password entry from new installation's user config file.
procedure DeleteProxyPassword;
begin
if not FileExists(gCurrentUserConfigFile) then
CreateUnicodeConfigFile(gCurrentUserConfigFile);
SetIniString(
'ProxyServer',
'Password',
'',
gCurrentUserConfigFile
);
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.