Subversion Repositories currency_converter

Rev

Rev 13 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
library CurConv;
2
 
3
uses
4
  SysUtils,
5
  Classes,
6
  Windows,
7
  Dialogs,
8
  uLkJSON in '..\RTL\uLkJSON.pas',
9
  VtsCurConv in '..\RTL\VtsCurConv.pas';
10
 
11
{$R *.res}
12
 
13
type
14
  TVtsCurConvFlags = type DWORD;
15
 
16
const
17
  CONVERT_DONT_SHOW_ERRORS:             TVtsCurConvFlags = 1;
18
  CONVERT_FALLBACK_TO_CACHE:            TVtsCurConvFlags = 2;
19
  CONVERT_USE_SSL:                      TVtsCurConvFlags = 4;
20
  CONVERT_CONFIRM_WEB_ACCESS:           TVtsCurConvFlags = 8;
21
  CONVERT_NO_INTERACTIVE_API_KEY_INPUT: TVtsCurConvFlags = 16;
22
 
23
const
24
  S_VTSCONV_OK:                HRESULT = $20000000; // Success, Customer defined, Facility 0, Code 0
25
  S_VTSCONV_NOTHING:           HRESULT = $20000001; // Success, Customer defined, Facility 0, Code 1
26
  E_VTSCONV_GENERIC_FAILURE:   HRESULT = $A0000000; // Failure, Customer defined, Facility 0, Code 0
27
  E_VTSCONV_BAD_ARGS:          HRESULT = $A0000001; // Failure, Customer defined, Facility 0, Code 1
28
  E_VTSCONV_STOREDKEY_INVALID: HRESULT = $A0000002; // Failure, Customer defined, Facility 0, Code 2
29
  E_VTSCONV_NO_STOREDKEY:      HRESULT = $A0000003; // Failure, Customer defined, Facility 0, Code 3
30
 
31
function DeleteAPIKey(UserMode: BOOL; DontShowErrors: BOOL): HRESULT; stdcall;
32
begin
33
  try
34
    if TVtsCurConv.DeleteAPIKey(UserMode) then
35
      result := S_VTSCONV_OK
36
    else
37
      result := S_VTSCONV_NOTHING;
38
  except
39
    on E: Exception do
40
    begin
41
      if DontShowErrors then MessageDlg(e.Message, mtError, [mbOk], 0);
42
      result := E_VTSCONV_GENERIC_FAILURE;
43
    end;
44
  end;
45
end;
46
 
47
function WriteAPIKeyW(key: LPCWSTR; UserMode: BOOL; DontShowErrors: BOOL): HRESULT; stdcall;
48
begin
49
  try
50
    if Length(key) <> 32 then
51
    begin
52
      result := E_VTSCONV_BAD_ARGS;
53
      Exit;
54
    end;
55
    TVtsCurConv.WriteAPIKey(TVtsCurApiKey(key), UserMode);
56
    result := S_VTSCONV_OK;
57
  except
58
    on E: Exception do
59
    begin
60
      if DontShowErrors then MessageDlg(e.Message, mtError, [mbOk], 0);
61
      result := E_VTSCONV_GENERIC_FAILURE;
62
    end;
63
  end;
64
end;
65
 
66
function WriteAPIKeyA(key: LPCSTR; UserMode: BOOL; DontShowErrors: BOOL): HRESULT; stdcall;
67
begin
68
  try
69
    if Length(key) <> 32 then
70
    begin
71
      result := E_VTSCONV_BAD_ARGS;
72
      Exit;
73
    end;
74
    TVtsCurConv.WriteAPIKey(TVtsCurApiKey(key), UserMode);
75
    result := S_VTSCONV_OK;
76
  except
77
    on E: Exception do
78
    begin
79
      if DontShowErrors then MessageDlg(e.Message, mtError, [mbOk], 0);
80
      result := E_VTSCONV_GENERIC_FAILURE;
81
    end;
82
  end;
83
end;
84
 
85
function ReadAPIKeyW(key: LPWSTR; DontShowErrors: BOOL): HRESULT; stdcall;
86
var
87
  s: WideString;
88
begin
89
  try
90
    s := WideString(TVtsCurConv.ReadAPIKey);
91
    if s = '' then
92
    begin
93
      result := E_VTSCONV_NO_STOREDKEY;
94
      Exit;
95
    end;
96
    if Length(s) <> 32 then
97
    begin
98
      result := E_VTSCONV_STOREDKEY_INVALID;
99
      Exit;
100
    end;
101
    ZeroMemory(key, 33*SizeOf(WideChar));
102
    CopyMemory(key, @s[1], 32*SizeOf(WideChar));
103
    Result := S_VTSCONV_OK;
104
  except
