messages.pas 9.0 KB

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