cmsgs.pas 11 KB

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