cmsgs.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements the message object
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cmsgs;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype;
  22. const
  23. maxmsgidxparts = 20;
  24. type
  25. ppchar=^pchar;
  26. TMsgStr = AnsiString;
  27. TArrayOfPChar = array[0..1000] of pchar;
  28. PArrayOfPChar = ^TArrayOfPChar;
  29. TArrayOfState = array[0..1000] of tmsgstate;
  30. PArrayOfState = ^TArrayOfState;
  31. PMessage=^TMessage;
  32. TMessage=object
  33. msgfilename : string;
  34. msgintern : boolean;
  35. msgallocsize,
  36. msgsize,
  37. msgparts,
  38. msgs : longint;
  39. msgtxt : pchar;
  40. msgidx : array[1..maxmsgidxparts] of PArrayOfPChar;
  41. msgidxmax : array[1..maxmsgidxparts] of longint;
  42. msgstates : array[1..maxmsgidxparts] of PArrayOfState;
  43. msgcodepage : TSystemCodePage;
  44. { set if changes with $WARN need to be cleared at next module change }
  45. has_local_changes : boolean;
  46. constructor Init(n:longint;const idxmax:array of longint);
  47. destructor Done;
  48. function LoadIntern(p:pointer;n:longint;cp:TSystemCodePage):boolean;
  49. function LoadExtern(const fn:string):boolean;
  50. procedure ClearIdx;
  51. procedure ResetStates;
  52. procedure CreateIdx;
  53. { function ClearVerbosity(nr:longint):boolean; not used anymore }
  54. function SetVerbosity(nr:longint;newstate:tmsgstate):boolean;
  55. function Get(nr:longint;const args:array of TMsgStr):TMsgStr;
  56. function Valid(nr:longint):boolean;
  57. end;
  58. { this will read a line until #10 or #0 and also increase p }
  59. function GetMsgLine(var p:pchar):string;
  60. implementation
  61. uses
  62. SysUtils,
  63. cutils;
  64. function MsgReplace(const s:TMsgStr;const args:array of TMsgStr):TMsgStr;
  65. var
  66. last,
  67. i : longint;
  68. hs : TMsgStr;
  69. begin
  70. if s='' then
  71. begin
  72. MsgReplace:='';
  73. exit;
  74. end;
  75. hs:='';
  76. i:=0;
  77. last:=0;
  78. while (i<length(s)-1) do
  79. begin
  80. inc(i);
  81. if (s[i]='$') and (s[i+1] in ['1'..'9']) then
  82. begin
  83. hs:=hs+copy(s,last+1,i-last-1)+args[byte(s[i+1])-byte('1')];
  84. inc(i);
  85. last:=i;
  86. end;
  87. end;
  88. MsgReplace:=hs+copy(s,last+1,length(s)-last);
  89. end;
  90. constructor TMessage.Init(n:longint;const idxmax:array of longint);
  91. var
  92. i,j : longint;
  93. begin
  94. msgtxt:=nil;
  95. has_local_changes:=false;
  96. msgsize:=0;
  97. msgparts:=n;
  98. msgcodepage:=CP_ACP;
  99. if n<>high(idxmax)+1 then
  100. fail;
  101. for i:=1 to n do
  102. begin
  103. msgidxmax[i]:=idxmax[i-1];
  104. { create array of msgidx }
  105. getmem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  106. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  107. { create array of states }
  108. getmem(msgstates[i],msgidxmax[i]*sizeof(tmsgstate));
  109. { default value for msgstate is ms_on_global }
  110. for j:=0 to msgidxmax[i]-1 do
  111. msgstates[i]^[j]:=ms_on_global;
  112. end;
  113. end;
  114. destructor TMessage.Done;
  115. var
  116. i : longint;
  117. begin
  118. for i:=1 to msgparts do
  119. begin
  120. freemem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  121. freemem(msgstates[i],msgidxmax[i]*sizeof(tmsgstate));
  122. end;
  123. if msgallocsize>0 then
  124. begin
  125. freemem(msgtxt,msgsize);
  126. msgallocsize:=0;
  127. end;
  128. msgtxt:=nil;
  129. msgsize:=0;
  130. msgparts:=0;
  131. end;
  132. function TMessage.LoadIntern(p:pointer;n:longint;cp:TSystemCodePage):boolean;
  133. begin
  134. msgcodepage:=cp;
  135. msgtxt:=pchar(p);
  136. msgsize:=n;
  137. msgallocsize:=0;
  138. msgintern:=true;
  139. ClearIdx;
  140. CreateIdx;
  141. LoadIntern:=true;
  142. end;
  143. function TMessage.LoadExtern(const fn:string):boolean;
  144. const
  145. bufsize=8192;
  146. var
  147. f : text;
  148. error,multiline : boolean;
  149. line,i,j : longint;
  150. ptxt : pchar;
  151. s,s1 : string;
  152. buf : pointer;
  153. procedure err(const msgstr:TMsgStr);
  154. begin
  155. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  156. error:=true;
  157. end;
  158. begin
  159. LoadExtern:=false;
  160. msgcodepage:=CP_ACP;
  161. getmem(buf,bufsize);
  162. { Read the message file }
  163. assign(f,fn);
  164. {$push}{$I-}
  165. reset(f);
  166. {$pop}
  167. if ioresult<>0 then
  168. begin
  169. WriteLn('*** PPC, can not open message file ',fn);
  170. exit;
  171. end;
  172. settextbuf(f,buf^,bufsize);
  173. { First parse the file and count bytes needed }
  174. error:=false;
  175. line:=0;
  176. multiline:=false;
  177. msgsize:=0;
  178. while not eof(f) do
  179. begin
  180. readln(f,s);
  181. inc(line);
  182. if multiline then
  183. begin
  184. if s=']' then
  185. multiline:=false
  186. else
  187. inc(msgsize,length(s)+1); { +1 for linebreak }
  188. end
  189. else
  190. begin
  191. if (s<>'') and not(s[1] in ['#',';','%']) then
  192. begin
  193. i:=pos('=',s);
  194. if i>0 then
  195. begin
  196. j:=i+1;
  197. if not(s[j] in ['0'..'9']) then
  198. err('no number found')
  199. else
  200. begin
  201. while (s[j] in ['0'..'9']) do
  202. inc(j);
  203. end;
  204. if j-i-1<>5 then
  205. err('number length is not 5');
  206. if s[j+1]='[' then
  207. begin
  208. inc(msgsize,j-i);
  209. multiline:=true
  210. end
  211. else
  212. inc(msgsize,length(s)-i+1);
  213. end
  214. else
  215. err('no = found');
  216. end
  217. else if (Length(s)>11) and (Copy(s,1,11)='# CodePage ') then
  218. begin
  219. msgcodepage:=StrToInt(Copy(s,12,Length(s)-11));
  220. end;
  221. end;
  222. end;
  223. if multiline then
  224. err('still in multiline mode');
  225. if error then
  226. begin
  227. freemem(buf,bufsize);
  228. close(f);
  229. exit;
  230. end;
  231. { now read the buffer in mem }
  232. msgallocsize:=msgsize;
  233. getmem(msgtxt,msgallocsize);
  234. ptxt:=msgtxt;
  235. reset(f);
  236. while not eof(f) do
  237. begin
  238. readln(f,s);
  239. if multiline then
  240. begin
  241. if s=']' then
  242. begin
  243. multiline:=false;
  244. { overwrite last eol }
  245. dec(ptxt);
  246. ptxt^:=#0;
  247. inc(ptxt);
  248. end
  249. else
  250. begin
  251. move(s[1],ptxt^,length(s));
  252. inc(ptxt,length(s));
  253. ptxt^:=#10;
  254. inc(ptxt);
  255. end;
  256. end
  257. else
  258. begin
  259. if (s<>'') and not(s[1] in ['#',';','%']) then
  260. begin
  261. i:=pos('=',s);
  262. if i>0 then
  263. begin
  264. j:=i+1;
  265. while (s[j] in ['0'..'9']) do
  266. inc(j);
  267. { multiline start then no txt }
  268. if s[j+1]='[' then
  269. begin
  270. s1:=Copy(s,i+1,j-i);
  271. move(s1[1],ptxt^,length(s1));
  272. inc(ptxt,length(s1));
  273. multiline:=true;
  274. end
  275. else
  276. begin
  277. { txt including number }
  278. s1:=Copy(s,i+1,255);
  279. move(s1[1],ptxt^,length(s1));
  280. inc(ptxt,length(s1));
  281. ptxt^:=#0;
  282. inc(ptxt);
  283. end;
  284. end;
  285. end;
  286. end;
  287. end;
  288. close(f);
  289. freemem(buf,bufsize);
  290. { now we can create the index, clear if the previous load was also
  291. an external file, because those can't be reused }
  292. if not msgintern then
  293. ClearIdx;
  294. CreateIdx;
  295. { set that we've loaded an external file }
  296. msgintern:=false;
  297. LoadExtern:=true;
  298. end;
  299. procedure TMessage.ClearIdx;
  300. var
  301. i : longint;
  302. begin
  303. { clear }
  304. for i:=1 to msgparts do
  305. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  306. end;
  307. procedure TMessage.CreateIdx;
  308. var
  309. hp1,
  310. hp,hpend : pchar;
  311. code : integer;
  312. num : longint;
  313. number : string[5];
  314. i : longint;
  315. numpart,numidx : longint;
  316. begin
  317. { process msgtxt buffer }
  318. number:='00000';
  319. hp:=msgtxt;
  320. hpend:=@msgtxt[msgsize];
  321. while (hp<hpend) do
  322. begin
  323. hp1:=hp;
  324. for i:=1 to 5 do
  325. begin
  326. number[i]:=hp1^;
  327. inc(hp1);
  328. end;
  329. val(number,num,code);
  330. numpart:=num div 1000;
  331. numidx:=num mod 1000;
  332. { check range }
  333. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  334. begin
  335. { skip _ }
  336. inc(hp1);
  337. { set default verbosity to off is '-' is found just after the '_' }
  338. if hp1^='-' then
  339. begin
  340. msgstates[numpart]^[numidx]:=ms_off_global;
  341. inc(hp1);
  342. end;
  343. { put the address in the idx, the numbers are already checked }
  344. msgidx[numpart]^[numidx]:=hp1;
  345. end;
  346. { next string }
  347. hp:=pchar(@hp[strlen(hp)+1]);
  348. end;
  349. end;
  350. function GetMsgLine(var p:pchar):string;
  351. var
  352. i : longint;
  353. begin
  354. i:=0;
  355. while not(p^ in [#0,#10]) and (i<256) do
  356. begin
  357. inc(i);
  358. GetMsgLine[i]:=p^;
  359. inc(p);
  360. end;
  361. { skip #10 }
  362. if p^=#10 then
  363. inc(p);
  364. { if #0 then set p to nil }
  365. if p^=#0 then
  366. p:=nil;
  367. { return string }
  368. GetMsgLine[0]:=chr(i);
  369. end;
  370. function TMessage.SetVerbosity(nr:longint;newstate:tmsgstate):boolean;
  371. var
  372. i: longint;
  373. oldstate : tmsgstate;
  374. is_global : boolean;
  375. begin
  376. result:=false;
  377. i:=nr div 1000;
  378. if (i < low(msgstates)) or
  379. (i > msgparts) then
  380. exit;
  381. if (nr mod 1000 < msgidxmax[i]) then
  382. begin
  383. is_global:=(ord(newstate) and ms_global_mask) <> 0;
  384. oldstate:=msgstates[i]^[nr mod 1000];
  385. if not is_global then
  386. newstate:= tmsgstate((ord(newstate) and ms_local_mask) or (ord(oldstate) and ms_global_mask));
  387. if newstate<>oldstate then
  388. has_local_changes:=true;
  389. msgstates[i]^[nr mod 1000]:=newstate;
  390. result:=true;
  391. end;
  392. end;
  393. {
  394. function TMessage.ClearVerbosity(nr:longint):boolean;
  395. begin
  396. ClearVerbosity:=SetVerbosity(nr,ms_off);
  397. end;
  398. }
  399. function TMessage.Get(nr:longint;const args:array of TMsgStr):TMsgStr;
  400. var
  401. hp : pchar;
  402. s: TMsgStr;
  403. begin
  404. if (nr div 1000 < msgparts) and
  405. (nr mod 1000 < msgidxmax[nr div 1000]) then
  406. hp:=msgidx[nr div 1000]^[nr mod 1000]
  407. else
  408. hp:=nil;
  409. if hp=nil then
  410. Get:='msg nr '+tostr(nr)
  411. else
  412. begin
  413. s:=sysutils.StrPas(hp);
  414. {$ifdef cpawaremessages}
  415. SetCodePage(RawByteString(s),msgcodepage,False);
  416. SetCodePage(RawByteString(s),CP_ACP,True);
  417. {$endif cpawaremessages}
  418. Get:=MsgReplace(s,args);
  419. end;
  420. end;
  421. function TMessage.Valid(nr:longint):boolean;
  422. var
  423. i,j : longint;
  424. begin
  425. i:=nr div 1000;
  426. j:=nr mod 1000;
  427. result:=(i>=low(msgstates)) and (i<msgparts) and (j<msgidxmax[i]) and assigned(msgidx[i]^[j]);
  428. end;
  429. procedure TMessage.ResetStates;
  430. var
  431. i,j,glob : longint;
  432. state : tmsgstate;
  433. begin
  434. if not has_local_changes then
  435. exit;
  436. for i:=1 to msgparts do
  437. for j:=0 to msgidxmax[i] - 1 do
  438. begin
  439. state:=msgstates[i]^[j];
  440. glob:=(ord(state) and ms_global_mask) shr ms_shift;
  441. state:=tmsgstate((glob shl ms_shift) or glob);
  442. msgstates[i]^[j]:=state;
  443. end;
  444. has_local_changes:=false;
  445. end;
  446. end.