messages.pas 9.9 KB

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