cresstr.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Michael van Canneyt
  4. Handles resourcestrings
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cresstr;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses;
  23. Type
  24. { These are used to form a singly-linked list, ordered by hash value }
  25. TResourceStringItem = class(TLinkedListItem)
  26. Name : String;
  27. Value : Pchar;
  28. Len : Longint;
  29. hash : Cardinal;
  30. constructor Create(const AName:string;AValue:pchar;ALen:longint);
  31. destructor Destroy;override;
  32. procedure CalcHash;
  33. end;
  34. TResourceStrings=class
  35. private
  36. List : TLinkedList;
  37. public
  38. ResStrCount : longint;
  39. constructor Create;
  40. destructor Destroy;override;
  41. function Register(Const name : string;p : pchar;len : longint) : longint;
  42. procedure CreateResourceStringList;
  43. Procedure WriteResourceFile(const FileName : String);
  44. end;
  45. var
  46. ResourceStrings : TResourceStrings;
  47. implementation
  48. uses
  49. cutils,globtype,globals,
  50. symdef,
  51. verbose,fmodule,
  52. aasmbase,aasmtai,
  53. aasmcpu;
  54. { ---------------------------------------------------------------------
  55. Calculate hash value, based on the string
  56. ---------------------------------------------------------------------}
  57. { ---------------------------------------------------------------------
  58. TRESOURCESTRING_ITEM
  59. ---------------------------------------------------------------------}
  60. constructor TResourceStringItem.Create(const AName:string;AValue:pchar;ALen:longint);
  61. begin
  62. inherited Create;
  63. Name:=AName;
  64. Len:=ALen;
  65. GetMem(Value,Len);
  66. Move(AValue^,Value^,Len);
  67. CalcHash;
  68. end;
  69. destructor TResourceStringItem.Destroy;
  70. begin
  71. FreeMem(Value,Len);
  72. end;
  73. {$ifdef ver1_0}
  74. {$R-}
  75. {$endif}
  76. procedure TResourceStringItem.CalcHash;
  77. Var
  78. g : Cardinal;
  79. I : longint;
  80. begin
  81. hash:=0;
  82. For I:=0 to Len-1 do { 0 terminated }
  83. begin
  84. hash:=hash shl 4;
  85. inc(Hash,Ord(Value[i]));
  86. g:=hash and ($f shl 28);
  87. if g<>0 then
  88. begin
  89. hash:=hash xor (g shr 24);
  90. hash:=hash xor g;
  91. end;
  92. end;
  93. If Hash=0 then
  94. Hash:=$ffffffff;
  95. end;
  96. { ---------------------------------------------------------------------
  97. TRESOURCESTRINGS
  98. ---------------------------------------------------------------------}
  99. Constructor TResourceStrings.Create;
  100. begin
  101. List:=TStringList.Create;
  102. ResStrCount:=0;
  103. end;
  104. Destructor TResourceStrings.Destroy;
  105. begin
  106. List.Free;
  107. end;
  108. { ---------------------------------------------------------------------
  109. Create the full asmlist for resourcestrings.
  110. ---------------------------------------------------------------------}
  111. procedure TResourceStrings.CreateResourceStringList;
  112. Procedure AppendToAsmResList (P : TResourceStringItem);
  113. Var
  114. l1 : tasmlabel;
  115. s : pchar;
  116. l : longint;
  117. begin
  118. With P Do
  119. begin
  120. if (Value=nil) or (len=0) then
  121. resourcestringlist.concat(tai_const.create_sym(nil))
  122. else
  123. begin
  124. objectlibrary.getdatalabel(l1);
  125. resourcestringlist.concat(tai_const.create_sym(l1));
  126. consts.concat(tai_align.Create(const_align(sizeof(aint))));
  127. consts.concat(tai_const.create_aint(-1));
  128. consts.concat(tai_const.create_aint(len));
  129. consts.concat(tai_label.create(l1));
  130. getmem(s,len+1);
  131. move(Value^,s^,len);
  132. s[len]:=#0;
  133. consts.concat(tai_string.create_length_pchar(s,len));
  134. consts.concat(tai_const.create_8bit(0));
  135. end;
  136. { append Current value (nil) and hash...}
  137. resourcestringlist.concat(tai_const.create_sym(nil));
  138. resourcestringlist.concat(tai_const.create_32bit(longint(hash)));
  139. { Append the name as a ansistring. }
  140. objectlibrary.getdatalabel(l1);
  141. L:=Length(Name);
  142. resourcestringlist.concat(tai_const.create_sym(l1));
  143. consts.concat(tai_align.Create(const_align(sizeof(aint))));
  144. consts.concat(tai_const.create_aint(-1));
  145. consts.concat(tai_const.create_aint(l));
  146. consts.concat(tai_label.create(l1));
  147. getmem(s,l+1);
  148. move(Name[1],s^,l);
  149. s[l]:=#0;
  150. consts.concat(tai_string.create_length_pchar(s,l));
  151. consts.concat(tai_const.create_8bit(0));
  152. end;
  153. end;
  154. Var
  155. R : tresourceStringItem;
  156. begin
  157. if not(assigned(resourcestringlist)) then
  158. resourcestringlist:=taasmoutput.create;
  159. resourcestringlist.insert(tai_const.create_32bit(resstrcount));
  160. resourcestringlist.insert(tai_symbol.createname_global(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  161. resourcestringlist.insert(tai_align.Create(const_align(sizeof(aint))));
  162. R:=TResourceStringItem(List.First);
  163. While assigned(R) do
  164. begin
  165. AppendToAsmResList(R);
  166. R:=TResourceStringItem(R.Next);
  167. end;
  168. resourcestringlist.concat(tai_symbol_end.createname(current_module.modulename^+'_'+'RESOURCESTRINGLIST'));
  169. end;
  170. { ---------------------------------------------------------------------
  171. Insert 1 resource string in all tables.
  172. ---------------------------------------------------------------------}
  173. function TResourceStrings.Register(const name : string;p : pchar;len : longint) : longint;
  174. begin
  175. List.Concat(tResourceStringItem.Create(lower(current_module.modulename^+'.'+Name),p,len));
  176. Register:=ResStrCount;
  177. inc(ResStrCount);
  178. end;
  179. Procedure TResourceStrings.WriteResourceFile(const FileName : String);
  180. Type
  181. TMode = (quoted,unquoted);
  182. Var
  183. F : Text;
  184. Mode : TMode;
  185. R : TResourceStringItem;
  186. C : char;
  187. Col,i : longint;
  188. Procedure Add(Const S : String);
  189. begin
  190. Write(F,S);
  191. Col:=Col+length(s);
  192. end;
  193. begin
  194. If List.Empty then
  195. exit;
  196. message1 (general_i_writingresourcefile,SplitFileName(filename));
  197. Assign(F,Filename);
  198. {$i-}
  199. Rewrite(f);
  200. {$i+}
  201. If IOresult<>0 then
  202. begin
  203. message1(general_e_errorwritingresourcefile,filename);
  204. exit;
  205. end;
  206. R:=TResourceStringItem(List.First);
  207. While assigned(R) do
  208. begin
  209. writeln(f);
  210. Writeln(f,'# hash value = ',R.hash);
  211. col:=0;
  212. Add(R.Name+'=');
  213. Mode:=unquoted;
  214. For I:=0 to R.Len-1 do
  215. begin
  216. C:=R.Value[i];
  217. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  218. begin
  219. If mode=Quoted then
  220. Add(c)
  221. else
  222. begin
  223. Add(''''+c);
  224. mode:=quoted
  225. end;
  226. end
  227. else
  228. begin
  229. If Mode=quoted then
  230. begin
  231. Add('''');
  232. mode:=unquoted;
  233. end;
  234. Add('#'+tostr(ord(c)));
  235. end;
  236. If Col>72 then
  237. begin
  238. if mode=quoted then
  239. Write (F,'''');
  240. Writeln(F,'+');
  241. Col:=0;
  242. Mode:=unQuoted;
  243. end;
  244. end;
  245. if mode=quoted then
  246. writeln (f,'''');
  247. Writeln(f);
  248. R:=TResourceStringItem(R.Next);
  249. end;
  250. close(f);
  251. end;
  252. end.
  253. {
  254. $Log$
  255. Revision 1.27 2004-11-01 20:22:04 florian
  256. * resource ansistrings now use aint for len and ref. count as well
  257. Revision 1.26 2004/07/12 17:58:19 peter
  258. * remove maxlen field from ansistring/widestrings
  259. Revision 1.25 2004/06/20 08:55:29 florian
  260. * logs truncated
  261. Revision 1.24 2004/06/16 20:07:07 florian
  262. * dwarf branch merged
  263. Revision 1.23 2004/05/23 15:23:30 peter
  264. * fixed qword(longint) that removed sign from the number
  265. * removed code in the compiler that relied on wrong qword(longint)
  266. code generation
  267. Revision 1.22.2.3 2004/05/01 16:02:09 peter
  268. * POINTER_SIZE replaced with sizeof(aint)
  269. * aint,aword,tconst*int moved to globtype
  270. Revision 1.22.2.2 2004/04/27 18:18:25 peter
  271. * aword -> aint
  272. Revision 1.22.2.1 2004/04/12 14:45:11 peter
  273. * tai_const_symbol and tai_const merged
  274. }