cresstr.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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,defutil, 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. AValue : TAnsiCharDynArray;
  36. WValue : pcompilerwidestring; // just a reference, do not free.
  37. Len : Longint; // in bytes, not characters
  38. hash : Cardinal;
  39. isUnicode : Boolean;
  40. constructor Create(asym:TConstsym);
  41. destructor Destroy;override;
  42. procedure CalcHash;
  43. end;
  44. Tresourcestrings=class
  45. private
  46. List : TLinkedList;
  47. procedure ConstSym_Register(p:TObject;arg:pointer);
  48. public
  49. constructor Create;
  50. destructor Destroy;override;
  51. procedure CreateResourceStringData;
  52. procedure WriteRSJFile;
  53. procedure RegisterResourceStrings;
  54. end;
  55. { ---------------------------------------------------------------------
  56. TRESOURCESTRING_ITEM
  57. ---------------------------------------------------------------------}
  58. constructor TResourceStringItem.Create(asym:TConstsym);
  59. var
  60. pw : pcompilerwidestring;
  61. t : TDef;
  62. begin
  63. inherited Create;
  64. Sym:=Asym;
  65. Name:=lower(asym.owner.name^+'.'+asym.Name);
  66. isUnicode:=is_systemunit_unicode;
  67. if IsUnicode then
  68. begin
  69. T:=aSym.constdef;
  70. WValue:=pcompilerwidestring(asym.value.valueptr);
  71. Len:=WValue^.len*sizeOf(tcompilerwidechar);
  72. end
  73. else
  74. begin
  75. Len:=asym.value.len;
  76. SetLength(AValue,Len);
  77. Move(asym.value.valueptr^,AValue[0],Len);
  78. end;
  79. CalcHash;
  80. end;
  81. destructor TResourceStringItem.Destroy;
  82. begin
  83. AValue:=nil;
  84. end;
  85. procedure TResourceStringItem.CalcHash;
  86. Var
  87. g : Cardinal;
  88. llen,wlen,I : longint;
  89. P : PByte;
  90. pc : PAnsiChar;
  91. begin
  92. pc:=nil;
  93. hash:=0;
  94. if IsUnicode then
  95. begin
  96. // Need to calculate hash on UTF8 encoded string, GNU gettext.
  97. llen:=UnicodeToUtf8(nil,0,PUnicodeChar(wValue^.data),wValue^.len);
  98. getmem(pc,llen);
  99. UnicodeToUtf8(PC,llen,PUnicodeChar(wValue^.data),len);
  100. P:=PByte(pc);
  101. llen:=llen-1; // Take of terminating #0
  102. end
  103. else
  104. begin
  105. llen:=Len;
  106. P:=PByte(AValue);
  107. end;
  108. For I:=0 to lLen-1 do { 0 terminated }
  109. begin
  110. hash:=hash shl 4;
  111. inc(Hash,P[i]);
  112. g:=hash and ($f shl 28);
  113. if g<>0 then
  114. begin
  115. hash:=hash xor (g shr 24);
  116. hash:=hash xor g;
  117. end;
  118. end;
  119. if Assigned(Pc) then
  120. FreeMem(PC);
  121. If Hash=0 then
  122. Hash:=$ffffffff;
  123. end;
  124. { ---------------------------------------------------------------------
  125. Tresourcestrings
  126. ---------------------------------------------------------------------}
  127. Constructor Tresourcestrings.Create;
  128. begin
  129. List:=TAsmList.Create;
  130. end;
  131. Destructor Tresourcestrings.Destroy;
  132. begin
  133. List.Free;
  134. end;
  135. procedure Tresourcestrings.CreateResourceStringData;
  136. Var
  137. namelab,
  138. valuelab : tasmlabofs;
  139. R : TResourceStringItem;
  140. resstrdef: tdef;
  141. tcb : ttai_typedconstbuilder;
  142. enc : tstringencoding;
  143. begin
  144. resstrdef:=search_system_type('TRESOURCESTRINGRECORD').typedef;
  145. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_start,tcalo_data_force_indirect,tcalo_is_public_asm]);
  146. { Write unitname entry }
  147. tcb.maybe_begin_aggregate(resstrdef);
  148. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^),getansistringcodepage);
  149. tcb.emit_string_offset(namelab,length(current_module.localsymtable.name^),st_ansistring,false,charpointertype);
  150. tcb.emit_tai(tai_const.create_nil_dataptr,cansistringtype);
  151. tcb.emit_tai(tai_const.create_nil_dataptr,cansistringtype);
  152. tcb.emit_ord_const(0,u32inttype);
  153. tcb.maybe_end_aggregate(resstrdef);
  154. current_asmdata.asmlists[al_resourcestrings].concatList(
  155. tcb.get_final_asmlist_vectorized_dead_strip(
  156. nil,resstrdef,'RESSTR','',current_module.localsymtable,sizeof(pint)
  157. )
  158. );
  159. tcb.free;
  160. { Add entries }
  161. R:=TResourceStringItem(List.First);
  162. while assigned(R) do
  163. begin
  164. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_item,tcalo_data_force_indirect]);
  165. valuelab.lab:=nil;
  166. valuelab.ofs:=0;
  167. if (R.len<>0) then
  168. begin
  169. if R.isUnicode and assigned(R.WValue) then
  170. begin
  171. enc:=tstringdef(cunicodestringtype).encoding;
  172. valuelab:=tcb.emit_unicodestring_const(current_asmdata.asmlists[al_const],R.WValue,enc,False);
  173. end
  174. else
  175. begin
  176. if assigned(R.AValue) then
  177. valuelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],PAnsiChar(R.AValue),R.Len,getansistringcodepage)
  178. end;
  179. end;
  180. current_asmdata.asmlists[al_const].concat(cai_align.Create(sizeof(pint)));
  181. namelab:=tcb.emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage);
  182. {
  183. Resourcestring index:
  184. TResourceStringRecord = Packed Record
  185. Name,
  186. CurrentValue,
  187. DefaultValue : AnsiString/Widestring;
  188. HashValue : LongWord;
  189. end;
  190. }
  191. tcb.maybe_begin_aggregate(resstrdef);
  192. tcb.emit_string_offset(namelab,length(R.name),st_ansistring,false,charpointertype);
  193. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  194. tcb.emit_string_offset(valuelab,R.Len,st_ansistring,false,charpointertype);
  195. tcb.emit_ord_const(R.hash,u32inttype);
  196. tcb.maybe_end_aggregate(resstrdef);
  197. current_asmdata.asmlists[al_resourcestrings].concatList(
  198. tcb.get_final_asmlist_vectorized_dead_strip(
  199. R.Sym,resstrdef,'RESSTR',R.Sym.Name,R.Sym.Owner,sizeof(pint))
  200. );
  201. R:=TResourceStringItem(R.Next);
  202. tcb.free;
  203. end;
  204. tcb:=ctai_typedconstbuilder.create([tcalo_vectorized_dead_strip_end,tcalo_data_force_indirect,tcalo_is_public_asm]);
  205. tcb.begin_anonymous_record(internaltypeprefixName[itp_emptyrec],
  206. default_settings.packrecords,sizeof(pint),
  207. targetinfos[target_info.system]^.alignment.recordalignmin);
  208. current_asmdata.AsmLists[al_resourcestrings].concatList(
  209. tcb.get_final_asmlist_vectorized_dead_strip(
  210. nil,tcb.end_anonymous_record,'RESSTR','',current_module.localsymtable,sizeof(pint)
  211. )
  212. );
  213. tcb.free;
  214. end;
  215. procedure Tresourcestrings.WriteRSJFile;
  216. Var
  217. F: Text;
  218. R: TResourceStringItem;
  219. ResFileName: string;
  220. I,Len: Integer;
  221. C: tcompilerwidechar;
  222. W: pcompilerwidestring;
  223. P : PByte;
  224. begin
  225. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rsj');
  226. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  227. Assign(F,ResFileName);
  228. {$push}{$i-}
  229. Rewrite(f);
  230. {$pop}
  231. if IOresult<>0 then
  232. begin
  233. message1(general_e_errorwritingresourcefile,ResFileName);
  234. exit;
  235. end;
  236. { write the data in two formats:
  237. a) backward compatible: the plain bytes from the source file
  238. b) portable: converted to utf-16
  239. }
  240. writeln(f,'{"version":1,"strings":[');
  241. R:=TResourceStringItem(List.First);
  242. while assigned(R) do
  243. begin
  244. write(f, '{"hash":',R.Hash,',"name":"',R.Name,'","sourcebytes":[');
  245. if R.isUnicode then
  246. P:=PByte(R.WValue^.data)
  247. else
  248. P:=PByte(R.AValue);
  249. for i:=0 to R.Len-1 do
  250. begin
  251. write(f,P[i]);
  252. if i<>R.Len-1 then
  253. write(f,',');
  254. end;
  255. write(f,'],"value":"');
  256. if Not r.isUnicode then
  257. begin
  258. initwidestring(W);
  259. ascii2unicode(PAnsiChar(R.AValue),R.Len,current_settings.sourcecodepage,W);
  260. end
  261. else
  262. begin
  263. W:=R.WValue;
  264. end;
  265. for I := 0 to W^.len - 1 do
  266. begin
  267. C := W^.Data[I];
  268. case C of
  269. Ord('"'), Ord('\'), Ord('/'):
  270. write(f, '\', Chr(C));
  271. 8:
  272. write(f, '\b');
  273. 9:
  274. write(f, '\t');
  275. 10:
  276. write(f, '\n');
  277. 13:
  278. write(f, '\r');
  279. 12:
  280. write(f, '\f');
  281. else
  282. if (C < 32) or (C > 127) then
  283. write(f,'\u',hexStr(Longint(C), 4))
  284. else
  285. write(f,Chr(C));
  286. end;
  287. end;
  288. if W<>R.WValue then
  289. donewidestring(W);
  290. write(f,'"}');
  291. R:=TResourceStringItem(R.Next);
  292. if assigned(R) then
  293. writeln(f,',')
  294. else
  295. writeln(f);
  296. end;
  297. writeln(f,']}');
  298. close(f);
  299. end;
  300. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  301. begin
  302. if (tsym(p).typ=constsym) and
  303. (tconstsym(p).consttyp in [constresourcestring,constwresourcestring]) then
  304. List.Concat(TResourceStringItem.Create(TConstsym(p)));
  305. end;
  306. procedure Tresourcestrings.RegisterResourceStrings;
  307. begin
  308. if assigned(current_module.globalsymtable) then
  309. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  310. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  311. end;
  312. Procedure GenerateResourceStrings;
  313. var
  314. resstrs : Tresourcestrings;
  315. begin
  316. { needed for the typed constant defs that get generated/looked up }
  317. if assigned(current_module.globalsymtable) then
  318. symtablestack.push(current_module.globalsymtable);
  319. symtablestack.push(current_module.localsymtable);
  320. resstrs:=Tresourcestrings.Create;
  321. resstrs.RegisterResourceStrings;
  322. if not resstrs.List.Empty then
  323. begin
  324. include(current_module.moduleflags,mf_has_resourcestrings);
  325. resstrs.CreateResourceStringData;
  326. resstrs.WriteRSJFile;
  327. end;
  328. resstrs.Free;
  329. symtablestack.pop(current_module.localsymtable);
  330. if assigned(current_module.globalsymtable) then
  331. symtablestack.pop(current_module.globalsymtable);
  332. end;
  333. end.