messages.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. line,i,j : longint;
  130. ptxt : pchar;
  131. s,s1 : string;
  132. buf : pointer;
  133. procedure err(const msgstr:string);
  134. begin
  135. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  136. error:=true;
  137. end;
  138. begin
  139. LoadExtern:=false;
  140. getmem(buf,bufsize);
  141. {Read the message file}
  142. assign(f,fn);
  143. {$I-}
  144. reset(f);
  145. {$I+}
  146. if ioresult<>0 then
  147. begin
  148. WriteLn('*** PPC, can not open message file ',fn);
  149. exit;
  150. end;
  151. settextbuf(f,buf^,bufsize);
  152. { First parse the file and count bytes needed }
  153. error:=false;
  154. line:=0;
  155. multiline:=false;
  156. msgsize:=0;
  157. while not eof(f) do
  158. begin
  159. readln(f,s);
  160. inc(line);
  161. if multiline then
  162. begin
  163. if s=']' then
  164. multiline:=false
  165. else
  166. inc(msgsize,length(s)+1); { +1 for linebreak }
  167. end
  168. else
  169. begin
  170. if (s<>'') and not(s[1] in ['#',';','%']) then
  171. begin
  172. i:=pos('=',s);
  173. if i>0 then
  174. begin
  175. j:=i+1;
  176. if not(s[j] in ['0'..'9']) then
  177. err('no number found')
  178. else
  179. begin
  180. while (s[j] in ['0'..'9']) do
  181. inc(j);
  182. end;
  183. if j-i-1<>5 then
  184. err('number length is not 5');
  185. if s[j+1]='[' then
  186. begin
  187. inc(msgsize,j-i);
  188. multiline:=true
  189. end
  190. else
  191. inc(msgsize,length(s)-i+1);
  192. end
  193. else
  194. err('no = found');
  195. end;
  196. end;
  197. end;
  198. if multiline then
  199. err('still in multiline mode');
  200. if error then
  201. begin
  202. freemem(buf,bufsize);
  203. close(f);
  204. exit;
  205. end;
  206. { now read the buffer in mem }
  207. msgallocsize:=msgsize;
  208. getmem(msgtxt,msgallocsize);
  209. ptxt:=msgtxt;
  210. reset(f);
  211. while not eof(f) do
  212. begin
  213. readln(f,s);
  214. if multiline then
  215. begin
  216. if s=']' then
  217. begin
  218. multiline:=false;
  219. { overwrite last eol }
  220. dec(ptxt);
  221. ptxt^:=#0;
  222. inc(ptxt);
  223. end
  224. else
  225. begin
  226. move(s[1],ptxt^,length(s));
  227. inc(ptxt,length(s));
  228. ptxt^:=#10;
  229. inc(ptxt);
  230. end;
  231. end
  232. else
  233. begin
  234. if (s<>'') and not(s[1] in ['#',';','%']) then
  235. begin
  236. i:=pos('=',s);
  237. if i>0 then
  238. begin
  239. j:=i+1;
  240. while (s[j] in ['0'..'9']) do
  241. inc(j);
  242. { multiline start then no txt }
  243. if s[j+1]='[' then
  244. begin
  245. s1:=Copy(s,i+1,j-i);
  246. move(s1[1],ptxt^,length(s1));
  247. inc(ptxt,length(s1));
  248. multiline:=true;
  249. end
  250. else
  251. begin
  252. { txt including number }
  253. s1:=Copy(s,i+1,255);
  254. move(s1[1],ptxt^,length(s1));
  255. inc(ptxt,length(s1));
  256. ptxt^:=#0;
  257. inc(ptxt);
  258. end;
  259. end;
  260. end;
  261. end;
  262. end;
  263. close(f);
  264. freemem(buf,bufsize);
  265. { now we can create the index, clear if the previous load was also
  266. an external file, because those can't be reused }
  267. if not msgintern then
  268. ClearIdx;
  269. CreateIdx;
  270. { set that we've loaded an external file }
  271. msgintern:=false;
  272. LoadExtern:=true;
  273. end;
  274. procedure TMessage.ClearIdx;
  275. var
  276. i : longint;
  277. begin
  278. { clear }
  279. for i:=1to msgparts do
  280. fillchar(msgidx[i]^,msgidxmax[i]*4,0);
  281. end;
  282. procedure TMessage.CreateIdx;
  283. var
  284. hp1,
  285. hp,hpend : pchar;
  286. code : integer;
  287. num : longint;
  288. number : string[5];
  289. i : longint;
  290. numpart,numidx : longint;
  291. begin
  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. { check range }
  308. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  309. begin
  310. { skip _ }
  311. inc(hp1);
  312. { put the address in the idx, the numbers are already checked }
  313. msgidx[numpart]^[numidx]:=hp1;
  314. end;
  315. { next string }
  316. hp:=pchar(@hp[strlen(hp)+1]);
  317. end;
  318. end;
  319. function GetMsgLine(var p:pchar):string;
  320. var
  321. i : longint;
  322. begin
  323. i:=0;
  324. while not(p^ in [#0,#10]) and (i<255) do
  325. begin
  326. inc(i);
  327. GetMsgLine[i]:=p^;
  328. inc(p);
  329. end;
  330. { skip #10 }
  331. if p^=#10 then
  332. inc(p);
  333. { if #0 then set p to nil }
  334. if p^=#0 then
  335. p:=nil;
  336. { return string }
  337. GetMsgLine[0]:=chr(i);
  338. end;
  339. function TMessage.GetPChar(nr:longint):pchar;
  340. begin
  341. GetPChar:=msgidx[nr div 1000]^[nr mod 1000];
  342. end;
  343. function TMessage.Get(nr:longint):string;
  344. var
  345. s : string[16];
  346. hp : pchar;
  347. begin
  348. hp:=msgidx[nr div 1000]^[nr mod 1000];
  349. if hp=nil then
  350. begin
  351. Str(nr,s);
  352. Get:='msg nr '+s;
  353. end
  354. else
  355. Get:=StrPas(hp);
  356. end;
  357. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  358. var
  359. i : longint;
  360. s : string;
  361. begin
  362. s:=Get(nr);
  363. { $1 -> s1 }
  364. i:=pos('$1',s);
  365. if i>0 then
  366. begin
  367. Delete(s,i,2);
  368. Insert(s1,s,i);
  369. end;
  370. { $2 -> s2 }
  371. i:=pos('$2',s);
  372. if i>0 then
  373. begin
  374. Delete(s,i,2);
  375. Insert(s2,s,i);
  376. end;
  377. { $3 -> s3 }
  378. i:=pos('$3',s);
  379. if i>0 then
  380. begin
  381. Delete(s,i,2);
  382. Insert(s3,s,i);
  383. end;
  384. Get3:=s;
  385. end;
  386. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  387. begin
  388. Get2:=Get3(nr,s1,s2,'');
  389. end;
  390. function TMessage.Get1(nr:longint;const s1:string):string;
  391. begin
  392. Get1:=Get3(nr,s1,'','');
  393. end;
  394. end.
  395. {
  396. $Log$
  397. Revision 1.6 2001-03-10 13:19:10 peter
  398. * don't check messagefile for numbers, this allows the usage of
  399. 1.1 msgfiles with a 1.0.x compiler
  400. Revision 1.5 2000/11/29 00:30:31 florian
  401. * unused units removed from uses clause
  402. * some changes for widestrings
  403. Revision 1.4 2000/09/24 21:33:46 peter
  404. * message updates merges
  405. Revision 1.3 2000/09/24 15:06:18 peter
  406. * use defines.inc
  407. Revision 1.2 2000/07/13 11:32:43 michael
  408. + removed logs
  409. }