messages.pas 8.9 KB

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