2
0

cresstr.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. resstrlab : tasmsymbol;
  110. endsymlab : tasmsymbol;
  111. R : TResourceStringItem;
  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. maybe_new_object_file(current_asmdata.asmlists[al_const]);
  117. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTRTABLE',current_module.localsymtable,''),sizeof(pint));
  118. maybe_new_object_file(current_asmdata.asmlists[al_resourcestrings]);
  119. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'1_START'),sizeof(pint));
  120. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  121. make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
  122. { Write unitname entry }
  123. namelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage,False);
  124. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  125. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  126. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_nil_dataptr);
  127. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  128. {$ifdef cpu64bitaddr}
  129. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  130. {$endif cpu64bitaddr}
  131. { Add entries }
  132. R:=TResourceStringItem(List.First);
  133. while assigned(R) do
  134. begin
  135. new_section(current_asmdata.asmlists[al_const],sec_rodata_norel,make_mangledname('RESSTR',current_module.localsymtable,'d_'+r.name),sizeof(pint));
  136. { Write default value }
  137. if assigned(R.value) and (R.len<>0) then
  138. valuelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage,False)
  139. else
  140. begin
  141. valuelab.lab:=nil;
  142. valuelab.ofs:=0;
  143. end;
  144. { Append the name as a ansistring. }
  145. current_asmdata.asmlists[al_const].concat(cai_align.Create(const_align(sizeof(pint))));
  146. namelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage,False);
  147. {
  148. Resourcestring index:
  149. TResourceStringRecord = Packed Record
  150. Name,
  151. CurrentValue,
  152. DefaultValue : AnsiString;
  153. HashValue : LongWord;
  154. end;
  155. }
  156. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(pint));
  157. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  158. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  159. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(namelab.lab,namelab.ofs));
  160. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.Create_sym_offset(valuelab.lab,valuelab.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_32bit(longint(R.Hash)));
  163. {$ifdef cpu64bitaddr}
  164. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  165. {$endif cpu64bitaddr}
  166. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  167. R:=TResourceStringItem(R.Next);
  168. end;
  169. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  170. endsymlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',current_module.localsymtable,'END'),AB_GLOBAL,AT_DATA);
  171. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.create_global(endsymlab,0));
  172. { The darwin/ppc64 assembler or linker seems to have trouble }
  173. { if a section ends with a global label without any data after it. }
  174. { So for safety, just put a dummy value here. }
  175. { Further, the regular linker also kills this symbol when turning }
  176. { on smart linking in case no value appears after it, so put the }
  177. { dummy byte there always }
  178. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  179. { relocated before all other data, so make this data relocatable, }
  180. { otherwise the end label won't be moved with the rest }
  181. if (target_info.system in (systems_darwin+systems_aix)) then
  182. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_sym(endsymlab));
  183. end;
  184. procedure Tresourcestrings.WriteRSJFile;
  185. Var
  186. F: Text;
  187. R: TResourceStringItem;
  188. ResFileName: string;
  189. I: Integer;
  190. C: tcompilerwidechar;
  191. W: pcompilerwidestring;
  192. begin
  193. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  194. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  195. Assign(F,ResFileName);
  196. {$push}{$i-}
  197. Rewrite(f);
  198. {$pop}
  199. if IOresult<>0 then
  200. begin
  201. message1(general_e_errorwritingresourcefile,ResFileName);
  202. exit;
  203. end;
  204. writeln(f,'{"version":1,"strings":[');
  205. R:=TResourceStringItem(List.First);
  206. while assigned(R) do
  207. begin
  208. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","value":"');
  209. initwidestring(W);
  210. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  211. for I := 0 to W^.len - 1 do
  212. begin
  213. C := W^.Data[I];
  214. case C of
  215. Ord('"'), Ord('\'), Ord('/'):
  216. write(f, '\', Chr(C));
  217. 8:
  218. write(f, '\b');
  219. 9:
  220. write(f, '\t');
  221. 10:
  222. write(f, '\n');
  223. 13:
  224. write(f, '\r');
  225. 12:
  226. write(f, '\f');
  227. else
  228. if (C < 32) or (C > 127) then
  229. write(f,'\u',hexStr(Longint(C), 4))
  230. else
  231. write(f,Chr(C));
  232. end;
  233. end;
  234. donewidestring(W);
  235. write(f,'"}');
  236. R:=TResourceStringItem(R.Next);
  237. if assigned(R) then
  238. writeln(f,',')
  239. else
  240. writeln(f);
  241. end;
  242. writeln(f,']}');
  243. close(f);
  244. end;
  245. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  246. begin
  247. if (tsym(p).typ=constsym) and
  248. (tconstsym(p).consttyp=constresourcestring) then
  249. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  250. end;
  251. procedure Tresourcestrings.RegisterResourceStrings;
  252. begin
  253. if assigned(current_module.globalsymtable) then
  254. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  255. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  256. end;
  257. Procedure GenerateResourceStrings;
  258. var
  259. resstrs : Tresourcestrings;
  260. begin
  261. resstrs:=Tresourcestrings.Create;
  262. resstrs.RegisterResourceStrings;
  263. if not resstrs.List.Empty then
  264. begin
  265. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  266. resstrs.CreateResourceStringData;
  267. resstrs.WriteRSJFile;
  268. end;
  269. resstrs.Free;
  270. end;
  271. end.