cresstr.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. asmlist[al_resourcestrings].concat(tai_const.create_sym(nil))
  118. else
  119. begin
  120. objectlibrary.getdatalabel(l1);
  121. asmlist[al_resourcestrings].concat(tai_const.create_sym(l1));
  122. maybe_new_object_file(asmlist[al_const]);
  123. asmlist[al_const].concat(tai_align.Create(const_align(sizeof(aint))));
  124. asmlist[al_const].concat(tai_const.create_aint(-1));
  125. asmlist[al_const].concat(tai_const.create_aint(len));
  126. asmlist[al_const].concat(tai_label.create(l1));
  127. getmem(s,len+1);
  128. move(value^,s^,len);
  129. s[len]:=#0;
  130. asmlist[al_const].concat(tai_string.create_pchar(s,len));
  131. asmlist[al_const].concat(tai_const.create_8bit(0));
  132. end;
  133. { append Current value (nil) and hash...}
  134. asmlist[al_resourcestrings].concat(tai_const.create_sym(nil));
  135. asmlist[al_resourcestrings].concat(tai_const.create_32bit(longint(hash)));
  136. { Append the name as a ansistring. }
  137. objectlibrary.getdatalabel(l1);
  138. l:=length(name);
  139. asmlist[al_resourcestrings].concat(tai_const.create_sym(l1));
  140. maybe_new_object_file(asmlist[al_const]);
  141. asmlist[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  142. asmlist[al_const].concat(tai_const.create_aint(-1));
  143. asmlist[al_const].concat(tai_const.create_aint(l));
  144. asmlist[al_const].concat(tai_label.create(l1));
  145. getmem(s,l+1);
  146. move(Name[1],s^,l);
  147. s[l]:=#0;
  148. asmlist[al_const].concat(tai_string.create_pchar(s,l));
  149. asmlist[al_const].concat(tai_const.create_8bit(0));
  150. end;
  151. end;
  152. Var
  153. R : tresourceStringItem;
  154. begin
  155. if asmlist[al_resourcestrings]=nil then
  156. asmlist[al_resourcestrings]:=taasmoutput.create;
  157. maybe_new_object_file(asmlist[al_resourcestrings]);
  158. new_section(asmlist[al_resourcestrings],sec_data,'',4);
  159. asmlist[al_resourcestrings].concat(tai_align.create(const_align(sizeof(aint))));
  160. asmlist[al_resourcestrings].concat(tai_symbol.createname_global(
  161. make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  162. asmlist[al_resourcestrings].concat(tai_const.create_32bit(resstrcount));
  163. R:=TResourceStringItem(List.First);
  164. while assigned(R) do
  165. begin
  166. AppendToAsmResList(R);
  167. R:=TResourceStringItem(R.Next);
  168. end;
  169. asmlist[al_resourcestrings].concat(tai_symbol_end.createname(
  170. make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,'')));
  171. end;
  172. { ---------------------------------------------------------------------
  173. Insert 1 resource string in all tables.
  174. ---------------------------------------------------------------------}
  175. function Tresourcestrings.Register(const name : string;p : pchar;len : longint) : longint;
  176. begin
  177. List.Concat(tResourceStringItem.Create(lower(current_module.modulename^+'.'+Name),p,len));
  178. Register:=ResStrCount;
  179. inc(ResStrCount);
  180. end;
  181. Procedure Tresourcestrings.WriteResourceFile(const FileName : String);
  182. Type
  183. TMode = (quoted,unquoted);
  184. Var
  185. F : Text;
  186. Mode : TMode;
  187. R : TResourceStringItem;
  188. C : char;
  189. Col,i : longint;
  190. Procedure Add(Const S : String);
  191. begin
  192. Write(F,S);
  193. Col:=Col+length(s);
  194. end;
  195. begin
  196. If List.Empty then
  197. exit;
  198. message1 (general_i_writingresourcefile,SplitFileName(filename));
  199. Assign(F,Filename);
  200. {$i-}
  201. Rewrite(f);
  202. {$i+}
  203. If IOresult<>0 then
  204. begin
  205. message1(general_e_errorwritingresourcefile,filename);
  206. exit;
  207. end;
  208. R:=TResourceStringItem(List.First);
  209. While assigned(R) do
  210. begin
  211. writeln(f);
  212. Writeln(f,'# hash value = ',R.hash);
  213. col:=0;
  214. Add(R.Name+'=');
  215. Mode:=unquoted;
  216. For I:=0 to R.Len-1 do
  217. begin
  218. C:=R.Value[i];
  219. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  220. begin
  221. If mode=Quoted then
  222. Add(c)
  223. else
  224. begin
  225. Add(''''+c);
  226. mode:=quoted
  227. end;
  228. end
  229. else
  230. begin
  231. If Mode=quoted then
  232. begin
  233. Add('''');
  234. mode:=unquoted;
  235. end;
  236. Add('#'+tostr(ord(c)));
  237. end;
  238. If Col>72 then
  239. begin
  240. if mode=quoted then
  241. Write (F,'''');
  242. Writeln(F,'+');
  243. Col:=0;
  244. Mode:=unQuoted;
  245. end;
  246. end;
  247. if mode=quoted then
  248. writeln (f,'''');
  249. Writeln(f);
  250. R:=TResourceStringItem(R.Next);
  251. end;
  252. close(f);
  253. end;
  254. end.