cresstr.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects;
  23. Type
  24. { These are used to form a singly-linked list, ordered by hash value }
  25. PResourceStringItem = ^TResourceStringItem;
  26. TResourceStringItem = object(TLinkedList_Item)
  27. Name : String;
  28. Value : Pchar;
  29. Len,
  30. hash : longint;
  31. constructor Init(const AName:string;AValue:pchar;ALen:longint);
  32. destructor Done;virtual;
  33. procedure CalcHash;
  34. end;
  35. PResourceStrings=^TResourceStrings;
  36. TResourceStrings=object
  37. private
  38. List : TLinkedList;
  39. public
  40. ResStrCount : longint;
  41. constructor Init;
  42. destructor Done;
  43. function Register(Const name : string;p : pchar;len : longint) : longint;
  44. procedure CreateResourceStringList;
  45. Procedure WriteResourceFile(FileName : String);
  46. end;
  47. var
  48. ResourceStrings : PResourceStrings;
  49. implementation
  50. uses
  51. cutils,globals,aasm,verbose,fmodule;
  52. { ---------------------------------------------------------------------
  53. Calculate hash value, based on the string
  54. ---------------------------------------------------------------------}
  55. { ---------------------------------------------------------------------
  56. TRESOURCESTRING_ITEM
  57. ---------------------------------------------------------------------}
  58. constructor TResourceStringItem.Init(const AName:string;AValue:pchar;ALen:longint);
  59. begin
  60. inherited Init;
  61. Name:=AName;
  62. Len:=ALen;
  63. GetMem(Value,Len);
  64. Move(AValue^,Value^,Len);
  65. CalcHash;
  66. end;
  67. destructor TResourceStringItem.Done;
  68. begin
  69. FreeMem(Value,Len);
  70. end;
  71. {$ifopt r+}
  72. {$define rangeon}
  73. {$r-}
  74. {$endif}
  75. procedure TResourceStringItem.CalcHash;
  76. Var
  77. g,I : longint;
  78. begin
  79. hash:=0;
  80. For I:=0 to Len-1 do { 0 terminated }
  81. begin
  82. hash:=hash shl 4;
  83. inc(Hash,Ord(Value[i]));
  84. g:=hash and ($f shl 28);
  85. if g<>0 then
  86. begin
  87. hash:=hash xor (g shr 24);
  88. hash:=hash xor g;
  89. end;
  90. end;
  91. If Hash=0 then
  92. Hash:=Not(0);
  93. end;
  94. {$ifdef rangeon}
  95. {$r+}
  96. {$undef rangeon}
  97. {$endif}
  98. { ---------------------------------------------------------------------
  99. TRESOURCESTRINGS
  100. ---------------------------------------------------------------------}
  101. Constructor TResourceStrings.Init;
  102. begin
  103. List.Init;
  104. ResStrCount:=0;
  105. end;
  106. Destructor TResourceStrings.Done;
  107. begin
  108. List.Done;
  109. end;
  110. { ---------------------------------------------------------------------
  111. Create the full asmlist for resourcestrings.
  112. ---------------------------------------------------------------------}
  113. procedure TResourceStrings.CreateResourceStringList;
  114. Procedure AppendToAsmResList (P : PResourceStringItem);
  115. Var
  116. l1 : pasmlabel;
  117. s : pchar;
  118. l : longint;
  119. begin
  120. With P^ Do
  121. begin
  122. if (Value=nil) or (len=0) then
  123. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  124. else
  125. begin
  126. getdatalabel(l1);
  127. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  128. consts^.concat(new(pai_const,init_32bit(len)));
  129. consts^.concat(new(pai_const,init_32bit(len)));
  130. consts^.concat(new(pai_const,init_32bit(-1)));
  131. consts^.concat(new(pai_label,init(l1)));
  132. getmem(s,len+1);
  133. move(Value^,s^,len);
  134. s[len]:=#0;
  135. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  136. consts^.concat(new(pai_const,init_8bit(0)));
  137. end;
  138. { append Current value (nil) and hash...}
  139. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  140. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  141. { Append the name as a ansistring. }
  142. getdatalabel(l1);
  143. L:=Length(Name);
  144. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  145. consts^.concat(new(pai_const,init_32bit(l)));
  146. consts^.concat(new(pai_const,init_32bit(l)));
  147. consts^.concat(new(pai_const,init_32bit(-1)));
  148. consts^.concat(new(pai_label,init(l1)));
  149. getmem(s,l+1);
  150. move(Name[1],s^,l);
  151. s[l]:=#0;
  152. consts^.concat(new(pai_string,init_length_pchar(s,l)));
  153. consts^.concat(new(pai_const,init_8bit(0)));
  154. end;
  155. end;
  156. Var
  157. R : PresourceStringItem;
  158. begin
  159. if not(assigned(resourcestringlist)) then
  160. resourcestringlist:=new(paasmoutput,init);
  161. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  162. resourcestringlist^.insert(new(pai_symbol,initdataname_global(current_module^.modulename^+'_'+'RESOURCESTRINGLIST',0)));
  163. R:=PResourceStringItem(List.First);
  164. While assigned(R) do
  165. begin
  166. AppendToAsmResList(R);
  167. R:=PResourceStringItem(R^.Next);
  168. end;
  169. resourcestringlist^.concat(new(pai_symbol_end,initname(current_module^.modulename^+'_'+'RESOURCESTRINGLIST')));
  170. end;
  171. { ---------------------------------------------------------------------
  172. Insert 1 resource string in all tables.
  173. ---------------------------------------------------------------------}
  174. function TResourceStrings.Register(const name : string;p : pchar;len : longint) : longint;
  175. begin
  176. List.Concat(new(PResourceStringItem,Init(lower(current_module^.modulename^+'.'+Name),p,len)));
  177. Register:=ResStrCount;
  178. inc(ResStrCount);
  179. end;
  180. Procedure TResourceStrings.WriteResourceFile(Filename : String);
  181. Type
  182. TMode = (quoted,unquoted);
  183. Var
  184. F : Text;
  185. Mode : TMode;
  186. R : PResourceStringItem;
  187. C : char;
  188. Col,i : longint;
  189. Procedure Add(Const S : String);
  190. begin
  191. Write(F,S);
  192. Col:=Col+length(s);
  193. end;
  194. begin
  195. If List.Empty then
  196. exit;
  197. FileName:=current_module^.outputpath^+FixFileName(ForceExtension(FileName,'.rst'));
  198. message1 (general_i_writingresourcefile,filename);
  199. Assign(F,Filename);
  200. {$i-}
  201. Rewrite(f);
  202. {$i+}
  203. If IOresult<>0 then
  204. begin
  205. message(general_e_errorwritingresourcefile);
  206. exit;
  207. end;
  208. R:=PResourceStringItem(List.First);
  209. While assigned(R) do
  210. begin
  211. writeln(f);
  212. Writeln(f,'# hash value = ',R^.hash);
  213. col:=0;
  214. Add(R^.Name+'=');
  215. Mode:=unquoted;
  216. For I:=0 to R^.Len-1 do
  217. begin
  218. C:=R^.Value[i];
  219. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  220. begin
  221. If mode=Quoted then
  222. Add(c)
  223. else
  224. begin
  225. Add(''''+c);
  226. mode:=quoted
  227. end;
  228. end
  229. else
  230. begin
  231. If Mode=quoted then
  232. begin
  233. Add('''');
  234. mode:=unquoted;
  235. end;
  236. Add('#'+tostr(ord(c)));
  237. end;
  238. If Col>72 then
  239. begin
  240. if mode=quoted then
  241. Write (F,'''');
  242. Writeln(F,'+');
  243. Col:=0;
  244. Mode:=unQuoted;
  245. end;
  246. end;
  247. if mode=quoted then
  248. writeln (f,'''');
  249. Writeln(f);
  250. R:=PResourceStringItem(R^.Next);
  251. end;
  252. close(f);
  253. end;
  254. end.
  255. {
  256. $Log$
  257. Revision 1.7 2000-11-13 14:44:35 jonas
  258. * fixes so no more range errors with improved range checking code
  259. Revision 1.6 2000/09/24 15:06:14 peter
  260. * use defines.inc
  261. Revision 1.5 2000/08/27 16:11:50 peter
  262. * moved some util functions from globals,cobjects to cutils
  263. * splitted files into finput,fmodule
  264. Revision 1.4 2000/08/15 09:45:29 michael
  265. + Merged changes in fixbranch
  266. Revision 1.1.2.1 2000/08/15 09:41:56 michael
  267. + Fix to write rst file in output directory of module
  268. Revision 1.1 2000/07/13 06:29:48 michael
  269. + Initial import
  270. }