cresstr.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 WriteResourceFile;
  55. procedure WriteRSJFile;
  56. procedure RegisterResourceStrings;
  57. end;
  58. { ---------------------------------------------------------------------
  59. TRESOURCESTRING_ITEM
  60. ---------------------------------------------------------------------}
  61. constructor TResourceStringItem.Create(asym:TConstsym);
  62. begin
  63. inherited Create;
  64. Sym:=Asym;
  65. Name:=lower(asym.owner.name^+'.'+asym.Name);
  66. Len:=asym.value.len;
  67. GetMem(Value,Len);
  68. Move(asym.value.valueptr^,Value^,Len);
  69. CalcHash;
  70. end;
  71. destructor TResourceStringItem.Destroy;
  72. begin
  73. FreeMem(Value);
  74. end;
  75. procedure TResourceStringItem.CalcHash;
  76. Var
  77. g : Cardinal;
  78. I : longint;
  79. begin
  80. hash:=0;
  81. For I:=0 to Len-1 do { 0 terminated }
  82. begin
  83. hash:=hash shl 4;
  84. inc(Hash,Ord(Value[i]));
  85. g:=hash and ($f shl 28);
  86. if g<>0 then
  87. begin
  88. hash:=hash xor (g shr 24);
  89. hash:=hash xor g;
  90. end;
  91. end;
  92. If Hash=0 then
  93. Hash:=$ffffffff;
  94. end;
  95. { ---------------------------------------------------------------------
  96. Tresourcestrings
  97. ---------------------------------------------------------------------}
  98. Constructor Tresourcestrings.Create;
  99. begin
  100. List:=TLinkedList.Create;
  101. end;
  102. Destructor Tresourcestrings.Destroy;
  103. begin
  104. List.Free;
  105. end;
  106. procedure Tresourcestrings.CreateResourceStringData;
  107. Var
  108. namelab,
  109. valuelab : tasmlabofs;
  110. resstrlab : tasmsymbol;
  111. endsymlab : tasmsymbol;
  112. R : TResourceStringItem;
  113. begin
  114. { Put resourcestrings in a new objectfile. Putting it in multiple files
  115. makes the linking too dependent on the linker script requiring a SORT(*) for
  116. the data sections }
  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:=emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage,False);
  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:=emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage,False)
  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:=emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage,False);
  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. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  171. endsymlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',current_module.localsymtable,'END'),AB_GLOBAL,AT_DATA);
  172. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.create_global(endsymlab,0));
  173. { The darwin/ppc64 assembler or linker seems to have trouble }
  174. { if a section ends with a global label without any data after it. }
  175. { So for safety, just put a dummy value here. }
  176. { Further, the regular linker also kills this symbol when turning }
  177. { on smart linking in case no value appears after it, so put the }
  178. { dummy byte there always }
  179. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  180. { relocated before all other data, so make this data relocatable, }
  181. { otherwise the end label won't be moved with the rest }
  182. if (target_info.system in (systems_darwin+systems_aix)) then
  183. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_sym(endsymlab));
  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:=ChangeFileExt(current_module.ppufilename,'.rst');
  202. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  203. Assign(F,ResFileName);
  204. {$push}{$i-}
  205. Rewrite(f);
  206. {$pop}
  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.WriteRSJFile;
  259. Var
  260. F: Text;
  261. R: TResourceStringItem;
  262. ResFileName: string;
  263. I: Integer;
  264. C: tcompilerwidechar;
  265. W: pcompilerwidestring;
  266. begin
  267. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  268. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  269. Assign(F,ResFileName);
  270. {$push}{$i-}
  271. Rewrite(f);
  272. {$pop}
  273. if IOresult<>0 then
  274. begin
  275. message1(general_e_errorwritingresourcefile,ResFileName);
  276. exit;
  277. end;
  278. writeln(f,'{"version":1,"strings":[');
  279. R:=TResourceStringItem(List.First);
  280. while assigned(R) do
  281. begin
  282. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","value":"');
  283. initwidestring(W);
  284. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  285. for I := 0 to W^.len - 1 do
  286. begin
  287. C := W^.Data[I];
  288. case C of
  289. Ord('"'), Ord('\'), Ord('/'):
  290. write(f, '\', Chr(C));
  291. 8:
  292. write(f, '\b');
  293. 9:
  294. write(f, '\t');
  295. 10:
  296. write(f, '\n');
  297. 13:
  298. write(f, '\r');
  299. 12:
  300. write(f, '\f');
  301. else
  302. if (C < 32) or (C > 127) then
  303. write(f,'\u',hexStr(Longint(C), 4))
  304. else
  305. write(f,Chr(C));
  306. end;
  307. end;
  308. donewidestring(W);
  309. write(f,'"}');
  310. R:=TResourceStringItem(R.Next);
  311. if assigned(R) then
  312. writeln(f,',')
  313. else
  314. writeln(f);
  315. end;
  316. writeln(f,']}');
  317. close(f);
  318. end;
  319. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  320. begin
  321. if (tsym(p).typ=constsym) and
  322. (tconstsym(p).consttyp=constresourcestring) then
  323. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  324. end;
  325. procedure Tresourcestrings.RegisterResourceStrings;
  326. begin
  327. if assigned(current_module.globalsymtable) then
  328. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  329. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  330. end;
  331. Procedure GenerateResourceStrings;
  332. var
  333. resstrs : Tresourcestrings;
  334. begin
  335. resstrs:=Tresourcestrings.Create;
  336. resstrs.RegisterResourceStrings;
  337. if not resstrs.List.Empty then
  338. begin
  339. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  340. resstrs.CreateResourceStringData;
  341. resstrs.WriteResourceFile;
  342. resstrs.WriteRSJFile;
  343. end;
  344. resstrs.Free;
  345. end;
  346. end.