cresstr.pas 9.5 KB

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