messages.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 Messages;
  19. {$i defines.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):string;
  46. function Get3(nr:longint;const s1,s2,s3:string):string;
  47. function Get2(nr:longint;const s1,s2:string):string;
  48. function Get1(nr:longint;const s1:string):string;
  49. end;
  50. { this will read a line until #10 or #0 and also increase p }
  51. function GetMsgLine(var p:pchar):string;
  52. implementation
  53. uses
  54. cutils,
  55. globals,
  56. {$ifdef DELPHI}
  57. sysutils;
  58. {$else DELPHI}
  59. strings;
  60. {$endif DELPHI}
  61. constructor TMessage.Init(n:longint;const idxmax:array of longint);
  62. var
  63. i : longint;
  64. begin
  65. msgtxt:=nil;
  66. msgsize:=0;
  67. msgparts:=n;
  68. if n<>high(idxmax)+1 then
  69. fail;
  70. for i:=1to n do
  71. begin
  72. msgidxmax[i]:=idxmax[i-1];
  73. getmem(msgidx[i],msgidxmax[i]*4);
  74. fillchar(msgidx[i]^,msgidxmax[i]*4,0);
  75. end;
  76. end;
  77. destructor TMessage.Done;
  78. var
  79. i : longint;
  80. begin
  81. for i:=1to msgparts do
  82. freemem(msgidx[i],msgidxmax[i]*4);
  83. if msgallocsize>0 then
  84. begin
  85. freemem(msgtxt,msgsize);
  86. msgallocsize:=0;
  87. end;
  88. msgtxt:=nil;
  89. msgsize:=0;
  90. msgparts:=0;
  91. end;
  92. function TMessage.LoadIntern(p:pointer;n:longint):boolean;
  93. begin
  94. msgtxt:=pchar(p);
  95. msgsize:=n;
  96. msgallocsize:=0;
  97. msgintern:=true;
  98. ClearIdx;
  99. CreateIdx;
  100. LoadIntern:=true;
  101. end;
  102. function TMessage.LoadExtern(const fn:string):boolean;
  103. {$ifndef FPC}
  104. procedure readln(var t:text;var s:string);
  105. var
  106. c : char;
  107. i : longint;
  108. begin
  109. c:=#0;
  110. i:=0;
  111. while (not eof(t)) and (c<>#10) do
  112. begin
  113. read(t,c);
  114. if c<>#10 then
  115. begin
  116. inc(i);
  117. s[i]:=c;
  118. end;
  119. end;
  120. if (i>0) and (s[i]=#13) then
  121. dec(i);
  122. s[0]:=chr(i);
  123. end;
  124. {$endif}
  125. const
  126. bufsize=8192;
  127. var
  128. f : text;
  129. error,multiline : boolean;
  130. line,i,j : longint;
  131. ptxt : pchar;
  132. s,s1 : string;
  133. buf : pointer;
  134. procedure err(const msgstr:string);
  135. begin
  136. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  137. error:=true;
  138. end;
  139. begin
  140. LoadExtern:=false;
  141. getmem(buf,bufsize);
  142. {Read the message file}
  143. assign(f,fn);
  144. {$I-}
  145. reset(f);
  146. {$I+}
  147. if ioresult<>0 then
  148. begin
  149. WriteLn('*** PPC, can not open message file ',fn);
  150. exit;
  151. end;
  152. settextbuf(f,buf^,bufsize);
  153. { First parse the file and count bytes needed }
  154. error:=false;
  155. line:=0;
  156. multiline:=false;
  157. msgsize:=0;
  158. while not eof(f) do
  159. begin
  160. readln(f,s);
  161. inc(line);
  162. if multiline then
  163. begin
  164. if s=']' then
  165. multiline:=false
  166. else
  167. inc(msgsize,length(s)+1); { +1 for linebreak }
  168. end
  169. else
  170. begin
  171. if (s<>'') and not(s[1] in ['#',';','%']) then
  172. begin
  173. i:=pos('=',s);
  174. if i>0 then
  175. begin
  176. j:=i+1;
  177. if not(s[j] in ['0'..'9']) then
  178. err('no number found')
  179. else
  180. begin
  181. while (s[j] in ['0'..'9']) do
  182. inc(j);
  183. end;
  184. if j-i-1<>5 then
  185. err('number length is not 5');
  186. if s[j+1]='[' then
  187. begin
  188. inc(msgsize,j-i);
  189. multiline:=true
  190. end
  191. else
  192. inc(msgsize,length(s)-i+1);
  193. end
  194. else
  195. err('no = found');
  196. end;
  197. end;
  198. end;
  199. if multiline then
  200. err('still in multiline mode');
  201. if error then
  202. begin
  203. freemem(buf,bufsize);
  204. close(f);
  205. exit;
  206. end;
  207. { now read the buffer in mem }
  208. msgallocsize:=msgsize;
  209. getmem(msgtxt,msgallocsize);
  210. ptxt:=msgtxt;
  211. reset(f);
  212. while not eof(f) do
  213. begin
  214. readln(f,s);
  215. if multiline then
  216. begin
  217. if s=']' then
  218. begin
  219. multiline:=false;
  220. { overwrite last eol }
  221. dec(ptxt);
  222. ptxt^:=#0;
  223. inc(ptxt);
  224. end
  225. else
  226. begin
  227. move(s[1],ptxt^,length(s));
  228. inc(ptxt,length(s));
  229. ptxt^:=#10;
  230. inc(ptxt);
  231. end;
  232. end
  233. else
  234. begin
  235. if (s<>'') and not(s[1] in ['#',';','%']) then
  236. begin
  237. i:=pos('=',s);
  238. if i>0 then
  239. begin
  240. j:=i+1;
  241. while (s[j] in ['0'..'9']) do
  242. inc(j);
  243. { multiline start then no txt }
  244. if s[j+1]='[' then
  245. begin
  246. s1:=Copy(s,i+1,j-i);
  247. move(s1[1],ptxt^,length(s1));
  248. inc(ptxt,length(s1));
  249. multiline:=true;
  250. end
  251. else
  252. begin
  253. { txt including number }
  254. s1:=Copy(s,i+1,255);
  255. move(s1[1],ptxt^,length(s1));
  256. inc(ptxt,length(s1));
  257. ptxt^:=#0;
  258. inc(ptxt);
  259. end;
  260. end;
  261. end;
  262. end;
  263. end;
  264. close(f);
  265. freemem(buf,bufsize);
  266. { now we can create the index, clear if the previous load was also
  267. an external file, because those can't be reused }
  268. if not msgintern then
  269. ClearIdx;
  270. CreateIdx;
  271. { set that we've loaded an external file }
  272. msgintern:=false;
  273. LoadExtern:=true;
  274. end;
  275. procedure TMessage.ClearIdx;
  276. var
  277. i : longint;
  278. begin
  279. { clear }
  280. for i:=1to msgparts do
  281. fillchar(msgidx[i]^,msgidxmax[i]*4,0);
  282. end;
  283. procedure TMessage.CreateIdx;
  284. var
  285. hp1,
  286. hp,hpend : pchar;
  287. code : integer;
  288. num : longint;
  289. number : string[5];
  290. i : longint;
  291. numpart,numidx : longint;
  292. begin
  293. { process msgtxt buffer }
  294. number:='00000';
  295. hp:=msgtxt;
  296. hpend:=@msgtxt[msgsize];
  297. while (hp<hpend) do
  298. begin
  299. hp1:=hp;
  300. for i:=1to 5 do
  301. begin
  302. number[i]:=hp1^;
  303. inc(hp1);
  304. end;
  305. val(number,num,code);
  306. numpart:=num div 1000;
  307. numidx:=num mod 1000;
  308. { check range }
  309. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  310. begin
  311. { skip _ }
  312. inc(hp1);
  313. { put the address in the idx, the numbers are already checked }
  314. msgidx[numpart]^[numidx]:=hp1;
  315. end;
  316. { next string }
  317. hp:=pchar(@hp[strlen(hp)+1]);
  318. end;
  319. end;
  320. function GetMsgLine(var p:pchar):string;
  321. var
  322. i : longint;
  323. begin
  324. i:=0;
  325. while not(p^ in [#0,#10]) and (i<255) do
  326. begin
  327. inc(i);
  328. GetMsgLine[i]:=p^;
  329. inc(p);
  330. end;
  331. { skip #10 }
  332. if p^=#10 then
  333. inc(p);
  334. { if #0 then set p to nil }
  335. if p^=#0 then
  336. p:=nil;
  337. { return string }
  338. GetMsgLine[0]:=chr(i);
  339. end;
  340. function TMessage.GetPChar(nr:longint):pchar;
  341. begin
  342. GetPChar:=msgidx[nr div 1000]^[nr mod 1000];
  343. end;
  344. function TMessage.Get(nr:longint):string;
  345. var
  346. s : string[16];
  347. hp : pchar;
  348. begin
  349. hp:=msgidx[nr div 1000]^[nr mod 1000];
  350. if hp=nil then
  351. begin
  352. Str(nr,s);
  353. Get:='msg nr '+s;
  354. end
  355. else
  356. Get:=StrPas(hp);
  357. end;
  358. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  359. var
  360. s : string;
  361. begin
  362. s:=Get(nr);
  363. Replace(s,'$1',s1);
  364. Replace(s,'$2',s2);
  365. Replace(s,'$3',s3);
  366. Get3:=s;
  367. end;
  368. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  369. var
  370. s : string;
  371. begin
  372. s:=Get(nr);
  373. Replace(s,'$1',s1);
  374. Replace(s,'$2',s2);
  375. Get2:=s;
  376. end;
  377. function TMessage.Get1(nr:longint;const s1:string):string;
  378. var
  379. s : string;
  380. begin
  381. s:=Get(nr);
  382. Replace(s,'$1',s1);
  383. Get1:=s;
  384. end;
  385. end.
  386. {
  387. $Log$
  388. Revision 1.8 2001-04-21 13:32:07 peter
  389. * remove endless loop with replacements (merged)
  390. Revision 1.7 2001/04/14 16:05:41 jonas
  391. * allow a single replacement string to be substituted more than once per
  392. message (already used in assembler reader messages for "fsub x" etc.
  393. transformations) (merged)
  394. Revision 1.6 2001/03/10 13:19:10 peter
  395. * don't check messagefile for numbers, this allows the usage of
  396. 1.1 msgfiles with a 1.0.x compiler
  397. Revision 1.5 2000/11/29 00:30:31 florian
  398. * unused units removed from uses clause
  399. * some changes for widestrings
  400. Revision 1.4 2000/09/24 21:33:46 peter
  401. * message updates merges
  402. Revision 1.3 2000/09/24 15:06:18 peter
  403. * use defines.inc
  404. Revision 1.2 2000/07/13 11:32:43 michael
  405. + removed logs
  406. }