cresstr.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. SysUtils,
  24. {$if FPC_FULLVERSION<20700}
  25. ccharset,
  26. {$endif}
  27. cclasses,widestr,
  28. cutils,globtype,globals,systems,
  29. symbase,symconst,symtype,symdef,symsym,
  30. verbose,fmodule,ppu,
  31. aasmbase,aasmtai,aasmdata,aasmcnst,
  32. aasmcpu;
  33. Type
  34. { These are used to form a singly-linked list, ordered by hash value }
  35. TResourceStringItem = class(TLinkedListItem)
  36. Sym : TConstSym;
  37. Name : String;
  38. Value : Pchar;
  39. Len : Longint;
  40. hash : Cardinal;
  41. constructor Create(asym:TConstsym);
  42. destructor Destroy;override;
  43. procedure CalcHash;
  44. end;
  45. Tresourcestrings=class
  46. private
  47. List : TLinkedList;
  48. procedure ConstSym_Register(p:TObject;arg:pointer);
  49. public
  50. constructor Create;
  51. destructor Destroy;override;
  52. procedure CreateResourceStringData;
  53. procedure WriteRSJFile;
  54. procedure RegisterResourceStrings;
  55. end;
  56. { ---------------------------------------------------------------------
  57. TRESOURCESTRING_ITEM
  58. ---------------------------------------------------------------------}
  59. constructor TResourceStringItem.Create(asym:TConstsym);
  60. begin
  61. inherited Create;
  62. Sym:=Asym;
  63. Name:=lower(asym.owner.name^+'.'+asym.Name);
  64. Len:=asym.value.len;
  65. GetMem(Value,Len);
  66. Move(asym.value.valueptr^,Value^,Len);
  67. CalcHash;
  68. end;
  69. destructor TResourceStringItem.Destroy;
  70. begin
  71. FreeMem(Value);
  72. end;
  73. procedure TResourceStringItem.CalcHash;
  74. Var
  75. g : Cardinal;
  76. I : longint;
  77. begin
  78. hash:=0;
  79. For I:=0 to Len-1 do { 0 terminated }
  80. begin
  81. hash:=hash shl 4;
  82. inc(Hash,Ord(Value[i]));
  83. g:=hash and ($f shl 28);
  84. if g<>0 then
  85. begin
  86. hash:=hash xor (g shr 24);
  87. hash:=hash xor g;
  88. end;
  89. end;
  90. If Hash=0 then
  91. Hash:=$ffffffff;
  92. end;
  93. { ---------------------------------------------------------------------
  94. Tresourcestrings
  95. ---------------------------------------------------------------------}
  96. Constructor Tresourcestrings.Create;
  97. begin
  98. List:=TLinkedList.Create;
  99. end;
  100. Destructor Tresourcestrings.Destroy;
  101. begin
  102. List.Free;
  103. end;
  104. procedure Tresourcestrings.CreateResourceStringData;
  105. Var
  106. namelab,
  107. valuelab : tasmlabofs;
  108. resstrlab : tasmsymbol;
  109. endsymlab : tasmsymbol;
  110. R : TResourceStringItem;
  111. tcb : ttai_typedconstbuilder;
  112. begin
  113. { Put resourcestrings in a new objectfile. Putting it in multiple files
  114. makes the linking too dependent on the linker script requiring a SORT(*) for
  115. the data sections }
  116. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section]);
  117. maybe_new_object_file(current_asmdata.asmlists[al_const]);
  118. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTRTABLE',current_module.localsymtable,''),sizeof(pint));
  119. maybe_new_object_file(current_asmdata.asmlists[al_resourcestrings]);
  120. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'1_START'),sizeof(pint));
  121. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  122. make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
  123. { Write unitname entry }
  124. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage);
  125. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  126. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  127. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  128. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  129. {$ifdef cpu64bitaddr}
  130. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  131. {$endif cpu64bitaddr}
  132. { Add entries }
  133. R:=TResourceStringItem(List.First);
  134. while assigned(R) do
  135. begin
  136. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTR',current_module.localsymtable,'d_'+r.name),sizeof(pint));
  137. { Write default value }
  138. if assigned(R.value) and (R.len<>0) then
  139. valuelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage)
  140. else
  141. begin
  142. valuelab.lab:=nil;
  143. valuelab.ofs:=0;
  144. end;
  145. { Append the name as a ansistring. }
  146. current_asmdata.asmlists[al_const].concat(cai_align.Create(const_align(sizeof(pint))));
  147. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage);
  148. {
  149. Resourcestring index:
  150. TResourceStringRecord = Packed Record
  151. Name,
  152. CurrentValue,
  153. DefaultValue : AnsiString;
  154. HashValue : LongWord;
  155. end;
  156. }
  157. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(pint));
  158. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  159. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  160. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  161. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(valuelab.lab,valuelab.ofs));
  162. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(valuelab.lab,valuelab.ofs));
  163. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(longint(R.Hash)));
  164. {$ifdef cpu64bitaddr}
  165. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  166. {$endif cpu64bitaddr}
  167. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  168. R:=TResourceStringItem(R.Next);
  169. end;
  170. { nothing has been emited to the tcb itself }
  171. tcb.free;
  172. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  173. endsymlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',current_module.localsymtable,'END'),AB_GLOBAL,AT_DATA);
  174. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.create_global(endsymlab,0));
  175. { The darwin/ppc64 assembler or linker seems to have trouble }
  176. { if a section ends with a global label without any data after it. }
  177. { So for safety, just put a dummy value here. }
  178. { Further, the regular linker also kills this symbol when turning }
  179. { on smart linking in case no value appears after it, so put the }
  180. { dummy byte there always }
  181. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  182. { relocated before all other data, so make this data relocatable, }
  183. { otherwise the end label won't be moved with the rest }
  184. if (target_info.system in (systems_darwin+systems_aix)) then
  185. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_sym(endsymlab));
  186. end;
  187. procedure Tresourcestrings.WriteRSJFile;
  188. Var
  189. F: Text;
  190. R: TResourceStringItem;
  191. ResFileName: string;
  192. I: Integer;
  193. C: tcompilerwidechar;
  194. W: pcompilerwidestring;
  195. begin
  196. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  197. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  198. Assign(F,ResFileName);
  199. {$push}{$i-}
  200. Rewrite(f);
  201. {$pop}
  202. if IOresult<>0 then
  203. begin
  204. message1(general_e_errorwritingresourcefile,ResFileName);
  205. exit;
  206. end;
  207. writeln(f,'{"version":1,"strings":[');
  208. R:=TResourceStringItem(List.First);
  209. while assigned(R) do
  210. begin
  211. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","value":"');
  212. initwidestring(W);
  213. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  214. for I := 0 to W^.len - 1 do
  215. begin
  216. C := W^.Data[I];
  217. case C of
  218. Ord('"'), Ord('\'), Ord('/'):
  219. write(f, '\', Chr(C));
  220. 8:
  221. write(f, '\b');
  222. 9:
  223. write(f, '\t');
  224. 10:
  225. write(f, '\n');
  226. 13:
  227. write(f, '\r');
  228. 12:
  229. write(f, '\f');
  230. else
  231. if (C < 32) or (C > 127) then
  232. write(f,'\u',hexStr(Longint(C), 4))
  233. else
  234. write(f,Chr(C));
  235. end;
  236. end;
  237. donewidestring(W);
  238. write(f,'"}');
  239. R:=TResourceStringItem(R.Next);
  240. if assigned(R) then
  241. writeln(f,',')
  242. else
  243. writeln(f);
  244. end;
  245. writeln(f,']}');
  246. close(f);
  247. end;
  248. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  249. begin
  250. if (tsym(p).typ=constsym) and
  251. (tconstsym(p).consttyp=constresourcestring) then
  252. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  253. end;
  254. procedure Tresourcestrings.RegisterResourceStrings;
  255. begin
  256. if assigned(current_module.globalsymtable) then
  257. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  258. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  259. end;
  260. Procedure GenerateResourceStrings;
  261. var
  262. resstrs : Tresourcestrings;
  263. begin
  264. { needed for the typed constant defs that get generated/looked up }
  265. if assigned(current_module.globalsymtable) then
  266. symtablestack.push(current_module.globalsymtable);
  267. symtablestack.push(current_module.localsymtable);
  268. resstrs:=Tresourcestrings.Create;
  269. resstrs.RegisterResourceStrings;
  270. if not resstrs.List.Empty then
  271. begin
  272. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  273. resstrs.CreateResourceStringData;
  274. resstrs.WriteRSJFile;
  275. end;
  276. resstrs.Free;
  277. symtablestack.pop(current_module.localsymtable);
  278. if assigned(current_module.globalsymtable) then
  279. symtablestack.pop(current_module.globalsymtable);
  280. end;
  281. end.