cresstr.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. procedure TResourceStringItem.CalcHash;
  72. Var
  73. g,I : longint;
  74. begin
  75. hash:=0;
  76. For I:=0 to Len-1 do { 0 terminated }
  77. begin
  78. hash:=hash shl 4;
  79. inc(Hash,Ord(Value[i]));
  80. g:=hash and ($f shl 28);
  81. if g<>0 then
  82. begin
  83. hash:=hash xor (g shr 24);
  84. hash:=hash xor g;
  85. end;
  86. end;
  87. If Hash=0 then
  88. Hash:=Not(0);
  89. end;
  90. { ---------------------------------------------------------------------
  91. TRESOURCESTRINGS
  92. ---------------------------------------------------------------------}
  93. Constructor TResourceStrings.Init;
  94. begin
  95. List.Init;
  96. ResStrCount:=0;
  97. end;
  98. Destructor TResourceStrings.Done;
  99. begin
  100. List.Done;
  101. end;
  102. { ---------------------------------------------------------------------
  103. Create the full asmlist for resourcestrings.
  104. ---------------------------------------------------------------------}
  105. procedure TResourceStrings.CreateResourceStringList;
  106. Procedure AppendToAsmResList (P : PResourceStringItem);
  107. Var
  108. l1 : pasmlabel;
  109. s : pchar;
  110. l : longint;
  111. begin
  112. With P^ Do
  113. begin
  114. if (Value=nil) or (len=0) then
  115. resourcestringlist^.concat(new(pai_const,init_32bit(0)))
  116. else
  117. begin
  118. getdatalabel(l1);
  119. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  120. consts^.concat(new(pai_const,init_32bit(len)));
  121. consts^.concat(new(pai_const,init_32bit(len)));
  122. consts^.concat(new(pai_const,init_32bit(-1)));
  123. consts^.concat(new(pai_label,init(l1)));
  124. getmem(s,len+1);
  125. move(Value^,s^,len);
  126. s[len]:=#0;
  127. consts^.concat(new(pai_string,init_length_pchar(s,len)));
  128. consts^.concat(new(pai_const,init_8bit(0)));
  129. end;
  130. { append Current value (nil) and hash...}
  131. resourcestringlist^.concat(new(pai_const,init_32bit(0)));
  132. resourcestringlist^.concat(new(pai_const,init_32bit(hash)));
  133. { Append the name as a ansistring. }
  134. getdatalabel(l1);
  135. L:=Length(Name);
  136. resourcestringlist^.concat(new(pai_const_symbol,init(l1)));
  137. consts^.concat(new(pai_const,init_32bit(l)));
  138. consts^.concat(new(pai_const,init_32bit(l)));
  139. consts^.concat(new(pai_const,init_32bit(-1)));
  140. consts^.concat(new(pai_label,init(l1)));
  141. getmem(s,l+1);
  142. move(Name[1],s^,l);
  143. s[l]:=#0;
  144. consts^.concat(new(pai_string,init_length_pchar(s,l)));
  145. consts^.concat(new(pai_const,init_8bit(0)));
  146. end;
  147. end;
  148. Var
  149. R : PresourceStringItem;
  150. begin
  151. if not(assigned(resourcestringlist)) then
  152. resourcestringlist:=new(paasmoutput,init);
  153. resourcestringlist^.insert(new(pai_const,init_32bit(resstrcount)));
  154. resourcestringlist^.insert(new(pai_symbol,initdataname_global(current_module^.modulename^+'_'+'RESOURCESTRINGLIST',0)));
  155. R:=PResourceStringItem(List.First);
  156. While assigned(R) do
  157. begin
  158. AppendToAsmResList(R);
  159. R:=PResourceStringItem(R^.Next);
  160. end;
  161. resourcestringlist^.concat(new(pai_symbol_end,initname(current_module^.modulename^+'_'+'RESOURCESTRINGLIST')));
  162. end;
  163. { ---------------------------------------------------------------------
  164. Insert 1 resource string in all tables.
  165. ---------------------------------------------------------------------}
  166. function TResourceStrings.Register(const name : string;p : pchar;len : longint) : longint;
  167. begin
  168. List.Concat(new(PResourceStringItem,Init(lower(current_module^.modulename^+'.'+Name),p,len)));
  169. Register:=ResStrCount;
  170. inc(ResStrCount);
  171. end;
  172. Procedure TResourceStrings.WriteResourceFile(Filename : String);
  173. Type
  174. TMode = (quoted,unquoted);
  175. Var
  176. F : Text;
  177. Mode : TMode;
  178. R : PResourceStringItem;
  179. C : char;
  180. Col,i : longint;
  181. Procedure Add(Const S : String);
  182. begin
  183. Write(F,S);
  184. Col:=Col+length(s);
  185. end;
  186. begin
  187. If List.Empty then
  188. exit;
  189. FileName:=current_module^.outputpath^+FixFileName(ForceExtension(FileName,'.rst'));
  190. message1 (general_i_writingresourcefile,filename);
  191. Assign(F,Filename);
  192. {$i-}
  193. Rewrite(f);
  194. {$i+}
  195. If IOresult<>0 then
  196. begin
  197. message(general_e_errorwritingresourcefile);
  198. exit;
  199. end;
  200. R:=PResourceStringItem(List.First);
  201. While assigned(R) do
  202. begin
  203. writeln(f);
  204. Writeln(f,'# hash value = ',R^.hash);
  205. col:=0;
  206. Add(R^.Name+'=');
  207. Mode:=unquoted;
  208. For I:=0 to R^.Len-1 do
  209. begin
  210. C:=R^.Value[i];
  211. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  212. begin
  213. If mode=Quoted then
  214. Add(c)
  215. else
  216. begin
  217. Add(''''+c);
  218. mode:=quoted
  219. end;
  220. end
  221. else
  222. begin
  223. If Mode=quoted then
  224. begin
  225. Add('''');
  226. mode:=unquoted;
  227. end;
  228. Add('#'+tostr(ord(c)));
  229. end;
  230. If Col>72 then
  231. begin
  232. if mode=quoted then
  233. Write (F,'''');
  234. Writeln(F,'+');
  235. Col:=0;
  236. Mode:=unQuoted;
  237. end;
  238. end;
  239. if mode=quoted then
  240. writeln (f,'''');
  241. Writeln(f);
  242. R:=PResourceStringItem(R^.Next);
  243. end;
  244. close(f);
  245. end;
  246. end.
  247. {
  248. $Log$
  249. Revision 1.6 2000-09-24 15:06:14 peter
  250. * use defines.inc
  251. Revision 1.5 2000/08/27 16:11:50 peter
  252. * moved some util functions from globals,cobjects to cutils
  253. * splitted files into finput,fmodule
  254. Revision 1.4 2000/08/15 09:45:29 michael
  255. + Merged changes in fixbranch
  256. Revision 1.1.2.1 2000/08/15 09:41:56 michael
  257. + Fix to write rst file in output directory of module
  258. Revision 1.1 2000/07/13 06:29:48 michael
  259. + Initial import
  260. }