cresstr.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Michael van Canneyt
  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. uses
  21. cobjects;
  22. Type
  23. { These are used to form a singly-linked list, ordered by hash value }
  24. PResourceStringItem = ^TResourceStringItem;
  25. TResourceStringItem = object(TLinkedList_Item)
  26. Name : String;
  27. Value : Pchar;
  28. Len,
  29. hash : longint;
  30. constructor Init(const AName:string;AValue:pchar;ALen:longint);
  31. destructor Done;virtual;
  32. procedure CalcHash;
  33. end;
  34. PResourceStrings=^TResourceStrings;
  35. TResourceStrings=object
  36. private
  37. List : TLinkedList;
  38. public
  39. ResStrCount : longint;
  40. constructor Init;
  41. destructor Done;
  42. function Register(Const name : string;p : pchar;len : longint) : longint;
  43. procedure CreateResourceStringList;
  44. Procedure WriteResourceFile(FileName : String);
  45. end;
  46. var
  47. ResourceStrings : PResourceStrings;
  48. implementation
  49. uses
  50. globals,aasm,verbose,files;
  51. { ---------------------------------------------------------------------
  52. Calculate hash value, based on the string
  53. ---------------------------------------------------------------------}
  54. { ---------------------------------------------------------------------
  55. TRESOURCESTRING_ITEM
  56. ---------------------------------------------------------------------}
  57. constructor TResourceStringItem.Init(const AName:string;AValue:pchar;ALen:longint);
  58. begin
  59. inherited Init;
  60. Name:=AName;
  61. Len:=ALen;
  62. GetMem(Value,Len);
  63. Move(AValue^,Value^,Len);
  64. CalcHash;
  65. end;
  66. destructor TResourceStringItem.Done;
  67. begin
  68. FreeMem(Value,Len);
  69. end;
  70. procedure TResourceStringItem.CalcHash;
  71. Var
  72. g,I : longint;
  73. begin
  74. hash:=0;
  75. For I:=0 to Len-1 do { 0 terminated }
  76. begin
  77. hash:=hash shl 4;
  78. inc(Hash,Ord(Value[i]));
  79. g:=hash and ($f shl 28);
  80. if g<>0 then
  81. begin
  82. hash:=hash xor (g shr 24);
  83. hash:=hash xor g;
  84. end;
  85. end;
  86. If Hash=0 then
  87. Hash:=Not(0);
  88. end;
  89. { ---------------------------------------------------------------------
  90. TRESOURCESTRINGS
  91. ---------------------------------------------------------------------}
  92. Constructor TResourceStrings.Init;
  93. begin
  94. List.Init;
  95. ResStrCount:=0;
  96. end;
  97. Destructor TResourceStrings.Done;
  98. begin
  99. List.Done;
  100. end;
  101. { ---------------------------------------------------------------------
  102. Create the full asmlist for resourcestrings.
  103. ---------------------------------------------------------------------}
  104. procedure TResourceStrings.CreateResourceStringList;
  105. Procedure AppendToAsmResList (P : PResourceStringItem);
  106. Var
  107. l1 : pasmlabel;
  108. s : pchar;
  109. l : longint;
  110. begin
  111. With P^ Do
  112. begin
  113. if (Value=nil) or (len=0) then
  114. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  115. else
  116. begin
  117. getdatalabel(l1);
  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(Value^,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. { append Current value (nil) and hash...}
  130. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  131. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  132. { Append the name as a ansistring. }
  133. getdatalabel(l1);
  134. L:=Length(Name);
  135. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  136. consts^.concat(new(pai_const,init_32bit(l)));
  137. consts^.concat(new(pai_const,init_32bit(l)));
  138. consts^.concat(new(pai_const,init_32bit(-1)));
  139. consts^.concat(new(pai_label,init(l1)));
  140. getmem(s,l+1);
  141. move(Name[1],s^,l);
  142. s[l]:=#0;
  143. consts^.concat(new(pai_string,init_length_pchar(s,l)));
  144. consts^.concat(new(pai_const,init_8bit(0)));
  145. end;
  146. end;
  147. Var
  148. R : PresourceStringItem;
  149. begin
  150. if not(assigned(resourcestringlist)) then
  151. resourcestringlist:=new(paasmoutput,init);
  152. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  153. resourcestringlist^.insert(new(pai_symbol,initname_global(current_module^.modulename^+'_'+'RESOURCESTRINGLIST',0)));
  154. R:=PResourceStringItem(List.First);
  155. While assigned(R) do
  156. begin
  157. AppendToAsmResList(R);
  158. R:=PResourceStringItem(R^.Next);
  159. end;
  160. resourcestringlist^.concat(new(pai_symbol_end,initname(current_module^.modulename^+'_'+'RESOURCESTRINGLIST')));
  161. end;
  162. { ---------------------------------------------------------------------
  163. Insert 1 resource string in all tables.
  164. ---------------------------------------------------------------------}
  165. function TResourceStrings.Register(const name : string;p : pchar;len : longint) : longint;
  166. begin
  167. List.Concat(new(PResourceStringItem,Init(lower(current_module^.modulename^+'.'+Name),p,len)));
  168. Register:=ResStrCount;
  169. inc(ResStrCount);
  170. end;
  171. Procedure TResourceStrings.WriteResourceFile(Filename : String);
  172. Type
  173. TMode = (quoted,unquoted);
  174. Var
  175. F : Text;
  176. Mode : TMode;
  177. R : PResourceStringItem;
  178. C : char;
  179. Col,i : longint;
  180. Procedure Add(Const S : String);
  181. begin
  182. Write(F,S);
  183. Col:=Col+length(s);
  184. end;
  185. begin
  186. If List.Empty then
  187. exit;
  188. FileName:=ForceExtension(lower(FileName),'.rst');
  189. message1 (general_i_writingresourcefile,filename);
  190. Assign(F,Filename);
  191. {$i-}
  192. Rewrite(f);
  193. {$i+}
  194. If IOresult<>0 then
  195. begin
  196. message(general_e_errorwritingresourcefile);
  197. exit;
  198. end;
  199. R:=PResourceStringItem(List.First);
  200. While assigned(R) do
  201. begin
  202. writeln(f);
  203. Writeln(f,'# hash value = ',R^.hash);
  204. col:=0;
  205. Add(R^.Name+'=');
  206. Mode:=unquoted;
  207. For I:=0 to R^.Len-1 do
  208. begin
  209. C:=R^.Value[i];
  210. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  211. begin
  212. If mode=Quoted then
  213. Add(c)
  214. else
  215. begin
  216. Add(''''+c);
  217. mode:=quoted
  218. end;
  219. end
  220. else
  221. begin
  222. If Mode=quoted then
  223. begin
  224. Add('''');
  225. mode:=unquoted;
  226. end;
  227. Add('#'+tostr(ord(c)));
  228. end;
  229. If Col>72 then
  230. begin
  231. if mode=quoted then
  232. Write (F,'''');
  233. Writeln(F,'+');
  234. Col:=0;
  235. Mode:=unQuoted;
  236. end;
  237. end;
  238. if mode=quoted then
  239. writeln (f,'''');
  240. Writeln(f);
  241. R:=PResourceStringItem(R^.Next);
  242. end;
  243. close(f);
  244. end;
  245. end.
  246. {
  247. $Log$
  248. Revision 1.18 2000-06-04 08:48:54 jonas
  249. * resource string numbering
  250. Revision 1.17 2000/06/01 19:09:57 peter
  251. * made resourcestrings OOP so it's easier to handle it per module
  252. Revision 1.16 2000/01/07 01:14:23 peter
  253. * updated copyright to 2000
  254. Revision 1.15 1999/11/06 14:34:20 peter
  255. * truncated log to 20 revs
  256. Revision 1.14 1999/08/27 15:55:36 michael
  257. * Fixed small bug: next field in resourcelist was not initialized
  258. Revision 1.13 1999/08/26 20:24:39 michael
  259. + Hopefuly last fixes for resourcestrings
  260. Revision 1.12 1999/08/25 16:41:07 peter
  261. * resources are working again
  262. Revision 1.11 1999/08/23 11:48:23 michael
  263. * resourcestrings ams list needs unitname prepended
  264. Revision 1.10 1999/08/23 11:45:41 michael
  265. * Hopefully final attempt at resourcestrings
  266. Revision 1.9 1999/08/15 21:57:59 michael
  267. Changes for resource strings
  268. Revision 1.8 1999/07/29 20:54:01 peter
  269. * write .size also
  270. Revision 1.7 1999/07/26 09:42:00 florian
  271. * bugs 494-496 fixed
  272. Revision 1.6 1999/07/25 19:27:15 michael
  273. + Fixed hash computing, now compatible with gnu .mo file
  274. Revision 1.5 1999/07/24 18:35:41 michael
  275. * Forgot to add unitname to resourcestring data
  276. Revision 1.4 1999/07/24 16:22:10 michael
  277. + Improved resourcestring handling
  278. Revision 1.3 1999/07/24 15:12:58 michael
  279. changes for resourcestrings
  280. Revision 1.2 1999/07/22 20:04:58 michael
  281. + Added computehashvalue
  282. Revision 1.1 1999/07/22 09:34:04 florian
  283. + initial revision
  284. }