cresstr.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. procedure registerresourcestring(Const name : string;p : pchar;len,hash : longint);
  22. function calc_resstring_hashvalue(p : pchar;len : longint) : longint;
  23. Procedure WriteResourceFile(FileName : String);
  24. implementation
  25. uses
  26. globals,aasm,verbose,files;
  27. Type
  28. PResourcestring = ^TResourceString;
  29. TResourceString = record
  30. Name : String;
  31. Value : Pchar;
  32. Len,hash : longint;
  33. Next : PResourcestring;
  34. end;
  35. const
  36. { we can use a static constant because we compile a program only once }
  37. { per compiler call }
  38. resstrcount : longint = 0;
  39. resourcefilename = 'resource.rst';
  40. Var
  41. ResourceListRoot : PResourceString;
  42. { calcs the hash value for a give resourcestring, len is }
  43. { necessary because the resourcestring can contain #0 }
  44. function calc_resstring_hashvalue(p : pchar;len : longint) : longint;
  45. Var hash,g,I : longint;
  46. begin
  47. hash:=0;
  48. For I:=0 to Len-1 do { 0 terminated }
  49. begin
  50. hash:=hash shl 4;
  51. inc(Hash,Ord(p[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 insertresourcestrings;
  65. begin
  66. if not(assigned(resourcestringlist)) then
  67. resourcestringlist:=new(paasmoutput,init);
  68. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  69. resourcestringlist^.insert(new(pai_symbol,initname_global('RESOURCESTRINGLIST',0)));
  70. resourcestringlist^.concat(new(pai_symbol_end,initname('RESOURCESTRINGLIST')));
  71. end;
  72. Procedure AppendToResourceList(const name : string;p : pchar;len,hash : longint);
  73. Var R : PResourceString;
  74. begin
  75. inc(resstrcount);
  76. New(R);
  77. R^.Name:=Lower(Name);
  78. r^.Len:=Len;
  79. R^.Hash:=hash;
  80. GetMem(R^.Value,Len);
  81. Move(P^,R^.Value^,Len);
  82. R^.Next:=ResourceListRoot;
  83. ResourceListRoot:=R;
  84. end;
  85. procedure registerresourcestring(const name : string;p : pchar;len,hash : longint);
  86. var
  87. l1 : pasmlabel;
  88. s : pchar;
  89. begin
  90. { we don't need to generate consts in units }
  91. if (main_module^.is_unit) then
  92. exit;
  93. if not(assigned(resourcestringlist)) then
  94. resourcestringlist:=new(paasmoutput,init);
  95. AppendToResourceList(current_module^.modulename^+'.'+Name,P,Len,Hash);
  96. { an empty ansi string is nil! }
  97. if (p=nil) or (len=0) then
  98. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  99. else
  100. begin
  101. getdatalabel(l1);
  102. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  103. { first write the maximum size }
  104. consts^.concat(new(pai_const,init_32bit(len)));
  105. { second write the real length }
  106. consts^.concat(new(pai_const,init_32bit(len)));
  107. { redondent with maxlength but who knows ... (PM) }
  108. { third write use count (set to -1 for safety ) }
  109. consts^.concat(new(pai_const,init_32bit(-1)));
  110. consts^.concat(new(pai_label,init(l1)));
  111. getmem(s,len+1);
  112. move(p^,s^,len);
  113. s[len]:=#0;
  114. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  115. consts^.concat(new(pai_const,init_8bit(0)));
  116. end;
  117. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  118. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  119. end;
  120. Procedure WriteResourceFile(Filename : String);
  121. Type
  122. TMode = (quoted,unquoted);
  123. Var F : Text;
  124. Mode : TMode;
  125. old : PresourceString;
  126. C : char;
  127. Col,i : longint;
  128. Procedure Add(Const S : String);
  129. begin
  130. Write(F,S);
  131. Col:=Col+length(s);
  132. end;
  133. begin
  134. If resstrCount=0 then
  135. exit;
  136. FileName:=ForceExtension(lower(FileName),'.rst');
  137. message1 (general_i_writingresourcefile,filename);
  138. Assign(F,Filename);
  139. {$i-}
  140. Rewrite(f);
  141. {$i+}
  142. If IOresult<>0 then
  143. begin
  144. message(general_e_errorwritingresourcefile);
  145. exit;
  146. end;
  147. While ResourceListRoot<>Nil do
  148. With ResourceListRoot^ do
  149. begin
  150. writeln(f);
  151. Writeln (f,'# hash value = ',hash);
  152. col:=0;
  153. Add(Name+'=');
  154. Mode:=unquoted;
  155. For I:=0 to Len-1 do
  156. begin
  157. C:=Value[i];
  158. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  159. begin
  160. If mode=Quoted then
  161. Add(c)
  162. else
  163. begin
  164. Add(''''+c);
  165. mode:=quoted
  166. end
  167. end
  168. else
  169. begin
  170. If Mode=quoted then
  171. begin
  172. Add('''');
  173. mode:=unquoted;
  174. end;
  175. Add('#'+tostr(ord(c)));
  176. end;
  177. If Col>72 then
  178. begin
  179. if mode=quoted then
  180. Write (F,'''');
  181. Writeln(F,'+');
  182. Col:=0;
  183. Mode:=unQuoted;
  184. end;
  185. end;
  186. if mode=quoted then writeln (f,'''');
  187. Writeln(f);
  188. Old :=ResourceListRoot;
  189. ResourceListRoot:=old^.Next;
  190. FreeMem(Old^.Value,Len);
  191. Dispose(Old);
  192. end;
  193. close(f);
  194. end;
  195. end.
  196. {
  197. $Log$
  198. Revision 1.8 1999-07-29 20:54:01 peter
  199. * write .size also
  200. Revision 1.7 1999/07/26 09:42:00 florian
  201. * bugs 494-496 fixed
  202. Revision 1.6 1999/07/25 19:27:15 michael
  203. + Fixed hash computing, now compatible with gnu .mo file
  204. Revision 1.5 1999/07/24 18:35:41 michael
  205. * Forgot to add unitname to resourcestring data
  206. Revision 1.4 1999/07/24 16:22:10 michael
  207. + Improved resourcestring handling
  208. Revision 1.3 1999/07/24 15:12:58 michael
  209. changes for resourcestrings
  210. Revision 1.2 1999/07/22 20:04:58 michael
  211. + Added computehashvalue
  212. Revision 1.1 1999/07/22 09:34:04 florian
  213. + initial revision
  214. }