2
0

cresstr.pas 12 KB

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