cresstr.pas 12 KB

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