cresstr.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. {
  2. Copyright (c) 1998-2002 by Michael van Canneyt
  3. Handles resourcestrings
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cresstr;
  18. {$i fpcdefs.inc}
  19. interface
  20. Procedure GenerateResourceStrings;
  21. implementation
  22. uses
  23. cclasses,
  24. cutils,globtype,globals,systems,
  25. symconst,symtype,symdef,symsym,
  26. verbose,fmodule,ppu,
  27. aasmbase,aasmtai,aasmdata,
  28. aasmcpu;
  29. Type
  30. { These are used to form a singly-linked list, ordered by hash value }
  31. TResourceStringItem = class(TLinkedListItem)
  32. Sym : TConstSym;
  33. Name : String;
  34. Value : Pchar;
  35. Len : Longint;
  36. hash : Cardinal;
  37. constructor Create(asym:TConstsym);
  38. destructor Destroy;override;
  39. procedure CalcHash;
  40. end;
  41. Tresourcestrings=class
  42. private
  43. List : TLinkedList;
  44. procedure ConstSym_Register(p:tnamedindexitem;arg:pointer);
  45. public
  46. constructor Create;
  47. destructor Destroy;override;
  48. procedure CreateResourceStringData;
  49. Procedure WriteResourceFile;
  50. procedure RegisterResourceStrings;
  51. end;
  52. { ---------------------------------------------------------------------
  53. TRESOURCESTRING_ITEM
  54. ---------------------------------------------------------------------}
  55. constructor TResourceStringItem.Create(asym:TConstsym);
  56. begin
  57. inherited Create;
  58. Sym:=Asym;
  59. Name:=lower(asym.owner.name^+'.'+asym.Name);
  60. Len:=asym.value.len;
  61. GetMem(Value,Len);
  62. Move(asym.value.valueptr^,Value^,Len);
  63. CalcHash;
  64. end;
  65. destructor TResourceStringItem.Destroy;
  66. begin
  67. FreeMem(Value);
  68. end;
  69. procedure TResourceStringItem.CalcHash;
  70. Var
  71. g : Cardinal;
  72. 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:=$ffffffff;
  88. end;
  89. { ---------------------------------------------------------------------
  90. Tresourcestrings
  91. ---------------------------------------------------------------------}
  92. Constructor Tresourcestrings.Create;
  93. begin
  94. List:=TLinkedList.Create;
  95. end;
  96. Destructor Tresourcestrings.Destroy;
  97. begin
  98. List.Free;
  99. end;
  100. procedure Tresourcestrings.CreateResourceStringData;
  101. function WriteValueString(p:pchar;len:longint):TasmLabel;
  102. var
  103. s : pchar;
  104. reference: TAsmLabel;
  105. begin
  106. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  107. begin
  108. current_asmdata.getdatalabel(reference);
  109. current_asmdata.asmlists[al_const].concat(tai_label.create(reference));
  110. end;
  111. current_asmdata.getdatalabel(result);
  112. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(aint))));
  113. current_asmdata.asmlists[al_const].concat(tai_const.create_aint(-1));
  114. current_asmdata.asmlists[al_const].concat(tai_const.create_aint(len));
  115. current_asmdata.asmlists[al_const].concat(tai_label.create(result));
  116. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  117. current_asmdata.asmlists[al_const].concat(tai_directive.create(asd_reference,reference.getname));
  118. getmem(s,len+1);
  119. move(p^,s^,len);
  120. s[len]:=#0;
  121. current_asmdata.asmlists[al_const].concat(tai_string.create_pchar(s,len));
  122. current_asmdata.asmlists[al_const].concat(tai_const.create_8bit(0));
  123. end;
  124. Var
  125. namelab,
  126. valuelab : tasmlabel;
  127. resstrlab : tasmsymbol;
  128. R : TResourceStringItem;
  129. begin
  130. { Put resourcestrings in a new objectfile. Putting it in multiple files
  131. makes the linking too dependent on the linker script requiring a SORT(*) for
  132. the data sections }
  133. maybe_new_object_file(current_asmdata.asmlists[al_const]);
  134. maybe_new_object_file(current_asmdata.asmlists[al_resourcestrings]);
  135. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'1_START'),sizeof(aint));
  136. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  137. make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
  138. { Write unitname entry }
  139. namelab:=WriteValueString(@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^));
  140. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
  141. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  142. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  143. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  144. {$ifdef cpu64bit}
  145. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  146. {$endif cpu64bit}
  147. { Add entries }
  148. R:=TResourceStringItem(List.First);
  149. while assigned(R) do
  150. begin
  151. new_section(current_asmdata.asmlists[al_const],sec_rodata,make_mangledname('RESSTR',current_module.localsymtable,'d_'+r.name),sizeof(aint));
  152. { Write default value }
  153. if assigned(R.value) and (R.len<>0) then
  154. valuelab:=WriteValueString(R.Value,R.Len)
  155. else
  156. valuelab:=nil;
  157. { Append the name as a ansistring. }
  158. namelab:=WriteValueString(@R.Name[1],length(R.name));
  159. {
  160. Resourcestring index:
  161. TResourceStringRecord = Packed Record
  162. Name,
  163. CurrentValue,
  164. DefaultValue : AnsiString;
  165. HashValue : LongWord;
  166. end;
  167. }
  168. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(aint));
  169. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  170. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  171. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
  172. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  173. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(valuelab));
  174. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(longint(R.Hash)));
  175. {$ifdef cpu64bit}
  176. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  177. {$endif cpu64bit}
  178. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  179. R:=TResourceStringItem(R.Next);
  180. end;
  181. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(aint));
  182. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  183. make_mangledname('RESSTR',current_module.localsymtable,'END'),AT_DATA,0));
  184. end;
  185. Procedure Tresourcestrings.WriteResourceFile;
  186. Type
  187. TMode = (quoted,unquoted);
  188. Var
  189. F : Text;
  190. Mode : TMode;
  191. R : TResourceStringItem;
  192. C : char;
  193. Col,i : longint;
  194. ResFileName : string;
  195. Procedure Add(Const S : String);
  196. begin
  197. Write(F,S);
  198. inc(Col,length(s));
  199. end;
  200. begin
  201. ResFileName:=ForceExtension(current_module.ppufilename^,'.rst');
  202. message1 (general_i_writingresourcefile,SplitFileName(ResFileName));
  203. Assign(F,ResFileName);
  204. {$i-}
  205. Rewrite(f);
  206. {$i+}
  207. If IOresult<>0 then
  208. begin
  209. message1(general_e_errorwritingresourcefile,ResFileName);
  210. exit;
  211. end;
  212. R:=TResourceStringItem(List.First);
  213. while assigned(R) do
  214. begin
  215. writeln(f);
  216. Writeln(f,'# hash value = ',R.Hash);
  217. col:=0;
  218. Add(R.Name+'=');
  219. Mode:=unquoted;
  220. For I:=0 to R.Len-1 do
  221. begin
  222. C:=R.Value[i];
  223. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  224. begin
  225. If mode=Quoted then
  226. Add(c)
  227. else
  228. begin
  229. Add(''''+c);
  230. mode:=quoted
  231. end;
  232. end
  233. else
  234. begin
  235. If Mode=quoted then
  236. begin
  237. Add('''');
  238. mode:=unquoted;
  239. end;
  240. Add('#'+tostr(ord(c)));
  241. end;
  242. If Col>72 then
  243. begin
  244. if mode=quoted then
  245. Write (F,'''');
  246. Writeln(F,'+');
  247. Col:=0;
  248. Mode:=unQuoted;
  249. end;
  250. end;
  251. if mode=quoted then
  252. writeln (f,'''');
  253. Writeln(f);
  254. R:=TResourceStringItem(R.Next);
  255. end;
  256. close(f);
  257. end;
  258. procedure Tresourcestrings.ConstSym_Register(p:tnamedindexitem;arg:pointer);
  259. begin
  260. if (tsym(p).typ=constsym) and
  261. (tconstsym(p).consttyp=constresourcestring) then
  262. List.Concat(tResourceStringItem.Create(TConstsym(p)));
  263. end;
  264. procedure Tresourcestrings.RegisterResourceStrings;
  265. begin
  266. if assigned(current_module.globalsymtable) then
  267. current_module.globalsymtable.foreach(@ConstSym_Register,nil);
  268. current_module.localsymtable.foreach(@ConstSym_Register,nil);
  269. end;
  270. Procedure GenerateResourceStrings;
  271. var
  272. resstrs : Tresourcestrings;
  273. begin
  274. resstrs:=Tresourcestrings.Create;
  275. resstrs.RegisterResourceStrings;
  276. if not resstrs.List.Empty then
  277. begin
  278. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  279. resstrs.CreateResourceStringData;
  280. resstrs.WriteResourceFile;
  281. end;
  282. resstrs.Free;
  283. end;
  284. end.