messages.pas 9.5 KB

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