Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. unit ZMXcpt19;
  2.  
  3. (*
  4.   ZMXcpt19.pas - Exception class for ZipMaster
  5.     Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht,
  6.       Eric W. Engler and Chris Vleghert.
  7.  
  8.         This file is part of TZipMaster Version 1.9.
  9.  
  10.     TZipMaster is free software: you can redistribute it and/or modify
  11.     it under the terms of the GNU Lesser General Public License as published by
  12.     the Free Software Foundation, either version 3 of the License, or
  13.     (at your option) any later version.
  14.  
  15.     TZipMaster is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public License
  21.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  22.  
  23.     contact: problems@delphizip.org (include ZipMaster in the subject).
  24.     updates: http://www.delphizip.org
  25.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  26.  
  27.   modified 2009-11-22
  28. ---------------------------------------------------------------------------*)
  29.  
  30. interface
  31.  
  32. uses
  33.   SysUtils;
  34.        
  35. {$IFNDEF UNICODE}
  36. type
  37.   TZMXArgs = (zxaNoStr, zxaStr, zxa2Str);
  38. {$ENDIF}
  39.  
  40. type
  41.   EZMException = class(Exception)
  42. {$IFNDEF UNICODE}
  43.   private
  44.     fArgs: TZMXArgs;
  45.     fStr1: String;
  46.     fStr2: string;
  47. {$ENDIF}
  48.   protected
  49.     // We do not always want to see a message after an exception.
  50.     fDisplayMsg: Boolean;
  51.     // We also save the Resource ID in case the resource is not linked in the application.
  52.     fResIdent: Integer;
  53.     constructor CreateResFmt(Ident: Integer; const Args: array of const);
  54.   public
  55.     constructor CreateDisp(const Message: String; const Display: Boolean);
  56.  
  57.     constructor CreateResDisp(Ident: Integer; const Display: Boolean);
  58.     constructor CreateResInt(Ident, anInt: Integer);
  59.     constructor CreateResStr(Ident: Integer; const Str1: String);
  60.     constructor CreateRes2Str(Ident: Integer; const Str1, Str2: String);
  61. {$IFNDEF UNICODE}
  62.     function TheMessage(AsUTF8: boolean): string;
  63. {$ENDIF}
  64.  
  65.     property ResId: Integer Read fResIdent write fResIdent;
  66.     property DisplayMsg: boolean Read fDisplayMsg;
  67.   end;
  68.  
  69. type
  70.   EZipMaster = class(EZMException)
  71.  
  72.   end;
  73.  
  74.  
  75. implementation
  76.  
  77. uses
  78.   ZMMsg19, ZMMsgStr19 {$IFNDEF UNICODE}, ZMUTF819{$ENDIF};
  79.  
  80. const
  81.   ERRORMSG: String = 'Failed to Locate string';
  82.  
  83. // allow direct translation for negative error values
  84. function Id(err: integer): integer;
  85. begin
  86.   Result := err;
  87.   if (Result < 0) and (Result >= -TM_SystemError)
  88.     and (Result <= -DT_Language) then
  89.     Result := -Result;
  90. end;
  91.  
  92. //constructor EZMException.Create(const msg: String);
  93. //begin
  94. //  inherited Create(msg);
  95. //  fDisplayMsg := True;
  96. //  fResIdent   := DS_UnknownError;
  97. //end;
  98.  
  99. constructor EZMException.CreateDisp(const Message: String; const Display: Boolean);
  100. begin
  101.   inherited Create(Message);
  102.   fDisplayMsg := Display;
  103.   fResIdent   := DS_UnknownError;
  104. {$IFNDEF UNICODE}
  105.   fArgs := zxaNoStr;
  106. {$ENDIF}
  107. end;
  108.  
  109. constructor EZMException.CreateResFmt(Ident: Integer; const Args: array of const);
  110. begin
  111. //  CreateFmt(Ident, Args);
  112.   inherited Create(ERRORMSG);
  113.   fResIdent := Id(Ident);
  114.   Message := LoadZipStr(fResIdent);
  115.   Message := Format(Message, Args);
  116.   fDisplayMsg := True;  
  117. {$IFNDEF UNICODE}
  118.   fArgs := zxaNoStr;
  119. {$ENDIF}
  120. end;
  121.  
  122. constructor EZMException.CreateResDisp(Ident: Integer; const Display: Boolean);
  123. begin
  124.   inherited Create(ERRORMSG);
  125.   fResIdent := Id(Ident);
  126.   Message := LoadZipStr(fResIdent);
  127.   fDisplayMsg := Display;  
  128. {$IFNDEF UNICODE}
  129.   fArgs := zxaNoStr;
  130. {$ENDIF}
  131. end;
  132.  
  133. constructor EZMException.CreateResInt(Ident, anInt: Integer);
  134. begin
  135.   CreateResFmt(Ident, [anInt]);
  136. end;
  137.  
  138. constructor EZMException.CreateResStr(Ident: Integer; const Str1: String);
  139. begin
  140.   CreateResFmt(Ident, [Str1]);
  141. {$IFNDEF UNICODE}
  142.   fArgs := zxaStr;
  143.   fStr1 := Str1;
  144. {$ENDIF}
  145. end;
  146.  
  147. constructor EZMException.CreateRes2Str(Ident: Integer; const Str1, Str2: String);
  148. begin
  149.   CreateResFmt(Ident, [Str1, Str2]);  
  150. {$IFNDEF UNICODE}
  151.   fArgs := zxa2Str;
  152.   fStr1 := Str1;
  153.   fStr2 := Str2;
  154. {$ENDIF}
  155. end;
  156.  
  157. {$IFNDEF UNICODE}
  158. function EZMException.TheMessage(AsUTF8: boolean): string;
  159. begin
  160.   if not AsUTF8 then
  161.     Result := Message
  162.   else
  163.   begin
  164.     if fArgs <= zxaNoStr then
  165.       Result := StrToUTF8(Message)
  166.     else
  167.     begin
  168.       Result := LoadZipStr(fResIdent);
  169.       if Result <> '' then
  170.       begin
  171.         // we need the format string as UTF8
  172.         Result := StrToUTF8(Result);
  173.         case fArgs of
  174.           zxaStr:Result := Format(Result, [fStr1]);
  175.           zxa2Str:Result := Format(Result, [fStr1, fStr2]);
  176.         end;
  177.       end;
  178.     end;
  179.   end;
  180. end;
  181. {$ENDIF}
  182.  
  183. end.
  184.