cresstr.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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,
  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 WriteResourceFile;
  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_data,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_sym(nil));
  126. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  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,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.WriteResourceFile;
  185. Type
  186. TMode = (quoted,unquoted);
  187. Var
  188. F : Text;
  189. Mode : TMode;
  190. R : TResourceStringItem;
  191. C : char;
  192. Col,i : longint;
  193. ResFileName : string;
  194. Procedure Add(Const S : String);
  195. begin
  196. Write(F,S);
  197. inc(Col,length(s));
  198. end;
  199. begin
  200. ResFileName:=ChangeFileExt(current_module.ppufilename,'.rst');
  201. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  202. Assign(F,ResFileName);
  203. {$push}{$i-}
  204. Rewrite(f);
  205. {$pop}
  206. If IOresult<>0 then
  207. begin
  208. message1(general_e_errorwritingresourcefile,ResFileName);
  209. exit;
  210. end;
  211. R:=TResourceStringItem(List.First);
  212. while assigned(R) do
  213. begin
  214. writeln(f);
  215. Writeln(f,'# hash value = ',R.Hash);
  216. col:=0;
  217. Add(R.Name+'=');
  218. Mode:=unquoted;
  219. For I:=0 to R.Len-1 do
  220. begin
  221. C:=R.Value[i];
  222. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  223. begin
  224. If mode=Quoted then
  225. Add(c)
  226. else
  227. begin
  228. Add(''''+c);
  229. mode:=quoted
  230. end;
  231. end
  232. else
  233. begin
  234. If Mode=quoted then
  235. begin
  236. Add('''');
  237. mode:=unquoted;
  238. end;
  239. Add('#'+tostr(ord(c)));
  240. end;
  241. If Col>72 then
  242. begin
  243. if mode=quoted then
  244. Write (F,'''');
  245. Writeln(F,'+');
  246. Col:=0;
  247. Mode:=unQuoted;
  248. end;
  249. end;
  250. if mode=quoted then
  251. writeln (f,'''');
  252. Writeln(f);
  253. R:=TResourceStringItem(R.Next);
  254. end;
  255. close(f);
  256. end;
  257. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  258. begin
  259. if (tsym(p).typ=constsym) and
  260. (tconstsym(p).consttyp=constresourcestring) then
  261. List.Concat(tResourceStringItem.Create(TConstsym(p)));
  262. end;
  263. procedure Tresourcestrings.RegisterResourceStrings;
  264. begin
  265. if assigned(current_module.globalsymtable) then
  266. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  267. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  268. end;
  269. Procedure GenerateResourceStrings;
  270. var
  271. resstrs : Tresourcestrings;
  272. begin
  273. resstrs:=Tresourcestrings.Create;
  274. resstrs.RegisterResourceStrings;
  275. if not resstrs.List.Empty then
  276. begin
  277. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  278. resstrs.CreateResourceStringData;
  279. resstrs.WriteResourceFile;
  280. end;
  281. resstrs.Free;
  282. end;
  283. end.