2
0

cresstr.pas 7.5 KB

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