cmsgs.pas 11 KB

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