/vendor/jvcl/common/JvGnugettextD5.pas

http://my-chuanqi.googlecode.com/ · Pascal · 281 lines · 265 code · 8 blank · 8 comment · 0 complexity · 25560ad4c16d140c485c2b458d7cca1c MD5 · raw file

  1. unit JvGnugettextD5;
  2. // Delphi 5 optimized interface for gnugettext.pas
  3. // This unit must only be used on Delphi 5. When you upgrade to Delphi 6 or
  4. // later, you should remove this unit and replace all reference to gnugettextD5
  5. // with refernces to gnugettext.
  6. interface
  7. uses
  8. Classes;
  9. // Ansistring versions of the api
  10. function _(const szMsgId: string): string;
  11. function gettext(const szMsgId: string): string;
  12. function dgettext(const szDomain: string; const szMsgId: string): string;
  13. procedure TranslateComponent(AnObject: TComponent);
  14. //*****************************************************************************
  15. // Don't use anything in the interface below this line.
  16. // It only contains code or gnugettext.pas to make it compile with Delphi 5.
  17. type
  18. UTF8String = AnsiString;
  19. const
  20. PathDelim = '\';
  21. sLineBreak = #13#10;
  22. function GetEnvironmentVariable(const VarName: string): string;
  23. function DirectoryExists(const FileName: string): Boolean;
  24. function IncludeTrailingPathDelimiter(s: string): string;
  25. function ExcludeTrailingPathDelimiter(s: string): string;
  26. procedure RaiseLastOSError;
  27. function StrToFloatDef(const S: string; Default: Extended): Extended;
  28. function Utf8Decode(const S: UTF8String): WideString;
  29. function Utf8Encode(const WS: WideString): UTF8String;
  30. implementation
  31. uses
  32. Windows, SysUtils,
  33. JvGnugettext;
  34. function GetEnvironmentVariable(const VarName: string): string;
  35. var Size: Integer;
  36. begin
  37. Size := Windows.GetEnvironmentVariable(PChar(VarName), nil, 0);
  38. SetLength(Result, Size - 1);
  39. Windows.GetEnvironmentVariable(PChar(VarName), PChar(Result), Size);
  40. end;
  41. function DirectoryExists(const Filename: string): Boolean;
  42. var
  43. Attr: Cardinal;
  44. begin
  45. Attr := GetFileAttributes(PChar(Filename));
  46. Result := (Attr <> $FFFFFFFF) and (Attr and FILE_ATTRIBUTE_DIRECTORY <> 0);
  47. end;
  48. function IncludeTrailingPathDelimiter(s: string): string;
  49. begin
  50. Result := IncludeTrailingBackslash(s);
  51. end;
  52. function ExcludeTrailingPathDelimiter(s: string): string;
  53. begin
  54. Result := ExcludeTrailingBackslash(s);
  55. end;
  56. procedure RaiseLastOSError;
  57. begin
  58. RaiseLastWin32Error;
  59. end;
  60. function StrToFloatDef(const S: string; Default: Extended): Extended;
  61. begin
  62. if not TextToFloat(PChar(S), Result, fvExtended) then
  63. Result := Default;
  64. end;
  65. function UnicodeToUtf8(Dest: PChar; MaxDestBytes: Cardinal; Source: PWideChar; SourceChars: Cardinal): Cardinal;
  66. var
  67. i, Count: Cardinal;
  68. c: Cardinal;
  69. begin
  70. Result := 0;
  71. if Source = nil then
  72. Exit;
  73. Count := 0;
  74. i := 0;
  75. if Dest <> nil then
  76. begin
  77. while (i < SourceChars) and (Count < MaxDestBytes) do
  78. begin
  79. c := Cardinal(Source[i]);
  80. Inc(i);
  81. if c <= $7F then
  82. begin
  83. Dest[count] := Char(c);
  84. Inc(count);
  85. end
  86. else if c > $7FF then
  87. begin
  88. if Count + 3 > MaxDestBytes then
  89. Break;
  90. Dest[Count] := Char($E0 or (c shr 12));
  91. Dest[Count + 1] := Char($80 or ((c shr 6) and $3F));
  92. Dest[Count + 2] := Char($80 or (c and $3F));
  93. Inc(Count, 3);
  94. end
  95. else // $7F < Source[i] <= $7FF
  96. begin
  97. if Count + 2 > MaxDestBytes then
  98. Break;
  99. Dest[Count] := Char($C0 or (c shr 6));
  100. Dest[Count + 1] := Char($80 or (c and $3F));
  101. Inc(Count, 2);
  102. end;
  103. end;
  104. if Count >= MaxDestBytes then
  105. Count := MaxDestBytes - 1;
  106. Dest[Count] := #0;
  107. end
  108. else
  109. begin
  110. while i < SourceChars do
  111. begin
  112. c := Integer(Source[i]);
  113. Inc(i);
  114. if c > $7F then
  115. begin
  116. if c > $7FF then
  117. Inc(Count);
  118. Inc(Count);
  119. end;
  120. Inc(Count);
  121. end;
  122. end;
  123. Result := Count + 1; // convert zero based index to byte count
  124. end;
  125. function Utf8ToUnicode(Dest: PWideChar; MaxDestChars: Cardinal; Source: PChar; SourceBytes: Cardinal): Cardinal;
  126. var
  127. i, Count: Cardinal;
  128. c: Byte;
  129. wc: Cardinal;
  130. begin
  131. if Source = nil then
  132. begin
  133. Result := 0;
  134. Exit;
  135. end;
  136. Result := Cardinal(-1);
  137. count := 0;
  138. i := 0;
  139. if Dest <> nil then
  140. begin
  141. while (i < SourceBytes) and (count < MaxDestChars) do
  142. begin
  143. wc := Cardinal(Source[i]);
  144. Inc(i);
  145. if (wc and $80) <> 0 then
  146. begin
  147. if i >= SourceBytes then
  148. Exit; // incomplete multibyte char
  149. wc := wc and $3F;
  150. if (wc and $20) <> 0 then
  151. begin
  152. c := Byte(Source[i]);
  153. Inc(i);
  154. if (c and $C0) <> $80 then
  155. Exit; // malformed trail byte or out of range char
  156. if i >= SourceBytes then
  157. Exit; // incomplete multibyte char
  158. wc := (wc shl 6) or (c and $3F);
  159. end;
  160. c := Byte(Source[i]);
  161. Inc(i);
  162. if (c and $C0) <> $80 then
  163. Exit; // malformed trail byte
  164. Dest[Count] := WideChar((wc shl 6) or (c and $3F));
  165. end
  166. else
  167. Dest[Count] := WideChar(wc);
  168. Inc(Count);
  169. end;
  170. if Count >= MaxDestChars then
  171. Count := MaxDestChars - 1;
  172. Dest[Count] := #0;
  173. end
  174. else
  175. begin
  176. while (i < SourceBytes) do
  177. begin
  178. c := Byte(Source[i]);
  179. Inc(i);
  180. if (c and $80) <> 0 then
  181. begin
  182. if i >= SourceBytes then
  183. Exit; // incomplete multibyte char
  184. c := c and $3F;
  185. if (c and $20) <> 0 then
  186. begin
  187. c := Byte(Source[i]);
  188. Inc(i);
  189. if (c and $C0) <> $80 then
  190. Exit; // malformed trail byte or out of range char
  191. if i >= SourceBytes then
  192. Exit; // incomplete multibyte char
  193. end;
  194. c := Byte(Source[i]);
  195. Inc(i);
  196. if (c and $C0) <> $80 then
  197. Exit; // malformed trail byte
  198. end;
  199. Inc(Count);
  200. end;
  201. end;
  202. Result := Count + 1;
  203. end;
  204. function Utf8Decode(const S: UTF8String): WideString;
  205. var
  206. L: Integer;
  207. Temp: WideString;
  208. begin
  209. Result := '';
  210. if S = '' then
  211. Exit;
  212. SetLength(Temp, Length(S));
  213. L := Utf8ToUnicode(PWideChar(Temp), Length(Temp) + 1, PChar(S), Length(S));
  214. if L > 0 then
  215. SetLength(Temp, L - 1)
  216. else
  217. Temp := '';
  218. Result := Temp;
  219. end;
  220. function Utf8Encode(const WS: WideString): UTF8String;
  221. var
  222. L: Integer;
  223. Temp: UTF8String;
  224. begin
  225. Result := '';
  226. if WS = '' then
  227. Exit;
  228. SetLength(Temp, Length(WS) * 3); // SetLength includes space for null terminator
  229. L := UnicodeToUtf8(PChar(Temp), Length(Temp) + 1, PWideChar(WS), Length(WS));
  230. if L > 0 then
  231. SetLength(Temp, L - 1)
  232. else
  233. Temp := '';
  234. Result := Temp;
  235. end;
  236. function _(const szMsgId: string): string;
  237. begin
  238. Result := string(DefaultInstance.gettext(DefaultInstance.ansi2wide(szMsgId)));
  239. end;
  240. function gettext(const szMsgId: string): string;
  241. begin
  242. Result := string(DefaultInstance.gettext(DefaultInstance.ansi2wide(szMsgId)));
  243. end;
  244. function dgettext(const szDomain: string; const szMsgId: string): string;
  245. begin
  246. Result := string(DefaultInstance.dgettext(szDomain, DefaultInstance.ansi2wide(szMsgId)));
  247. end;
  248. procedure TranslateComponent(AnObject: TComponent);
  249. begin
  250. JvGnugettext.TranslateComponent(AnObject);
  251. end;
  252. end.