cresstr.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. {
  2. Copyright (c) 1998-2002 by Michael van Canneyt
  3. Handles al_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. Tal_resourcestrings=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. al_resourcestrings : Tal_resourcestrings;
  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. Tal_resourcestrings
  94. ---------------------------------------------------------------------}
  95. Constructor Tal_resourcestrings.Create;
  96. begin
  97. List:=TStringList.Create;
  98. ResStrCount:=0;
  99. end;
  100. Destructor Tal_resourcestrings.Destroy;
  101. begin
  102. List.Free;
  103. end;
  104. { ---------------------------------------------------------------------
  105. Create the full asmlist for al_resourcestrings.
  106. ---------------------------------------------------------------------}
  107. procedure Tal_resourcestrings.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[aasmtai.al_resourcestrings].concat(tai_const.create_sym(nil))
  118. else
  119. begin
  120. objectlibrary.getdatalabel(l1);
  121. asmlist[aasmtai.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_length_pchar(s,len));
  131. asmlist[al_const].concat(tai_const.create_8bit(0));
  132. end;
  133. { append Current value (nil) and hash...}
  134. asmlist[aasmtai.al_resourcestrings].concat(tai_const.create_sym(nil));
  135. asmlist[aasmtai.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[aasmtai.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_length_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[aasmtai.al_resourcestrings]=nil then
  156. asmlist[aasmtai.al_resourcestrings]:=taasmoutput.create;
  157. maybe_new_object_file(asmlist[aasmtai.al_resourcestrings]);
  158. asmlist[aasmtai.al_resourcestrings].concat(tai_align.create(const_align(sizeof(aint))));
  159. asmlist[aasmtai.al_resourcestrings].concat(tai_symbol.createname_global(
  160. make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  161. asmlist[aasmtai.al_resourcestrings].concat(tai_const.create_32bit(resstrcount));
  162. R:=TResourceStringItem(List.First);
  163. while assigned(R) do
  164. begin
  165. AppendToAsmResList(R);
  166. R:=TResourceStringItem(R.Next);
  167. end;
  168. asmlist[aasmtai.al_resourcestrings].concat(tai_symbol_end.createname(
  169. current_module.modulename^+'_'+'RESOURCESTRINGLIST'));
  170. end;
  171. { ---------------------------------------------------------------------
  172. Insert 1 resource string in all tables.
  173. ---------------------------------------------------------------------}
  174. function Tal_resourcestrings.Register(const name : string;p : pchar;len : longint) : longint;
  175. begin
  176. List.Concat(tResourceStringItem.Create(lower(current_module.modulename^+'.'+Name),p,len));
  177. Register:=ResStrCount;
  178. inc(ResStrCount);
  179. end;
  180. Procedure Tal_resourcestrings.WriteResourceFile(const FileName : String);
  181. Type
  182. TMode = (quoted,unquoted);
  183. Var
  184. F : Text;
  185. Mode : TMode;
  186. R : TResourceStringItem;
  187. C : char;
  188. Col,i : longint;
  189. Procedure Add(Const S : String);
  190. begin
  191. Write(F,S);
  192. Col:=Col+length(s);
  193. end;
  194. begin
  195. If List.Empty then
  196. exit;
  197. message1 (general_i_writingresourcefile,SplitFileName(filename));
  198. Assign(F,Filename);
  199. {$i-}
  200. Rewrite(f);
  201. {$i+}
  202. If IOresult<>0 then
  203. begin
  204. message1(general_e_errorwritingresourcefile,filename);
  205. exit;
  206. end;
  207. R:=TResourceStringItem(List.First);
  208. While assigned(R) do
  209. begin
  210. writeln(f);
  211. Writeln(f,'# hash value = ',R.hash);
  212. col:=0;
  213. Add(R.Name+'=');
  214. Mode:=unquoted;
  215. For I:=0 to R.Len-1 do
  216. begin
  217. C:=R.Value[i];
  218. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  219. begin
  220. If mode=Quoted then
  221. Add(c)
  222. else
  223. begin
  224. Add(''''+c);
  225. mode:=quoted
  226. end;
  227. end
  228. else
  229. begin
  230. If Mode=quoted then
  231. begin
  232. Add('''');
  233. mode:=unquoted;
  234. end;
  235. Add('#'+tostr(ord(c)));
  236. end;
  237. If Col>72 then
  238. begin
  239. if mode=quoted then
  240. Write (F,'''');
  241. Writeln(F,'+');
  242. Col:=0;
  243. Mode:=unQuoted;
  244. end;
  245. end;
  246. if mode=quoted then
  247. writeln (f,'''');
  248. Writeln(f);
  249. R:=TResourceStringItem(R.Next);
  250. end;
  251. close(f);
  252. end;
  253. end.