cresstr.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. {
  2. $Id$
  3. Copyright (c) 1999 by the Free Pascal development team
  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. interface
  20. procedure insertresourcestrings;
  21. function registerresourcestring(Const name : string;p : pchar;len : longint) : longint;
  22. function calc_resstring_hashvalue(N : String) : longint;
  23. Procedure WriteResourceFile(FileName : String);
  24. implementation
  25. uses
  26. globals,aasm,verbose,files;
  27. Type
  28. { These are used to form a singly-linked list, ordered by hash value }
  29. PResourcestring = ^TResourceString;
  30. TResourceString = record
  31. Name : String;
  32. Value : Pchar;
  33. Len,hash : longint;
  34. Next : PResourcestring;
  35. end;
  36. const
  37. { we can use a static constant because we compile a program only once }
  38. { per compiler call }
  39. resstrcount : longint = 0;
  40. resourcefilename = 'resource.rst';
  41. Var
  42. ResourceListRoot : PResourceString;
  43. { Calculate hash value, based on the name of the string }
  44. function calc_resstring_hashvalue(N : String) : longint;
  45. Var hash,g,I : longint;
  46. begin
  47. hash:=0;
  48. For I:=0 to Length(N)-1 do { 0 terminated }
  49. begin
  50. hash:=hash shl 4;
  51. inc(Hash,Ord(N[i]));
  52. g:=hash and ($f shl 28);
  53. if g<>0 then
  54. begin
  55. hash:=hash xor (g shr 24);
  56. hash:=hash xor g;
  57. end;
  58. end;
  59. If Hash=0 then
  60. Calc_resstring_hashvalue:=Not(0)
  61. else
  62. calc_resstring_hashvalue:=Hash;
  63. end;
  64. Procedure AppendToResourceList(const name : string;p : pchar;len,hash : longint);
  65. Var R,Run,prev : PResourceString;
  66. begin
  67. inc(resstrcount);
  68. New(R);
  69. { name is lower case... }
  70. R^.Name:=Name;
  71. r^.Len:=Len;
  72. R^.Hash:=hash;
  73. GetMem(R^.Value,Len);
  74. Move(P^,R^.Value^,Len);
  75. { insert at correct position }
  76. Run:=ResourceListRoot;
  77. Prev:=Nil;
  78. While (Run<>Nil) and (Run^.Hash<Hash) do
  79. begin
  80. Prev:=Run;
  81. Run:=Run^.Next;
  82. end;
  83. If Prev<>Nil Then
  84. Prev^.next:=R;
  85. R^.Next:=Run;
  86. If ResourceListRoot=Nil then
  87. ResourceListRoot:=R;
  88. end;
  89. Procedure AppendToAsmResList (P : PResourceString);
  90. Var
  91. l1 : pasmlabel;
  92. s : pchar;
  93. begin
  94. With P^ Do
  95. begin
  96. if (Value=nil) or (len=0) then
  97. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  98. else
  99. begin
  100. getdatalabel(l1);
  101. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  102. consts^.concat(new(pai_const,init_32bit(len)));
  103. consts^.concat(new(pai_const,init_32bit(len)));
  104. consts^.concat(new(pai_const,init_32bit(-1)));
  105. consts^.concat(new(pai_label,init(l1)));
  106. getmem(s,len+1);
  107. move(Value^,s^,len);
  108. s[len]:=#0;
  109. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  110. consts^.concat(new(pai_const,init_8bit(0)));
  111. end;
  112. { append Current value (nil) and hash...}
  113. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  114. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  115. { Append the name as a ansistring. }
  116. getdatalabel(l1);
  117. Len:=Length(Name);
  118. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  119. consts^.concat(new(pai_const,init_32bit(len)));
  120. consts^.concat(new(pai_const,init_32bit(len)));
  121. consts^.concat(new(pai_const,init_32bit(-1)));
  122. consts^.concat(new(pai_label,init(l1)));
  123. getmem(s,len+1);
  124. move(Name[1],s^,len);
  125. s[len]:=#0;
  126. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  127. consts^.concat(new(pai_const,init_8bit(0)));
  128. end;
  129. end;
  130. procedure insertresourcestrings;
  131. Var R : PresourceString;
  132. begin
  133. if not(assigned(resourcestringlist)) then
  134. resourcestringlist:=new(paasmoutput,init);
  135. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  136. resourcestringlist^.insert(new(pai_symbol,initname_global('RESOURCESTRINGLIST',0)));
  137. R:=ResourceListRoot;
  138. While R<>Nil do
  139. begin
  140. AppendToAsmResList(R);
  141. R:=R^.Next;
  142. end;
  143. resourcestringlist^.concat(new(pai_symbol_end,initname('RESOURCESTRINGLIST')));
  144. end;
  145. function registerresourcestring(const name : string;p : pchar;len : longint) : longint;
  146. var
  147. fullname : string;
  148. hash : longint;
  149. begin
  150. { Calculate result }
  151. fullname:=lower(current_module^.modulename^+'.'+Name);
  152. hash:=calc_resstring_hashvalue(FullName);
  153. registerresourcestring:=hash;
  154. { we don't need to generate consts in units }
  155. if (main_module^.is_unit) then
  156. exit;
  157. if not(assigned(resourcestringlist)) then
  158. resourcestringlist:=new(paasmoutput,init);
  159. AppendToResourceList(fullname,P,Len,Hash);
  160. end;
  161. Procedure WriteResourceFile(Filename : String);
  162. Type
  163. TMode = (quoted,unquoted);
  164. Var F : Text;
  165. Mode : TMode;
  166. old : PresourceString;
  167. C : char;
  168. Col,i : longint;
  169. Procedure Add(Const S : String);
  170. begin
  171. Write(F,S);
  172. Col:=Col+length(s);
  173. end;
  174. begin
  175. If resstrCount=0 then
  176. exit;
  177. FileName:=ForceExtension(lower(FileName),'.rst');
  178. message1 (general_i_writingresourcefile,filename);
  179. Assign(F,Filename);
  180. {$i-}
  181. Rewrite(f);
  182. {$i+}
  183. If IOresult<>0 then
  184. begin
  185. message(general_e_errorwritingresourcefile);
  186. exit;
  187. end;
  188. While ResourceListRoot<>Nil do
  189. With ResourceListRoot^ do
  190. begin
  191. writeln(f);
  192. Writeln (f,'# hash value = ',hash);
  193. col:=0;
  194. Add(Name+'=');
  195. Mode:=unquoted;
  196. For I:=0 to Len-1 do
  197. begin
  198. C:=Value[i];
  199. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  200. begin
  201. If mode=Quoted then
  202. Add(c)
  203. else
  204. begin
  205. Add(''''+c);
  206. mode:=quoted
  207. end
  208. end
  209. else
  210. begin
  211. If Mode=quoted then
  212. begin
  213. Add('''');
  214. mode:=unquoted;
  215. end;
  216. Add('#'+tostr(ord(c)));
  217. end;
  218. If Col>72 then
  219. begin
  220. if mode=quoted then
  221. Write (F,'''');
  222. Writeln(F,'+');
  223. Col:=0;
  224. Mode:=unQuoted;
  225. end;
  226. end;
  227. if mode=quoted then writeln (f,'''');
  228. Writeln(f);
  229. Old :=ResourceListRoot;
  230. ResourceListRoot:=old^.Next;
  231. FreeMem(Old^.Value,Len);
  232. Dispose(Old);
  233. end;
  234. close(f);
  235. end;
  236. end.
  237. {
  238. $Log$
  239. Revision 1.9 1999-08-15 21:57:59 michael
  240. Changes for resource strings
  241. Revision 1.8 1999/07/29 20:54:01 peter
  242. * write .size also
  243. Revision 1.7 1999/07/26 09:42:00 florian
  244. * bugs 494-496 fixed
  245. Revision 1.6 1999/07/25 19:27:15 michael
  246. + Fixed hash computing, now compatible with gnu .mo file
  247. Revision 1.5 1999/07/24 18:35:41 michael
  248. * Forgot to add unitname to resourcestring data
  249. Revision 1.4 1999/07/24 16:22:10 michael
  250. + Improved resourcestring handling
  251. Revision 1.3 1999/07/24 15:12:58 michael
  252. changes for resourcestrings
  253. Revision 1.2 1999/07/22 20:04:58 michael
  254. + Added computehashvalue
  255. Revision 1.1 1999/07/22 09:34:04 florian
  256. + initial revision
  257. }