105
    on E: Exception do
106
    begin
107
      if DontShowErrors then MessageDlg(e.Message, mtError, [mbOk], 0);
108
      result := E_VTSCONV_GENERIC_FAILURE;
109
    end;
110
  end;
111
end;
112
 
113
function ReadAPIKeyA(key: LPSTR; DontShowErrors: BOOL): HRESULT; stdcall;
114
var
115
  s: AnsiString;
116
begin
117
  try
118
    s := AnsiString(TVtsCurConv.ReadAPIKey);
119
    if s = '' then
120
    begin
121
      result := E_VTSCONV_NO_STOREDKEY;
122
      Exit;
123
    end;
124
    if Length(s) <> 32 then
125
    begin
126
      result := E_VTSCONV_STOREDKEY_INVALID;
127
      Exit;
128
    end;
129
    ZeroMemory(key, 33*SizeOf(AnsiChar));
130
    CopyMemory(key, @s[1], 32*SizeOf(AnsiChar));
131
    result := S_VTSCONV_OK;
132
  except
133
    on E: Exception do
134
    begin
135
      if DontShowErrors then MessageDlg(e.Message, mtError, [mbOk], 0);
136
      result := E_VTSCONV_GENERIC_FAILURE;
137
    end;
138
  end;
139
end;
140
 
141
function ConvertW(Value: Double; CurFrom, CurTo: LPCWSTR; MaxAge: integer;
142
                  Flags: TVtsCurConvFlags; HistoricDate: TDate): Double; stdcall;
143
var
144
  x: TVtsCurConv;
145
begin
146
  try
147
    x := TVtsCurConv.Create;
148
    try
149
      x.Secure                 := Flags and CONVERT_USE_SSL <> 0;
150
      x.MaxAgeSeconds          := MaxAge;
151
      x.ConfirmWebAccess       := Flags and CONVERT_CONFIRM_WEB_ACCESS <> 0;
152
      x.FallBackToCache        := Flags and CONVERT_FALLBACK_TO_CACHE <> 0;
153
      x.InteractiveAPIKeyInput := Flags and CONVERT_NO_INTERACTIVE_API_KEY_INPUT = 0;
154
      result := x.Convert(value, TVtsCur(CurFrom), TVtsCur(CurTo), HistoricDate);
155
    finally
156
      x.Free;
157
    end;
158
  except
159
    on E: Exception do
160
    begin
161
      if Flags and CONVERT_DONT_SHOW_ERRORS = 0 then MessageDlg(e.Message, mtError, [mbOk], 0);
162
      result := -1;
163
    end;
164
  end;
165
end;
166
 
167
function ConvertA(Value: Double; CurFrom, CurTo: LPCSTR; MaxAge: integer;
168
                  Flags: TVtsCurConvFlags; HistoricDate: TDate): Double; stdcall;
169
var
170
  x: TVtsCurConv;
171
begin
172
  try
173
    x := TVtsCurConv.Create;
174
    try
175
      x.Secure                 := Flags and CONVERT_USE_SSL <> 0;
176
      x.MaxAgeSeconds          := MaxAge;
177
      x.ConfirmWebAccess       := Flags and CONVERT_CONFIRM_WEB_ACCESS <> 0;
178
      x.FallBackToCache        := Flags and CONVERT_FALLBACK_TO_CACHE <> 0;
179
      x.InteractiveAPIKeyInput := Flags and CONVERT_NO_INTERACTIVE_API_KEY_INPUT = 0;
180
      result := x.Convert(value, TVtsCur(CurFrom), TVtsCur(CurTo), HistoricDate);
181
    finally
182
      x.Free;
183
    end;
184
  except
185
    on E: Exception do
186
    begin
187
      if Flags and CONVERT_DONT_SHOW_ERRORS = 0 then MessageDlg(e.Message, mtError, [mbOk], 0);
188
      result := -1;
189
    end;
190
  end;
191
end;
192
 
193
function AcceptedCurrenciesW(WriteTo: LPWSTR; MaxAge: integer; Flags: TVtsCurConvFlags;
194
                             HistoricDate: TDate): Integer; stdcall;
195
var
196
  x: TVtsCurConv;
197
  sl: TStringList;
198
  s: WideString;
199
  i: integer;
200
begin
201
  try
202
    x := TVtsCurConv.Create;
203
    if Assigned(WriteTo) then sl := TStringList.Create else sl := nil;
204
    try
205
      x.Secure                 := Flags and CONVERT_USE_SSL <> 0;
206
      x.MaxAgeSeconds          := MaxAge;
207
      x.ConfirmWebAccess       := Flags and CONVERT_CONFIRM_WEB_ACCESS <> 0;
