/components/lazreport/source/sysutilsadds.pas

http://github.com/graemeg/lazarus · Pascal · 230 lines · 146 code · 30 blank · 54 comment · 12 complexity · fed2986866433b23bc29525437b290ca MD5 · raw file

  1. {
  2. ***************************************************************************
  3. * *
  4. * This source is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU Library General Public License *
  6. * as published by the Free Software Foundation; either version 2 of *
  7. * the License, or (at your option) any later version. *
  8. * *
  9. * This code is distributed in the hope that it will be useful, but *
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  12. * General Public License for more details. *
  13. * *
  14. * A copy of the GNU Library General Public License is available on the *
  15. * Web at <http://www.gnu.org/copyleft/lgpl.html>. You can also *
  16. * obtain it by writing to the Free Software Foundation, *
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. * *
  19. ***************************************************************************
  20. Author: Olivier GUILBAUD
  21. Abstract:
  22. This code initialze all FPC Date and Numbers formats consts
  23. with the localized format.
  24. The KDE configuration it's suported (Default config and User config)
  25. I have tested this unit with Linux Mandrake 10.0.
  26. For the Euro Monay symbol, I have overriten the result because the config
  27. file it's utf8 coded.
  28. History
  29. 08/10/2004 OG - Create
  30. ------------------------------------------------------------------------------}
  31. unit SysUtilsAdds;
  32. {$mode objfpc}{$H+}
  33. interface
  34. {$IFDEF UNIX}
  35. {$IF (FPC_VERSION > 2) or
  36. ((FPC_VERSION = 2)
  37. and ((FPC_RELEASE > 2) or
  38. ((FPC_RELEASE = 2) and (FPC_PATCH >= 1))))}
  39. {$DEFINE USECLOCALE}
  40. {$ENDIF}
  41. {$ENDIF}
  42. {$IFDEF USECLOCALE}
  43. uses
  44. Classes, SysUtils, FileUtil,
  45. {$info if compiler can't find clocale unit, you probably are using old 2.2.1 or 2.3.1 version, please upgrade}
  46. CLocale;
  47. {$ENDIF}
  48. implementation
  49. {$IFDEF USECLOCALE}
  50. procedure InitInternationalFormats;
  51. Var i : Integer;
  52. St : string;
  53. Lg : String;
  54. LstKde : TStringList;
  55. DFMT : string;
  56. TFMT : string;
  57. TFMTAP : string;
  58. Function ConvertLinuxDateFormatToFp(Fmt,Dflt : String):string;
  59. begin
  60. try
  61. Fmt:=StringReplace(Fmt,'%x',DFMT,[rfReplaceAll]);
  62. Fmt:=StringReplace(Fmt,'%X',TFMT,[rfReplaceAll]);
  63. Fmt:=StringReplace(Fmt,'%r',TFMTAP,[rfReplaceAll]);
  64. Fmt:=StringReplace(Fmt,'%a','ddd',[rfReplaceAll]);
  65. Fmt:=StringReplace(Fmt,'%A','dddd',[rfReplaceAll]);
  66. Fmt:=StringReplace(Fmt,'%b','mmm',[rfReplaceAll]);
  67. Fmt:=StringReplace(Fmt,'%B','mmmm',[rfReplaceAll]);
  68. Fmt:=StringReplace(Fmt,'%c','c',[rfReplaceAll]);
  69. Fmt:=StringReplace(Fmt,'%d','dd',[rfReplaceAll]);
  70. Fmt:=StringReplace(Fmt,'%D','mm/dd/yy',[rfReplaceAll]);
  71. Fmt:=StringReplace(Fmt,'%e','d',[rfReplaceAll]);
  72. Fmt:=StringReplace(Fmt,'%F','yyyy-mm-dd',[rfReplaceAll]);
  73. Fmt:=StringReplace(Fmt,'%g','yy',[rfReplaceAll]);
  74. Fmt:=StringReplace(Fmt,'%G','yyyy',[rfReplaceAll]);
  75. Fmt:=StringReplace(Fmt,'%h','mm',[rfReplaceAll]);
  76. Fmt:=StringReplace(Fmt,'%H','hh',[rfReplaceAll]);
  77. Fmt:=StringReplace(Fmt,'%h','hh',[rfReplaceAll]);
  78. Fmt:=StringReplace(Fmt,'%k','h',[rfReplaceAll]);
  79. Fmt:=StringReplace(Fmt,'%l','h',[rfReplaceAll]);
  80. Fmt:=StringReplace(Fmt,'%m','mm',[rfReplaceAll]);
  81. Fmt:=StringReplace(Fmt,'%M','nn',[rfReplaceAll]);
  82. Fmt:=StringReplace(Fmt,'%p','ampm',[rfReplaceAll]);
  83. Fmt:=StringReplace(Fmt,'%R','hh:nn',[rfReplaceAll]);
  84. Fmt:=StringReplace(Fmt,'%S','ss',[rfReplaceAll]);
  85. Fmt:=StringReplace(Fmt,'%t',#9,[rfReplaceAll]);
  86. Fmt:=StringReplace(Fmt,'%T','hh:nn:ss',[rfReplaceAll]);
  87. Fmt:=StringReplace(Fmt,'%y','yy',[rfReplaceAll]);
  88. Fmt:=StringReplace(Fmt,'%Y','YYYY',[rfReplaceAll]);
  89. Fmt:=StringReplace(Fmt,'%%','%',[rfReplaceAll]);
  90. //No correspond values
  91. Fmt:=StringReplace(Fmt,'%n','',[rfReplaceAll]); //New line char
  92. Fmt:=StringReplace(Fmt,'%C','',[rfReplaceAll]);
  93. Fmt:=StringReplace(Fmt,'%E','',[rfReplaceAll]);
  94. Fmt:=StringReplace(Fmt,'%j','',[rfReplaceAll]);
  95. Fmt:=StringReplace(Fmt,'%o','',[rfReplaceAll]);
  96. Fmt:=StringReplace(Fmt,'%s','',[rfReplaceAll]);
  97. Fmt:=StringReplace(Fmt,'%u','',[rfReplaceAll]);
  98. Fmt:=StringReplace(Fmt,'%U','',[rfReplaceAll]);
  99. Fmt:=StringReplace(Fmt,'%V','',[rfReplaceAll]);
  100. Fmt:=StringReplace(Fmt,'%W','',[rfReplaceAll]);
  101. Fmt:=StringReplace(Fmt,'%w','',[rfReplaceAll]);
  102. Fmt:=StringReplace(Fmt,'%+','',[rfReplaceAll]);
  103. Fmt:=StringReplace(Fmt,'%z','',[rfReplaceAll]);
  104. Fmt:=StringReplace(Fmt,'%Z','',[rfReplaceAll]);
  105. finally
  106. if Trim(fmt)<>'' then
  107. Result:=Fmt
  108. else
  109. Result:=Dflt;
  110. end;
  111. end;
  112. procedure ReadKdeFileConfig(aFileName,aSection : String; alst : TStringList);
  113. Var f : Text;
  114. Stc,S : String;
  115. Flag : Boolean;
  116. begin
  117. if FileExistsUTF8(aFileName) then
  118. begin
  119. AssignFile(f,aFileName);
  120. Reset(f);
  121. try
  122. Flag:=False;
  123. While not EOF(F) do
  124. begin
  125. ReadLn(f,Stc);
  126. if ((Trim(Stc)='') or (Pos('[',Stc)=1)) and Flag then
  127. Break;
  128. if AnsiSameText(Stc,aSection) then
  129. Flag:=True;
  130. if Flag then
  131. begin
  132. S:=UpperCase(Trim(Copy(Stc,1,Pos('=',Stc)-1))); //Name
  133. System.Delete(Stc,1,Pos('=',Stc)); //Value
  134. //if code is Euro symbol (UTF8)
  135. if Stc=#226#130#172 then
  136. Stc:='¤';
  137. aLst.Values[S]:=Stc; //if name exists, the value is override
  138. end;
  139. end;
  140. finally
  141. CloseFile(F);
  142. end;
  143. end;
  144. end;
  145. function ReadKdeConfig(Ident,defaultValue : string; DateFmt : Boolean=False):string;
  146. begin
  147. Result:=LstKde.Values[UpperCase(Ident)];
  148. if Result='' then
  149. Result:=LstKde.Values[UpperCase(Format('%s[%s]',[Ident,Lg]))];
  150. if (Result<>'') and DateFmt then
  151. Result:=ConvertLinuxDateFormatToFp(Result,'');
  152. if Result='' then
  153. Result:=DefaultValue;
  154. end;
  155. begin
  156. //GNOME: GNOME_DESKTOP_SESSION_ID
  157. //KDE config
  158. if (GetEnvironmentVariable('KDE_FULL_SESSION')<>'') and
  159. (DirectoryExistsUTF8(ExpandFileNameUTF8('~/.kde/share/config'))) then
  160. begin
  161. DFMT := ShortDateFormat; //nl_langinfo(D_FMT)
  162. TFMT := ShortTimeFormat; //nl_langinfo(T_FMT)
  163. TFMTAP := LongTimeFormat; //nl_langinfo(T_FMT_AMPM)
  164. Lg:=Copy(GetEnvironmentVariableUTF8('LANG'),1,2); //Langue
  165. LstKde:=TStringList.Create;
  166. try
  167. ReadKdeFileConfig(Format('/usr/share/locale/l10n/%s/entry.desktop',[Lg]),'[KCM Locale]',LstKde);
  168. ReadKdeFileConfig(ExpandFileNameUTF8('~/.kde/share/config/kdeglobals'),'[Locale]',LstKde);
  169. St:=ReadKdeConfig('DecimalSymbol',DecimalSeparator);
  170. DecimalSeparator:=St[1];
  171. CurrencyString:=ReadKdeConfig('CurrencySymbol',CurrencyString);
  172. ThousandSeparator:=ReadKdeConfig('ThousandsSeparator',ThousandSeparator)[1];
  173. LongTimeFormat:=ReadKdeConfig('TimeFormat',LongTimeFormat,True);
  174. LongDateFormat:=ReadKdeConfig('DateFormat',LongDateFormat,True);
  175. ShortDateFormat:=ReadKdeConfig('DateFormatShort',ShortDateFormat,True);
  176. ShortTimeFormat:=ReadKdeConfig('TimeFormatShort',ShortTimeFormat,True);
  177. //Date separator
  178. for i:=1 to Length(ShortDateFormat) do
  179. begin
  180. if ShortDateFormat[i] in ['-','.','/'] then
  181. begin
  182. DateSeparator:=ShortDateFormat[i];
  183. Break;
  184. end;
  185. end;
  186. ShortDateFormat:=StringReplace(ShortDateFormat,DateSeparator,'/',[rfReplaceAll]);
  187. finally
  188. LstKde.Free;
  189. end;
  190. end;
  191. end;
  192. INITIALIZATION
  193. InitInternationalFormats;
  194. {$ENDIF}
  195. end.