cresstr.pas 9.2 KB

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