cresstr.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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:=len;
  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')));
  70. end;
  71. Procedure AppendToResourceList(const name : string;p : pchar;len,hash : longint);
  72. Var R : PResourceString;
  73. begin
  74. inc(resstrcount);
  75. New(R);
  76. R^.Name:=NAme;
  77. r^.Len:=Len;
  78. R^.Hash:=hash;
  79. GetMem(R^.Value,Len);
  80. Move(P^,R^.Value^,Len);
  81. R^.Next:=ResourceListRoot;
  82. ResourceListRoot:=R;
  83. end;
  84. procedure registerresourcestring(const name : string;p : pchar;len,hash : longint);
  85. var
  86. l1 : pasmlabel;
  87. s : pchar;
  88. begin
  89. { we don't need to generate consts in units }
  90. if (main_module^.is_unit) then
  91. exit;
  92. if not(assigned(resourcestringlist)) then
  93. resourcestringlist:=new(paasmoutput,init);
  94. AppendToResourceList(Name,P,Len,Hash);
  95. { an empty ansi string is nil! }
  96. if (p=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. { first write the maximum size }
  103. consts^.concat(new(pai_const,init_32bit(len)));
  104. { second write the real length }
  105. consts^.concat(new(pai_const,init_32bit(len)));
  106. { redondent with maxlength but who knows ... (PM) }
  107. { third write use count (set to -1 for safety ) }
  108. consts^.concat(new(pai_const,init_32bit(-1)));
  109. consts^.concat(new(pai_label,init(l1)));
  110. getmem(s,len+1);
  111. move(p^,s^,len);
  112. s[len]:=#0;
  113. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  114. consts^.concat(new(pai_const,init_8bit(0)));
  115. end;
  116. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  117. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  118. end;
  119. Procedure WriteResourceFile(Filename : String);
  120. Type
  121. TMode = (quoted,unquoted);
  122. Var F : Text;
  123. Mode : TMode;
  124. old : PresourceString;
  125. C : char;
  126. Col,i : longint;
  127. Procedure Add(Const S : String);
  128. begin
  129. Write(F,S);
  130. Col:=Col+length(s);
  131. end;
  132. begin
  133. If resstrCount=0 then
  134. exit;
  135. FileName:=ForceExtension(lower(FileName),'.rst');
  136. message1 (general_i_writingresourcefile,filename);
  137. Assign(F,Filename);
  138. {$i-}
  139. Rewrite(f);
  140. {$i+}
  141. If IOresult<>0 then
  142. begin
  143. message(general_e_errorwritingresourcefile);
  144. exit;
  145. end;
  146. While ResourceListRoot<>Nil do
  147. With ResourceListRoot^ do
  148. begin
  149. writeln(f);
  150. Writeln (f,'# hash value = ',hash);
  151. col:=0;
  152. Add(Name+'=');
  153. Mode:=unquoted;
  154. For I:=0 to Len-1 do
  155. begin
  156. C:=Value[i];
  157. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  158. begin
  159. If mode=Quoted then
  160. Add(c)
  161. else
  162. begin
  163. Add(''''+c);
  164. mode:=quoted
  165. end
  166. end
  167. else
  168. begin
  169. If Mode=quoted then
  170. begin
  171. Add('''');
  172. mode:=unquoted;
  173. end;
  174. Add('#'+tostr(ord(c)));
  175. end;
  176. If Col>72 then
  177. begin
  178. if mode=quoted then
  179. Write (F,'''');
  180. Writeln(F,'+');
  181. Col:=0;
  182. Mode:=unQuoted;
  183. end;
  184. end;
  185. if mode=quoted then writeln (f,'''');
  186. Writeln(f);
  187. Old :=ResourceListRoot;
  188. ResourceListRoot:=old^.Next;
  189. FreeMem(Old^.Value,Len);
  190. Dispose(Old);
  191. end;
  192. close(f);
  193. end;
  194. end.
  195. {
  196. $Log$
  197. Revision 1.4 1999-07-24 16:22:10 michael
  198. + Improved resourcestring handling
  199. Revision 1.3 1999/07/24 15:12:58 michael
  200. changes for resourcestrings
  201. Revision 1.2 1999/07/22 20:04:58 michael
  202. + Added computehashvalue
  203. Revision 1.1 1999/07/22 09:34:04 florian
  204. + initial revision
  205. }