cmsgs.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit implements the message object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cmsgs;
  19. {$i fpcdefs.inc}
  20. interface
  21. const
  22. maxmsgidxparts = 20;
  23. type
  24. ppchar=^pchar;
  25. TArrayOfPChar = array[0..1000] of pchar;
  26. PArrayOfPChar = ^TArrayOfPChar;
  27. PMessage=^TMessage;
  28. TMessage=object
  29. msgfilename : string;
  30. msgintern : boolean;
  31. msgallocsize,
  32. msgsize,
  33. msgparts,
  34. msgs : longint;
  35. msgtxt : pchar;
  36. msgidx : array[1..maxmsgidxparts] of PArrayOfPChar;
  37. msgidxmax : array[1..maxmsgidxparts] of longint;
  38. constructor Init(n:longint;const idxmax:array of longint);
  39. destructor Done;
  40. function LoadIntern(p:pointer;n:longint):boolean;
  41. function LoadExtern(const fn:string):boolean;
  42. procedure ClearIdx;
  43. procedure CreateIdx;
  44. function GetPChar(nr:longint):pchar;
  45. function Get(nr:longint;const args:array of string):string;
  46. end;
  47. { this will read a line until #10 or #0 and also increase p }
  48. function GetMsgLine(var p:pchar):string;
  49. implementation
  50. uses
  51. cutils,
  52. {$ifdef DELPHI}
  53. sysutils;
  54. {$else DELPHI}
  55. strings;
  56. {$endif DELPHI}
  57. function MsgReplace(const s:string;const args:array of string):string;
  58. var
  59. last,
  60. i : longint;
  61. hs : string;
  62. begin
  63. if s='' then
  64. begin
  65. MsgReplace:='';
  66. exit;
  67. end;
  68. hs:='';
  69. i:=0;
  70. last:=0;
  71. while (i<length(s)-1) do
  72. begin
  73. inc(i);
  74. if (s[i]='$') and (s[i+1] in ['1'..'9']) then
  75. begin
  76. hs:=hs+copy(s,last+1,i-last-1)+args[byte(s[i+1])-byte('1')];
  77. inc(i);
  78. last:=i;
  79. end;
  80. end;
  81. MsgReplace:=hs+copy(s,last+1,length(s)-last);;
  82. end;
  83. constructor TMessage.Init(n:longint;const idxmax:array of longint);
  84. var
  85. i : longint;
  86. begin
  87. msgtxt:=nil;
  88. msgsize:=0;
  89. msgparts:=n;
  90. if n<>high(idxmax)+1 then
  91. fail;
  92. for i:=1 to n do
  93. begin
  94. msgidxmax[i]:=idxmax[i-1];
  95. getmem(msgidx[i],msgidxmax[i]*4);
  96. fillchar(msgidx[i]^,msgidxmax[i]*4,0);
  97. end;
  98. end;
  99. destructor TMessage.Done;
  100. var
  101. i : longint;
  102. begin
  103. for i:=1 to msgparts do
  104. freemem(msgidx[i],msgidxmax[i]*4);
  105. if msgallocsize>0 then
  106. begin
  107. freemem(msgtxt,msgsize);
  108. msgallocsize:=0;
  109. end;
  110. msgtxt:=nil;
  111. msgsize:=0;
  112. msgparts:=0;
  113. end;
  114. function TMessage.LoadIntern(p:pointer;n:longint):boolean;
  115. begin
  116. msgtxt:=pchar(p);
  117. msgsize:=n;
  118. msgallocsize:=0;
  119. msgintern:=true;
  120. ClearIdx;
  121. CreateIdx;
  122. LoadIntern:=true;
  123. end;
  124. function TMessage.LoadExtern(const fn:string):boolean;
  125. {$ifndef FPC}
  126. procedure readln(var t:text;var s:string);
  127. var
  128. c : char;
  129. i : longint;
  130. begin
  131. c:=#0;
  132. i:=0;
  133. while (not eof(t)) and (c<>#10) do
  134. begin
  135. read(t,c);
  136. if c<>#10 then
  137. begin
  138. inc(i);
  139. s[i]:=c;
  140. end;
  141. end;
  142. if (i>0) and (s[i]=#13) then
  143. dec(i);
  144. s[0]:=chr(i);
  145. end;
  146. {$endif}
  147. const
  148. bufsize=8192;
  149. var
  150. f : text;
  151. error,multiline : boolean;
  152. line,i,j : longint;
  153. ptxt : pchar;
  154. s,s1 : string;
  155. buf : pointer;
  156. procedure err(const msgstr:string);
  157. begin
  158. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  159. error:=true;
  160. end;
  161. begin
  162. LoadExtern:=false;
  163. getmem(buf,bufsize);
  164. { Read the message file }
  165. assign(f,fn);
  166. {$I-}
  167. reset(f);
  168. {$I+}
  169. if ioresult<>0 then
  170. begin
  171. WriteLn('*** PPC, can not open message file ',fn);
  172. exit;
  173. end;
  174. settextbuf(f,buf^,bufsize);
  175. { First parse the file and count bytes needed }
  176. error:=false;
  177. line:=0;
  178. multiline:=false;
  179. msgsize:=0;
  180. while not eof(f) do
  181. begin
  182. readln(f,s);
  183. inc(line);
  184. if multiline then
  185. begin
  186. if s=']' then
  187. multiline:=false
  188. else
  189. inc(msgsize,length(s)+1); { +1 for linebreak }
  190. end
  191. else
  192. begin
  193. if (s<>'') and not(s[1] in ['#',';','%']) then
  194. begin
  195. i:=pos('=',s);
  196. if i>0 then
  197. begin
  198. j:=i+1;
  199. if not(s[j] in ['0'..'9']) then
  200. err('no number found')
  201. else
  202. begin
  203. while (s[j] in ['0'..'9']) do
  204. inc(j);
  205. end;
  206. if j-i-1<>5 then
  207. err('number length is not 5');
  208. if s[j+1]='[' then
  209. begin
  210. inc(msgsize,j-i);
  211. multiline:=true
  212. end
  213. else
  214. inc(msgsize,length(s)-i+1);
  215. end
  216. else
  217. err('no = found');
  218. end;
  219. end;
  220. end;
  221. if multiline then
  222. err('still in multiline mode');
  223. if error then
  224. begin
  225. freemem(buf,bufsize);
  226. close(f);
  227. exit;
  228. end;
  229. { now read the buffer in mem }
  230. msgallocsize:=msgsize;
  231. getmem(msgtxt,msgallocsize);
  232. ptxt:=msgtxt;
  233. reset(f);
  234. while not eof(f) do
  235. begin
  236. readln(f,s);
  237. if multiline then
  238. begin
  239. if s=']' then
  240. begin
  241. multiline:=false;
  242. { overwrite last eol }
  243. dec(ptxt);
  244. ptxt^:=#0;
  245. inc(ptxt);
  246. end
  247. else
  248. begin
  249. move(s[1],ptxt^,length(s));
  250. inc(ptxt,length(s));
  251. ptxt^:=#10;
  252. inc(ptxt);
  253. end;
  254. end
  255. else
  256. begin
  257. if (s<>'') and not(s[1] in ['#',';','%']) then
  258. begin
  259. i:=pos('=',s);
  260. if i>0 then
  261. begin
  262. j:=i+1;
  263. while (s[j] in ['0'..'9']) do
  264. inc(j);
  265. { multiline start then no txt }
  266. if s[j+1]='[' then
  267. begin
  268. s1:=Copy(s,i+1,j-i);
  269. move(s1[1],ptxt^,length(s1));
  270. inc(ptxt,length(s1));
  271. multiline:=true;
  272. end
  273. else
  274. begin
  275. { txt including number }
  276. s1:=Copy(s,i+1,255);
  277. move(s1[1],ptxt^,length(s1));
  278. inc(ptxt,length(s1));
  279. ptxt^:=#0;
  280. inc(ptxt);
  281. end;
  282. end;
  283. end;
  284. end;
  285. end;
  286. close(f);
  287. freemem(buf,bufsize);
  288. { now we can create the index, clear if the previous load was also
  289. an external file, because those can't be reused }
  290. if not msgintern then
  291. ClearIdx;
  292. CreateIdx;
  293. { set that we've loaded an external file }
  294. msgintern:=false;
  295. LoadExtern:=true;
  296. end;
  297. procedure TMessage.ClearIdx;
  298. var
  299. i : longint;
  300. begin
  301. { clear }
  302. for i:=1 to msgparts do
  303. fillchar(msgidx[i]^,msgidxmax[i]*4,0);
  304. end;
  305. procedure TMessage.CreateIdx;
  306. var
  307. hp1,
  308. hp,hpend : pchar;
  309. code : integer;
  310. num : longint;
  311. number : string[5];
  312. i : longint;
  313. numpart,numidx : longint;
  314. begin
  315. { process msgtxt buffer }
  316. number:='00000';
  317. hp:=msgtxt;
  318. hpend:=@msgtxt[msgsize];
  319. while (hp<hpend) do
  320. begin
  321. hp1:=hp;
  322. for i:=1 to 5 do
  323. begin
  324. number[i]:=hp1^;
  325. inc(hp1);
  326. end;
  327. val(number,num,code);
  328. numpart:=num div 1000;
  329. numidx:=num mod 1000;
  330. { check range }
  331. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  332. begin
  333. { skip _ }
  334. inc(hp1);
  335. { put the address in the idx, the numbers are already checked }
  336. msgidx[numpart]^[numidx]:=hp1;
  337. end;
  338. { next string }
  339. hp:=pchar(@hp[strlen(hp)+1]);
  340. end;
  341. end;
  342. function GetMsgLine(var p:pchar):string;
  343. var
  344. i : longint;
  345. begin
  346. i:=0;
  347. while not(p^ in [#0,#10]) and (i<255) do
  348. begin
  349. inc(i);
  350. GetMsgLine[i]:=p^;
  351. inc(p);
  352. end;
  353. { skip #10 }
  354. if p^=#10 then
  355. inc(p);
  356. { if #0 then set p to nil }
  357. if p^=#0 then
  358. p:=nil;
  359. { return string }
  360. GetMsgLine[0]:=chr(i);
  361. end;
  362. function TMessage.GetPChar(nr:longint):pchar;
  363. begin
  364. GetPChar:=msgidx[nr div 1000]^[nr mod 1000];
  365. end;
  366. function TMessage.Get(nr:longint;const args:array of string):string;
  367. var
  368. hp : pchar;
  369. begin
  370. hp:=msgidx[nr div 1000]^[nr mod 1000];
  371. if hp=nil then
  372. Get:='msg nr '+tostr(nr)
  373. else
  374. Get:=MsgReplace(strpas(hp),args);
  375. end;
  376. end.
  377. {
  378. $Log$
  379. Revision 1.10 2004-02-20 19:49:21 daniel
  380. * Message system uses open arrays internally
  381. * Bugfix for string handling in array constructor node
  382. * Micro code reductions in pdecl.pas
  383. Revision 1.9 2004/01/28 15:36:46 florian
  384. * fixed another couple of arm bugs
  385. Revision 1.8 2003/05/10 23:57:23 florian
  386. * vmtpointer_offset must be adjusted in after_pass1 as well
  387. Revision 1.7 2003/04/22 14:33:38 peter
  388. * removed some notes/hints
  389. Revision 1.6 2002/05/18 13:34:06 peter
  390. * readded missing revisions
  391. Revision 1.5 2002/05/16 19:46:35 carl
  392. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  393. + try to fix temp allocation (still in ifdef)
  394. + generic constructor calls
  395. + start of tassembler / tmodulebase class cleanup
  396. Revision 1.3 2002/04/19 15:41:39 peter
  397. * better replacements that also allow $1 in the replacements without
  398. replacing that instance also
  399. Revision 1.2 2002/03/01 12:41:40 peter
  400. * fixed Message4()
  401. }