cmsgs.pas 11 KB

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