Statistiques
| Révision:

root / UUtil.pas @ 3

Historique | Voir | Annoter | Télécharger (8,454 ko)

1 3 avalancogn
unit UUtil;
2
3
{$WARN SYMBOL_PLATFORM OFF}
4
5
interface
6
7
uses
8
  Windows, Forms, SysUtils, Messages, UVariables, lbCipher;
9
10
function BdRReadString(RegKey, RegValue, Default: String): String;
11
function BdRReadInteger(RegKey, RegValue: String; Default: Integer): Integer;
12
function BdRReadFloat(RegKey, RegValue: String; Default: Double): Double;
13
function BdRReadBoolean(RegKey, RegValue: String; Default: Boolean): Boolean;
14
procedure BdRWriteString(RegKey, RegValue, Value: String);
15
procedure BdRWriteInteger(RegKey, RegValue: String; Value: Integer);
16
procedure BdRWriteFloat(RegKey, RegValue: String; Value: Double);
17
procedure BdRWriteBoolean(RegKey, RegValue: String; Value: Boolean);
18
function WideToBool(Value: String): Boolean;
19
function BoolToWide(Value: Boolean): String;
20
function DblToStr(Value: Double; Digits: Integer): String;
21
function ExtToLCID(Ext: String): Cardinal;
22
function ExtToName(Ext: String): String;
23
function DefaultCurrency: String;
24
function DecimalSeparator: String;
25
function ThousandSeparator: String;
26
function ListSeparator: String;
27
function DriveType(Drive: String): UINT;
28
function MD5Str(Digest: TMD5Digest): String;
29
function CompleteKey: String;
30
function EducationKey: String;
31
function TooMuchRecords: Boolean;
32
function IsComplete: Boolean;
33
function IsEducation: Boolean;
34
function IsEvaluation: Boolean;
35
36
implementation
37
38
type
39
  WindowsString = type AnsiString(1252);
40
41
function BdRReadString(RegKey, RegValue, Default: String): String;
42
begin
43
  if Reg.OpenKeyReadOnly(RegKey)
44
  and Reg.ValueExists(RegValue)
45
  then
46
    Result := Reg.ReadString(RegValue)
47
  else
48
    Result := Default;
49
  Reg.CloseKey;
50
end;
51
52
function BdRReadInteger(RegKey, RegValue: String; Default: Integer): Integer;
53
begin
54
  if Reg.OpenKeyReadOnly(RegKey)
55
  and Reg.ValueExists(RegValue)
56
  then
57
    Result := Reg.ReadInteger(RegValue)
58
  else
59
    Result := Default;
60
  Reg.CloseKey;
61
end;
62
63
function BdRReadFloat(RegKey, RegValue: String; Default: Double): Double;
64
begin
65
  if Reg.OpenKeyReadOnly(RegKey)
66
  and Reg.ValueExists(RegValue)
67
  then
68
    Result := Reg.ReadFloat(RegValue)
69
  else
70
    Result := Default;
71
  Reg.CloseKey;
72
end;
73
74
function BdRReadBoolean(RegKey, RegValue: String; Default: Boolean): Boolean;
75
begin
76
  if Reg.OpenKeyReadOnly(RegKey)
77
  and Reg.ValueExists(RegValue)
78
  then
79
//    Result := Reg.ReadBool(RegValue);
80
    // TRegistry stocke les valeurs Boolean en tant que Integer (REG_DWORD)
81
    // TRegistryIniFile utilisait le type String (REG_SZ)
82
    // pour garder la compatibilit? avec l'ancienne version,
83
    // je lis une valeur String que je convertis en Boolean
84
    Result := WideToBool(Reg.ReadString(RegValue))
85
  else
86
    Result := Default;
87
  Reg.CloseKey;
88
end;
89
90
procedure BdRWriteString(RegKey, RegValue, Value: String);
91
begin
92
  Reg.Access := KEY_WRITE;
93
  if Reg.OpenKey(RegKey, True)
94
  then
95
    Reg.WriteString(RegValue, Value);
