cresstr.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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,symtable,
  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. R : TResourceStringItem;
  110. resstrdef: tdef;
  111. tcb : ttai_typedconstbuilder;
  112. begin
  113. resstrdef:=search_system_type('TRESOURCESTRINGRECORD').typedef;
  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. tcb:=ctai_typedconstbuilder.create([tcalo_make_dead_strippable,tcalo_new_section,tcalo_vectorized_dead_strip_start]);
  118. { Write unitname entry }
  119. tcb.maybe_begin_aggregate(resstrdef);
  120. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage);
  121. tcb.emit_string_offset(namelab,length(current_module.localsymtable.name^),st_ansistring,false,charpointertype);
  122. tcb.emit_tai(tai_const.create_nil_dataptr,voidpointertype);
  123. tcb.emit_tai(tai_const.create_nil_dataptr,voidpointertype);
  124. tcb.emit_ord_const(0,u32inttype);
  125. tcb.maybe_end_aggregate(resstrdef);
  126. current_asmdata.asmlists[al_resourcestrings].concatList(
  127. tcb.get_final_asmlist_vectorized_dead_strip(
  128. resstrdef,'RESSTR','',current_module.localsymtable,sizeof(pint)
  129. )
  130. );
  131. tcb.free;
  132. { Add entries }
  133. R:=TResourceStringItem(List.First);
  134. while assigned(R) do
  135. begin
  136. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_vectorized_dead_strip_item]);
  137. if assigned(R.value) and (R.len<>0) then
  138. valuelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage)
  139. else
  140. begin
  141. valuelab.lab:=nil;
  142. valuelab.ofs:=0;
  143. end;
  144. current_asmdata.asmlists[al_const].concat(cai_align.Create(const_align(sizeof(pint))));
  145. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage);
  146. {
  147. Resourcestring index:
  148. TResourceStringRecord = Packed Record
  149. Name,
  150. CurrentValue,
  151. DefaultValue : AnsiString;
  152. HashValue : LongWord;
  153. end;
  154. }
  155. tcb.maybe_begin_aggregate(resstrdef);
  156. tcb.emit_string_offset(namelab,length(current_module.localsymtable.name^),st_ansistring,false,charpointertype);
  157. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  158. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  159. tcb.emit_ord_const(R.hash,u32inttype);
  160. tcb.maybe_end_aggregate(resstrdef);
  161. current_asmdata.asmlists[al_resourcestrings].concatList(
  162. tcb.get_final_asmlist_vectorized_dead_strip(
  163. resstrdef,'RESSTR',R.Sym.Name,R.Sym.Owner,sizeof(pint))
  164. );
  165. R:=TResourceStringItem(R.Next);
  166. tcb.free;
  167. end;
  168. tcb:=ctai_typedconstbuilder.create([tcalo_new_section,tcalo_vectorized_dead_strip_end]);
  169. tcb.begin_anonymous_record(internaltypeprefixName[itp_emptyrec],
  170. default_settings.packrecords,sizeof(pint),
  171. targetinfos[target_info.system]^.alignment.recordalignmin,
  172. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  173. current_asmdata.AsmLists[al_resourcestrings].concatList(
  174. tcb.get_final_asmlist_vectorized_dead_strip(
  175. tcb.end_anonymous_record,'RESSTR','',current_module.localsymtable,sizeof(pint)
  176. )
  177. );
  178. tcb.free;
  179. end;
  180. procedure Tresourcestrings.WriteRSJFile;
  181. Var
  182. F: Text;
  183. R: TResourceStringItem;
  184. ResFileName: string;
  185. I: Integer;
  186. C: tcompilerwidechar;
  187. W: pcompilerwidestring;
  188. begin
  189. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  190. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  191. Assign(F,ResFileName);
  192. {$push}{$i-}
  193. Rewrite(f);
  194. {$pop}
  195. if IOresult<>0 then
  196. begin
  197. message1(general_e_errorwritingresourcefile,ResFileName);
  198. exit;
  199. end;
  200. { write the data in two formats:
  201. a) backward compatible: the plain bytes from the source file
  202. b) portable: converted to utf-16
  203. }
  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,'","sourcebytes":[');
  209. for i:=0 to R.Len-1 do
  210. begin
  211. write(f,ord(R.Value[i]));
  212. if i<>R.Len-1 then
  213. write(f,',');
  214. end;
  215. write(f,'],"value":"');
  216. initwidestring(W);
  217. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  218. for I := 0 to W^.len - 1 do
  219. begin
  220. C := W^.Data[I];
  221. case C of
  222. Ord('"'), Ord('\'), Ord('/'):
  223. write(f, '\', Chr(C));
  224. 8:
  225. write(f, '\b');
  226. 9:
  227. write(f, '\t');
  228. 10:
  229. write(f, '\n');
  230. 13:
  231. write(f, '\r');
  232. 12:
  233. write(f, '\f');
  234. else
  235. if (C < 32) or (C > 127) then
  236. write(f,'\u',hexStr(Longint(C), 4))
  237. else
  238. write(f,Chr(C));
  239. end;
  240. end;
  241. donewidestring(W);
  242. write(f,'"}');
  243. R:=TResourceStringItem(R.Next);
  244. if assigned(R) then
  245. writeln(f,',')
  246. else
  247. writeln(f);
  248. end;
  249. writeln(f,']}');
  250. close(f);
  251. end;
  252. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  253. begin
  254. if (tsym(p).typ=constsym) and
  255. (tconstsym(p).consttyp=constresourcestring) then
  256. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  257. end;
  258. procedure Tresourcestrings.RegisterResourceStrings;
  259. begin
  260. if assigned(current_module.globalsymtable) then
  261. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  262. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  263. end;
  264. Procedure GenerateResourceStrings;
  265. var
  266. resstrs : Tresourcestrings;
  267. begin
  268. { needed for the typed constant defs that get generated/looked up }
  269. if assigned(current_module.globalsymtable) then
  270. symtablestack.push(current_module.globalsymtable);
  271. symtablestack.push(current_module.localsymtable);
  272. resstrs:=Tresourcestrings.Create;
  273. resstrs.RegisterResourceStrings;
  274. if not resstrs.List.Empty then
  275. begin
  276. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  277. resstrs.CreateResourceStringData;
  278. resstrs.WriteRSJFile;
  279. end;
  280. resstrs.Free;
  281. symtablestack.pop(current_module.localsymtable);
  282. if assigned(current_module.globalsymtable) then
  283. symtablestack.pop(current_module.globalsymtable);
  284. end;
  285. end.