208
      x.FallBackToCache        := Flags and CONVERT_FALLBACK_TO_CACHE <> 0;
209
      x.InteractiveAPIKeyInput := Flags and CONVERT_NO_INTERACTIVE_API_KEY_INPUT = 0;
210
      result := x.GetAcceptedCurrencies(sl, HistoricDate);
211
      if Assigned(WriteTo) then
212
      begin
213
        s := '';
13 daniel-mar 214
        for i := 0 to sl.Count - 1 do s := s + WideString(Trim(sl.Strings[i]));
2 daniel-mar 215
        ZeroMemory(WriteTo, (3*result+1)*SizeOf(WideChar));
216
        CopyMemory(WriteTo, @s[1], 3*result*SizeOf(WideChar));
217
      end;
218
    finally
219
      x.Free;
220
      if Assigned(WriteTo) then sl.Free;
221
    end;
222
  except
223
    on E: Exception do
224
    begin
225
      if Flags and CONVERT_DONT_SHOW_ERRORS = 0 then MessageDlg(e.Message, mtError, [mbOk], 0);
226
      result := -1;
227
    end;
228
  end;
229
end;
230
 
231
function AcceptedCurrenciesA(WriteTo: LPSTR; MaxAge: integer; Flags: TVtsCurConvFlags;
232
                             HistoricDate: TDate): Integer; stdcall;
233
var
234
  x: TVtsCurConv;
235
  sl: TStringList;
236
  s: AnsiString;
237
  i: integer;
238
begin
239
  try
240
    x := TVtsCurConv.Create;
241
    if Assigned(WriteTo) then sl := TStringList.Create else sl := nil;
242
    try
243
      x.Secure                 := Flags and CONVERT_USE_SSL <> 0;
244
      x.MaxAgeSeconds          := MaxAge;
245
      x.ConfirmWebAccess       := Flags and CONVERT_CONFIRM_WEB_ACCESS <> 0;
246
      x.FallBackToCache        := Flags and CONVERT_FALLBACK_TO_CACHE <> 0;
247
      x.InteractiveAPIKeyInput := Flags and CONVERT_NO_INTERACTIVE_API_KEY_INPUT = 0;
248
      result := x.GetAcceptedCurrencies(sl, HistoricDate);
249
      if Assigned(WriteTo) then
250
      begin
251
        s := '';
13 daniel-mar 252
        for i := 0 to sl.Count - 1 do s := s + AnsiString(Trim(sl.Strings[i]));
2 daniel-mar 253
        ZeroMemory(WriteTo, (3*result+1)*SizeOf(AnsiChar));
254
        CopyMemory(WriteTo, @s[1], 3*result*SizeOf(AnsiChar));
255
      end;
256
    finally
257
      x.Free;
258
      if Assigned(WriteTo) then sl.Free;
259
    end;
260
  except
261
    on E: Exception do
262
    begin
263
      if Flags and CONVERT_DONT_SHOW_ERRORS = 0 then MessageDlg(e.Message, mtError, [mbOk], 0);
264
      result := -1;
265
    end;
266
  end;
267
end;
268
 
269
function DownloadNow(Flags: TVtsCurConvFlags; HistoricDate: TDate): HRESULT; stdcall;
270
var
271
  x: TVtsCurConv;
272
begin
273
  try
274
    x := TVtsCurConv.Create;
275
    try
276
      x.Secure                 := Flags and CONVERT_USE_SSL <> 0;
277
      x.MaxAgeSeconds          := 0; // Always Download
278
      x.ConfirmWebAccess       := Flags and CONVERT_CONFIRM_WEB_ACCESS <> 0;
279
      x.FallBackToCache        := Flags and CONVERT_FALLBACK_TO_CACHE <> 0;
280
      x.InteractiveAPIKeyInput := Flags and CONVERT_NO_INTERACTIVE_API_KEY_INPUT = 0;
281
      x.Convert(1, 'USD', 'USD', HistoricDate);
282
      result := S_VTSCONV_OK
283
    finally
284
      x.Free;
285
    end;
286
  except
287
    on E: Exception do
288
    begin
289
      if Flags and CONVERT_DONT_SHOW_ERRORS = 0 then MessageDlg(e.Message, mtError, [mbOk], 0);
290
      result := E_VTSCONV_GENERIC_FAILURE;
291
    end;
292
  end;
293
end;
294
 
295
exports
296
  DeleteAPIKey,
297
  WriteAPIKeyW,
298
  WriteAPIKeyA,
299
  ReadAPIKeyW,
300
  ReadAPIKeyA,
301
  ConvertW,
302
  ConvertA,
303
  AcceptedCurrenciesW,
304
  AcceptedCurrenciesA,
305
  DownloadNow;
306
 
307
begin
308
end.