96
  Reg.CloseKey;
97
end;
98
99
procedure BdRWriteInteger(RegKey, RegValue: String; Value: Integer);
100
begin
101
  Reg.Access := KEY_WRITE;
102
  if Reg.OpenKey(RegKey, True)
103
  then
104
    Reg.WriteInteger(RegValue, Value);
105
  Reg.CloseKey;
106
end;
107
108
procedure BdRWriteFloat(RegKey, RegValue: String; Value: Double);
109
begin
110
  Reg.Access := KEY_WRITE;
111
  if Reg.OpenKey(RegKey, True)
112
  then
113
    Reg.WriteFloat(RegValue, Value);
114
  Reg.CloseKey;
115
end;
116
117
procedure BdRWriteBoolean(RegKey, RegValue: String; Value: Boolean);
118
begin
119
  Reg.Access := KEY_WRITE;
120
  if Reg.OpenKey(RegKey, True)
121
  then
122
//    Reg.WriteBool(RegValue, Value);
123
    // TRegistry stocke les valeurs Boolean en tant que Integer (REG_DWORD)
124
    // TRegistryIniFile utilisait le type String (REG_SZ)
125
    // pour garder la compatibilit? avec l'ancienne version,
126
    // je convertis les valeurs Boolean en String avant de l'enregistrer
127
    Reg.WriteString(RegValue, BoolToWide(Value));
128
  Reg.CloseKey;
129
end;
130
131
function WideToBool(Value: String): Boolean;
132
begin
133
  Result := Value <> '0';
134
end;
135
136
function BoolToWide(Value: Boolean): String;
137
const
138
  Values: array[Boolean] of string = ('0', '1');
139
begin
140
  Result := Values[Value];
141
end;
142
143
function DblToStr(Value: Double; Digits: Integer): String;
144
var
145
  s: String;
146
  l: Integer;
147
begin
148
  s := FloatToStrF(Value, ffFixed, 15, Digits); // Conversion en cha?ne
149
  s := Trim(s); // Suppression des espaces
150
  l := Length(s);
151
  if Pos('.', s) > 0
152
  then
153
    while (l > 0) and (s[l] = '0') do Dec(l); // Suppression des z?ros
154
  if (l > 0) and (s[l] = '.') then Dec(l); // Suppression du point
155
  Result := Copy(s, 1, l);
156
end;
157
158
function ExtToLCID(Ext: String): Cardinal;
159
var
160
  i: Integer;
161
begin
162
  // Recherche du LCID correspondant ? l'extension
163
  i := 0;
164
  while (i < Languages.Count) and (UpperCase(Languages.Ext[i]) <> Ext) do
165
    Inc(i);
166
  if i = Languages.Count
167
  then // Introuvable
168
    Result := 0
169
  else
170
    Result := Languages.LocaleID[i];
171
end;
172
173
function ExtToName(Ext: String): String;
174
var
175
  i: Integer;
176
begin
177
  i := 0;
178
  while (i < Languages.Count) and (Languages.Ext[i] <> Ext) do
179
    Inc(i);
180
  if i = Languages.Count
181
  then // Introuvable
182
    Result := ''
183
  else
184
    Result := Languages.Name[i];
185
end;
186
187
function DefaultCurrency: String;
188
begin
189
  Result := GetLocaleStr(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, '$');
190
end;
191
192
function DecimalSeparator: String;
193
begin
194
  Result := GetLocaleStr(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, '.');
195
end;
196
197
function ThousandSeparator: String;
198
begin
199
  Result := GetLocaleStr(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, '');
200
end;
201
202
function ListSeparator: String;
203
begin
204
  Result := GetLocaleStr(LOCALE_USER_DEFAULT, LOCALE_SLIST, ';');
205
end;
206
207
function DriveType(Drive: String): UINT;
208
begin
209
  Result := GetDriveType(PChar(Drive));
210
end;
211
212
function MD5Str(Digest: TMD5Digest): String;
213
var
214
        i: Byte;
215
  sb: TStringBuilder;
