cresstr.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 : tasmlabel;
  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(namelab));
  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. valuelab:=nil;
  141. { Append the name as a ansistring. }
  142. current_asmdata.asmlists[al_const].concat(cai_align.Create(const_align(sizeof(pint))));
  143. namelab:=emit_ansistring_const(current_asmdata.asmlists[al_const],@R.Name[1],length(R.name),getansistringcodepage,False);
  144. {
  145. Resourcestring index:
  146. TResourceStringRecord = Packed Record
  147. Name,
  148. CurrentValue,
  149. DefaultValue : AnsiString;
  150. HashValue : LongWord;
  151. end;
  152. }
  153. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(pint));
  154. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  155. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  156. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
  157. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(valuelab));
  158. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(valuelab));
  159. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(longint(R.Hash)));
  160. {$ifdef cpu64bitaddr}
  161. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  162. {$endif cpu64bitaddr}
  163. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  164. R:=TResourceStringItem(R.Next);
  165. end;
  166. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  167. endsymlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',current_module.localsymtable,'END'),AB_GLOBAL,AT_DATA);
  168. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.create_global(endsymlab,0));
  169. { The darwin/ppc64 assembler or linker seems to have trouble }
  170. { if a section ends with a global label without any data after it. }
  171. { So for safety, just put a dummy value here. }
  172. { Further, the regular linker also kills this symbol when turning }
  173. { on smart linking in case no value appears after it, so put the }
  174. { dummy byte there always }
  175. { Update: the Mac OS X 10.6 linker orders data that needs to be }
  176. { relocated before all other data, so make this data relocatable, }
  177. { otherwise the end label won't be moved with the rest }
  178. if (target_info.system in systems_darwin) then
  179. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_sym(endsymlab));
  180. end;
  181. Procedure Tresourcestrings.WriteResourceFile;
  182. Type
  183. TMode = (quoted,unquoted);
  184. Var
  185. F : Text;
  186. Mode : TMode;
  187. R : TResourceStringItem;
  188. C : char;
  189. Col,i : longint;
  190. ResFileName : string;
  191. Procedure Add(Const S : String);
  192. begin
  193. Write(F,S);
  194. inc(Col,length(s));
  195. end;
  196. begin
  197. ResFileName:=ChangeFileExt(current_module.ppufilename^,'.rst');
  198. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  199. Assign(F,ResFileName);
  200. {$push}{$i-}
  201. Rewrite(f);
  202. {$pop}
  203. If IOresult<>0 then
  204. begin
  205. message1(general_e_errorwritingresourcefile,ResFileName);
  206. exit;
  207. end;
  208. R:=TResourceStringItem(List.First);
  209. while assigned(R) do
  210. begin
  211. writeln(f);
  212. Writeln(f,'# hash value = ',R.Hash);
  213. col:=0;
  214. Add(R.Name+'=');
  215. Mode:=unquoted;
  216. For I:=0 to R.Len-1 do
  217. begin
  218. C:=R.Value[i];
  219. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  220. begin
  221. If mode=Quoted then
  222. Add(c)
  223. else
  224. begin
  225. Add(''''+c);
  226. mode:=quoted
  227. end;
  228. end
  229. else
  230. begin
  231. If Mode=quoted then
  232. begin
  233. Add('''');
  234. mode:=unquoted;
  235. end;
  236. Add('#'+tostr(ord(c)));
  237. end;
  238. If Col>72 then
  239. begin
  240. if mode=quoted then
  241. Write (F,'''');
  242. Writeln(F,'+');
  243. Col:=0;
  244. Mode:=unQuoted;
  245. end;
  246. end;
  247. if mode=quoted then
  248. writeln (f,'''');
  249. Writeln(f);
  250. R:=TResourceStringItem(R.Next);
  251. end;
  252. close(f);
  253. end;
  254. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  255. begin
  256. if (tsym(p).typ=constsym) and
  257. (tconstsym(p).consttyp=constresourcestring) then
  258. List.Concat(tResourceStringItem.Create(TConstsym(p)));
  259. end;
  260. procedure Tresourcestrings.RegisterResourceStrings;
  261. begin
  262. if assigned(current_module.globalsymtable) then
  263. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  264. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  265. end;
  266. Procedure GenerateResourceStrings;
  267. var
  268. resstrs : Tresourcestrings;
  269. begin
  270. resstrs:=Tresourcestrings.Create;
  271. resstrs.RegisterResourceStrings;
  272. if not resstrs.List.Empty then
  273. begin
  274. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  275. resstrs.CreateResourceStringData;
  276. resstrs.WriteResourceFile;
  277. end;
  278. resstrs.Free;
  279. end;
  280. end.