Menu

[r4761]: / branches / parsnip / Src / FirstRun.UIniFile.pas  Maximize  Restore  History

Download this file

250 lines (217 with data), 7.7 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
{
* 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) 2012-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Ini file access helper routines for use with first-run startup code.
}
unit FirstRun.UIniFile;
interface
/// <summary>Gets a string value from an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of key whose value is required.</param>
/// <param name="Default">string [in] Default value to be returned if key can't
/// be read.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
/// <returns>string. Required key value or Default if key can't be read.
/// </returns>
function GetIniString(const Section, Key: string; Default: string;
const FileName: String): string;
/// <summary>Gets an integer value from an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of key whose value is required.</param>
/// <param name="Default">LongInt [in] Default value to be returned if key
/// can't be read.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
/// <returns>LongInt. Required key value or Default if key can't be read.
/// </returns>
function GetIniInt(const Section, Key: string; const Default: Longint;
const FileName: string): Longint;
/// <summary>Writes a string value to an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of key that receives value.</param>
/// <param name="Value">string [in] Value to be stored.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
procedure SetIniString(const Section, Key, Value, FileName: string);
/// <summary>Writes an integer value to an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of key that receives value.</param>
/// <param name="Value">LongInt [in] Value to be stored.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
procedure SetIniInt(const Section, Key: string; const Value: Longint;
const FileName: string);
/// <summary>Deletes a section of an ini file.</summary>
/// <param name="Section">string [in] Section to be deleted.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
procedure DeleteIniSection(const Section, FileName: string);
/// <summary>Delete a key / value pair from an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of key to be deleted.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
procedure DeleteIniKey(const Section, Key, FileName: string);
/// <summary>Checks if a section exists in an ini file.</summary>
/// <param name="Section">string [in] Ini file section to be deleted.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
/// <returns>Boolean. True if section exists, False if not.</returns>
function IniSectionExists(const Section, FileName: string): Boolean;
/// <summary>Checks if a key exists in an ini file.</summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="Key">string [in] Name of required key.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
/// <returns>Boolean. True if key exists, False if not.</returns>
function IniKeyExists(const Section, Key, FileName: string): Boolean;
/// <summary>Renames a section of an ini file, preserving all values.</summary>
/// <param name="OldSection">string [in] Name of section to be renamed.</param>
/// <param name="NewSection">string [in] New name of section.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
procedure RenameIniSection(const OldSection, NewSection, FileName: string);
/// <summary>Renames a key in a given section of an ini file if the key exists.
/// </summary>
/// <param name="Section">string [in] Ini file section containing key.</param>
/// <param name="OldKey">string [in] Name of key to be renamed.</param>
/// <param name="NewKey">string [in] New name of key.</param>
/// <param name="FileName">string [in] Name of ini file.</param>
/// <returns>Boolean. True if key exists and was renamed, False if key does not
/// exist and no action was taken.</returns>
function RenameIniKey(const Section, OldKey, NewKey, FileName: string):
Boolean;
implementation
uses
// Delphi
IniFiles, Classes;
function GetIniString(const Section, Key: string; Default: string;
const FileName: String): string;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Result := Ini.ReadString(Section, Key, Default);
finally
Ini.Free;
end;
end;
function GetIniInt(const Section, Key: string; const Default: Longint;
const FileName: string): Longint;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Result := Ini.ReadInteger(Section, Key, Default);
finally
Ini.Free;
end;
end;
procedure SetIniString(const Section, Key, Value, FileName: string);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Ini.WriteString(Section, Key, Value);
finally
Ini.Free;
end;
end;
procedure SetIniInt(const Section, Key: string; const Value: Longint;
const FileName: string);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Ini.WriteInteger(Section, Key, Value);
finally
Ini.Free;
end;
end;
procedure DeleteIniSection(const Section, FileName: string);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Ini.EraseSection(Section);
finally
Ini.Free;
end;
end;
procedure DeleteIniKey(const Section, Key, FileName: string);
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Ini.DeleteKey(Section, Key);
finally
Ini.Free;
end;
end;
function IniKeyExists(const Section, Key, FileName: string): Boolean;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Result := Ini.ValueExists(Section, Key);
finally
Ini.Free;
end;
end;
function IniSectionExists(const Section, FileName: string): Boolean;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Result := Ini.SectionExists(Section);
finally
Ini.Free;
end;
end;
procedure RenameIniSection(const OldSection, NewSection, FileName: string);
var
Ini: TIniFile;
AllValues: TStringList;
I: Integer;
begin
AllValues := nil;
Ini := TIniFile.Create(FileName);
try
if not Ini.SectionExists(OldSection) then
Exit;
AllValues := TStringList.Create;
Ini.ReadSectionValues(OldSection, AllValues);
for I := 0 to Pred(AllValues.Count) do
Ini.WriteString(
NewSection, AllValues.Names[I], AllValues.ValueFromIndex[I]
);
Ini.EraseSection(OldSection);
finally
AllValues.Free;
Ini.Free;
end;
end;
function RenameIniKey(const Section, OldKey, NewKey, FileName: string):
Boolean;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(FileName);
try
Result := Ini.ValueExists(Section, OldKey);
if not Result then
Exit;
Ini.WriteString(Section, NewKey, Ini.ReadString(Section, OldKey, ''));
Ini.DeleteKey(Section, OldKey);
finally
Ini.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.