216
const
217
        Digits: array[0..15] of Char =
218
                ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
219
begin
220
  sb := TStringBuilder.Create;
221
  for i := 0 to 15 do
222
    sb.Append(Digits[(Digest[I] shr 4) and $0f]).Append(Digits[Digest[I] and $0f]);
223
  sb.Insert(24, '-').Insert(16, '-').Insert(8, '-');
224
  Result := sb.ToString;
225
  FreeAndNil(sb);
226
end;
227
228
function CompleteKey: String;
229
var
230
  sb: TStringBuilder;
231
  Digest: TMD5Digest;
232
begin
233
  sb := TStringBuilder.Create;
234
  sb.Append(Format('InraPorc version %d : ', [MajorVersion]));
235
  sb.Append(Format('licence num?ro %d ', [LicenseNumber]));
236
  sb.Append(Format('sur le volume %s ', [Volume]));
237
  sb.Append(Format('accord?e ? %s %s (%s) ', [FirstName, LastName, Company]));
238
  sb.Append(Format('valable jusqu''au %s', [FinalDate]));
239
  StringHashMD5(Digest, WindowsString(sb.ToString));
240
  FreeAndNil(sb);
241
  Result := MD5Str(Digest);
242
end;
243
244
function EducationKey: String;
245
var
246
  sb: TStringBuilder;
247
  Digest: TMD5Digest;
248
begin
249
  sb := TStringBuilder.Create;
250
  sb.Append(Format('InraPorc version %d : ', [MajorVersion]));
251
  sb.Append('licence limit?e ? l''?ducation ');
252
  sb.Append(Format('sur le volume %s ', [Volume]));
253
  sb.Append(Format('accord?e ? %s %s (%s) ', [FirstName, LastName, Company]));
254
  sb.Append(Format('valable jusqu''au %s', [FinalDate]));
255
  StringHashMD5(Digest, WindowsString(sb.ToString));
256
  FreeAndNil(sb);
257
  Result := MD5Str(Digest);
258
end;
259
260
function TooMuchRecords: Boolean;
261
begin
262
  Result := (ListMatiere.Count - NumMatInraAfz > 10)
263
    or (ListAliment.Count > 10)
264
    or (ListSeqAliT.Count > 5)
265
    or (ListRationT.Count > 5)
266
    or (ListLogeT.Count > 5)
267
    or (ListProfilT.Count > 5)
268
    or (ListSimulT.Count > 5)
269
    or (ListSeqAliP.Count > 5)
270
    or (ListRationP.Count > 5)
271
    or (ListProfilP.Count > 5)
272
    or (ListSimulP.Count > 5);
273
end;
274
275
function IsComplete: Boolean;
276
begin
277
  Result := (LicenseType = 2)
278
    and (LicenseNumber > 0)
279
    and (Length(FirstName) > 0)
280
    and (Length(LastName) > 0)
281
    and (StrToInt(FormatDateTime('yyyymmdd', Date)) <= StrToInt(Copy(FinalDate, 7, 4) + Copy(FinalDate, 4, 2) + Copy(FinalDate, 1, 2)))
282
    and (DriveType(Drive) = DRIVE_FIXED)
283
    and AnsiSameText(SoftwareEnableKey, CompleteKey);
284
end;
285
286
function IsEducation: Boolean;
287
begin
288
  Result := (LicenseType = 1)
289
    and (Length(FirstName) > 0)
290
    and (Length(LastName) > 0)
291
    and (Length(Company) > 0)
292
    and (StrToInt(FormatDateTime('yyyymmdd', Date)) <= StrToInt(Copy(FinalDate, 7, 4) + Copy(FinalDate, 4, 2) + Copy(FinalDate, 1, 2)))
293
    and AnsiSameText(SoftwareEnableKey, EducationKey)
294
    and not TooMuchRecords;
295
end;
296
297
function IsEvaluation: Boolean;
298
begin
299
  Result := (LicenseType = 0)
300
    and not TooMuchRecords;
301
end;
302
303
end.