cresstr.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. symbase,symconst,symtype,symdef,symsym,symtable,
  27. verbose,fmodule,ppu,
  28. aasmtai,aasmdata,aasmcnst,
  29. aasmcpu;
  30. Type
  31. { These are used to form a singly-linked list, ordered by hash value }
  32. TResourceStringItem = class(TLinkedListItem)
  33. Sym : TConstSym;
  34. Name : String;
  35. Value : Pchar;
  36. Len : Longint;
  37. hash : Cardinal;
  38. constructor Create(asym:TConstsym);
  39. destructor Destroy;override;
  40. procedure CalcHash;
  41. end;
  42. Tresourcestrings=class
  43. private
  44. List : TLinkedList;
  45. procedure ConstSym_Register(p:TObject;arg:pointer);
  46. public
  47. constructor Create;
  48. destructor Destroy;override;
  49. procedure CreateResourceStringData;
  50. procedure WriteRSJFile;
  51. procedure RegisterResourceStrings;
  52. end;
  53. { ---------------------------------------------------------------------
  54. TRESOURCESTRING_ITEM
  55. ---------------------------------------------------------------------}
  56. constructor TResourceStringItem.Create(asym:TConstsym);
  57. begin
  58. inherited Create;
  59. Sym:=Asym;
  60. Name:=lower(asym.owner.name^+'.'+asym.Name);
  61. Len:=asym.value.len;
  62. GetMem(Value,Len);
  63. Move(asym.value.valueptr^,Value^,Len);
  64. CalcHash;
  65. end;
  66. destructor TResourceStringItem.Destroy;
  67. begin
  68. FreeMem(Value);
  69. end;
  70. procedure TResourceStringItem.CalcHash;
  71. Var
  72. g : Cardinal;
  73. I : longint;
  74. begin
  75. hash:=0;
  76. For I:=0 to Len-1 do { 0 terminated }
  77. begin
  78. hash:=hash shl 4;
  79. inc(Hash,Ord(Value[i]));
  80. g:=hash and ($f shl 28);
  81. if g<>0 then
  82. begin
  83. hash:=hash xor (g shr 24);
  84. hash:=hash xor g;
  85. end;
  86. end;
  87. If Hash=0 then
  88. Hash:=$ffffffff;
  89. end;
  90. { ---------------------------------------------------------------------
  91. Tresourcestrings
  92. ---------------------------------------------------------------------}
  93. Constructor Tresourcestrings.Create;
  94. begin
  95. List:=TAsmList.Create;
  96. end;
  97. Destructor Tresourcestrings.Destroy;
  98. begin
  99. List.Free;
  100. end;
  101. procedure Tresourcestrings.CreateResourceStringData;
  102. Var
  103. namelab,
  104. valuelab : tasmlabofs;
  105. R : TResourceStringItem;
  106. resstrdef: tdef;
  107. tcb : ttai_typedconstbuilder;
  108. begin
  109. resstrdef:=search_system_type('TRESOURCESTRINGRECORD').typedef;
  110. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_start,tcalo_data_force_indirect,tcalo_is_public_asm]);
  111. { Write unitname entry }
  112. tcb.maybe_begin_aggregate(resstrdef);
  113. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage);
  114. tcb.emit_string_offset(namelab,length(current_module.localsymtable.name^),st_ansistring,false,charpointertype);
  115. tcb.emit_tai(tai_const.create_nil_dataptr,cansistringtype);
  116. tcb.emit_tai(tai_const.create_nil_dataptr,cansistringtype);
  117. tcb.emit_ord_const(0,u32inttype);
  118. tcb.maybe_end_aggregate(resstrdef);
  119. current_asmdata.asmlists[al_resourcestrings].concatList(
  120. tcb.get_final_asmlist_vectorized_dead_strip(
  121. resstrdef,'RESSTR','',current_module.localsymtable,sizeof(pint)
  122. )
  123. );
  124. tcb.free;
  125. { Add entries }
  126. R:=TResourceStringItem(List.First);
  127. while assigned(R) do
  128. begin
  129. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_item,tcalo_data_force_indirect]);
  130. if assigned(R.value) and (R.len<>0) then
  131. valuelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],R.Value,R.Len,getansistringcodepage)
  132. else
  133. begin
  134. valuelab.lab:=nil;
  135. valuelab.ofs:=0;
  136. end;
  137. current_asmdata.asmlists[al_const].concat(cai_align.Create(sizeof(pint)));
  138. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage);
  139. {
  140. Resourcestring index:
  141. TResourceStringRecord = Packed Record
  142. Name,
  143. CurrentValue,
  144. DefaultValue : AnsiString;
  145. HashValue : LongWord;
  146. end;
  147. }
  148. tcb.maybe_begin_aggregate(resstrdef);
  149. tcb.emit_string_offset(namelab,length(current_module.localsymtable.name^),st_ansistring,false,charpointertype);
  150. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  151. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  152. tcb.emit_ord_const(R.hash,u32inttype);
  153. tcb.maybe_end_aggregate(resstrdef);
  154. current_asmdata.asmlists[al_resourcestrings].concatList(
  155. tcb.get_final_asmlist_vectorized_dead_strip(
  156. resstrdef,'RESSTR',R.Sym.Name,R.Sym.Owner,sizeof(pint))
  157. );
  158. R:=TResourceStringItem(R.Next);
  159. tcb.free;
  160. end;
  161. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_end,tcalo_data_force_indirect,tcalo_is_public_asm]);
  162. tcb.begin_anonymous_record(internaltypeprefixName[itp_emptyrec],
  163. default_settings.packrecords,sizeof(pint),
  164. targetinfos[target_info.system]^.alignment.recordalignmin);
  165. current_asmdata.AsmLists[al_resourcestrings].concatList(
  166. tcb.get_final_asmlist_vectorized_dead_strip(
  167. tcb.end_anonymous_record,'RESSTR','',current_module.localsymtable,sizeof(pint)
  168. )
  169. );
  170. tcb.free;
  171. end;
  172. procedure Tresourcestrings.WriteRSJFile;
  173. Var
  174. F: Text;
  175. R: TResourceStringItem;
  176. ResFileName: string;
  177. I: Integer;
  178. C: tcompilerwidechar;
  179. W: pcompilerwidestring;
  180. begin
  181. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  182. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  183. Assign(F,ResFileName);
  184. {$push}{$i-}
  185. Rewrite(f);
  186. {$pop}
  187. if IOresult<>0 then
  188. begin
  189. message1(general_e_errorwritingresourcefile,ResFileName);
  190. exit;
  191. end;
  192. { write the data in two formats:
  193. a) backward compatible: the plain bytes from the source file
  194. b) portable: converted to utf-16
  195. }
  196. writeln(f,'{"version":1,"strings":[');
  197. R:=TResourceStringItem(List.First);
  198. while assigned(R) do
  199. begin
  200. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","sourcebytes":[');
  201. for i:=0 to R.Len-1 do
  202. begin
  203. write(f,ord(R.Value[i]));
  204. if i<>R.Len-1 then
  205. write(f,',');
  206. end;
  207. write(f,'],"value":"');
  208. initwidestring(W);
  209. ascii2unicode(R.Value,R.Len,current_settings.sourcecodepage,W);
  210. for I := 0 to W^.len - 1 do
  211. begin
  212. C := W^.Data[I];
  213. case C of
  214. Ord('"'), Ord('\'), Ord('/'):
  215. write(f, '\', Chr(C));
  216. 8:
  217. write(f, '\b');
  218. 9:
  219. write(f, '\t');
  220. 10:
  221. write(f, '\n');
  222. 13:
  223. write(f, '\r');
  224. 12:
  225. write(f, '\f');
  226. else
  227. if (C < 32) or (C > 127) then
  228. write(f,'\u',hexStr(Longint(C), 4))
  229. else
  230. write(f,Chr(C));
  231. end;
  232. end;
  233. donewidestring(W);
  234. write(f,'"}');
  235. R:=TResourceStringItem(R.Next);
  236. if assigned(R) then
  237. writeln(f,',')
  238. else
  239. writeln(f);
  240. end;
  241. writeln(f,']}');
  242. close(f);
  243. end;
  244. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  245. begin
  246. if (tsym(p).typ=constsym) and
  247. (tconstsym(p).consttyp=constresourcestring) then
  248. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  249. end;
  250. procedure Tresourcestrings.RegisterResourceStrings;
  251. begin
  252. if assigned(current_module.globalsymtable) then
  253. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  254. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  255. end;
  256. Procedure GenerateResourceStrings;
  257. var
  258. resstrs : Tresourcestrings;
  259. begin
  260. { needed for the typed constant defs that get generated/looked up }
  261. if assigned(current_module.globalsymtable) then
  262. symtablestack.push(current_module.globalsymtable);
  263. symtablestack.push(current_module.localsymtable);
  264. resstrs:=Tresourcestrings.Create;
  265. resstrs.RegisterResourceStrings;
  266. if not resstrs.List.Empty then
  267. begin
  268. include(current_module.moduleflags,mf_has_resourcestrings);
  269. resstrs.CreateResourceStringData;
  270. resstrs.WriteRSJFile;
  271. end;
  272. resstrs.Free;
  273. symtablestack.pop(current_module.localsymtable);
  274. if assigned(current_module.globalsymtable) then
  275. symtablestack.pop(current_module.globalsymtable);
  276. end;
  277. end.