Subversion Repositories currency_converter

Rev

Rev 2 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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