cmsgs.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. { set if changes with $WARN need to be cleared at next module change }
  44. has_local_changes : boolean;
  45. constructor Init(n:longint;const idxmax:array of longint);
  46. destructor Done;
  47. function LoadIntern(p:pointer;n:longint):boolean;
  48. function LoadExtern(const fn:string):boolean;
  49. procedure ClearIdx;
  50. procedure ResetStates;
  51. procedure CreateIdx;
  52. function GetPChar(nr:longint):pchar;
  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):ansistring;
  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):ansistring;
  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. if n<>high(idxmax)+1 then
  98. fail;
  99. for i:=1 to n do
  100. begin
  101. msgidxmax[i]:=idxmax[i-1];
  102. { create array of msgidx }
  103. getmem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  104. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  105. { create array of states }
  106. getmem(msgstates[i],msgidxmax[i]*sizeof(tmsgstate));
  107. { default value for msgstate is ms_on_global }
  108. for j:=0 to msgidxmax[i]-1 do
  109. msgstates[i]^[j]:=ms_on_global;
  110. end;
  111. end;
  112. destructor TMessage.Done;
  113. var
  114. i : longint;
  115. begin
  116. for i:=1 to msgparts do
  117. begin
  118. freemem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  119. freemem(msgstates[i],msgidxmax[i]*sizeof(tmsgstate));
  120. end;
  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. const
  142. bufsize=8192;
  143. var
  144. f : text;
  145. error,multiline : boolean;
  146. line,i,j : longint;
  147. ptxt : pchar;
  148. s,s1 : string;
  149. buf : pointer;
  150. procedure err(const msgstr:TMsgStr);
  151. begin
  152. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  153. error:=true;
  154. end;
  155. begin
  156. LoadExtern:=false;
  157. getmem(buf,bufsize);
  158. { Read the message file }
  159. assign(f,fn);
  160. {$push}{$I-}
  161. reset(f);
  162. {$pop}
  163. if ioresult<>0 then
  164. begin
  165. WriteLn('*** PPC, can not open message file ',fn);
  166. exit;
  167. end;
  168. settextbuf(f,buf^,bufsize);
  169. { First parse the file and count bytes needed }
  170. error:=false;
  171. line:=0;
  172. multiline:=false;
  173. msgsize:=0;
  174. while not eof(f) do
  175. begin
  176. readln(f,s);
  177. inc(line);
  178. if multiline then
  179. begin
  180. if s=']' then
  181. multiline:=false
  182. else
  183. inc(msgsize,length(s)+1); { +1 for linebreak }
  184. end
  185. else
  186. begin
  187. if (s<>'') and not(s[1] in ['#',';','%']) then
  188. begin
  189. i:=pos('=',s);
  190. if i>0 then
  191. begin
  192. j:=i+1;
  193. if not(s[j] in ['0'..'9']) then
  194. err('no number found')
  195. else
  196. begin
  197. while (s[j] in ['0'..'9']) do
  198. inc(j);
  199. end;
  200. if j-i-1<>5 then
  201. err('number length is not 5');
  202. if s[j+1]='[' then
  203. begin
  204. inc(msgsize,j-i);
  205. multiline:=true
  206. end
  207. else
  208. inc(msgsize,length(s)-i+1);
  209. end
  210. else
  211. err('no = found');
  212. end;
  213. end;
  214. end;
  215. if multiline then
  216. err('still in multiline mode');
  217. if error then
  218. begin
  219. freemem(buf,bufsize);
  220. close(f);
  221. exit;
  222. end;
  223. { now read the buffer in mem }
  224. msgallocsize:=msgsize;
  225. getmem(msgtxt,msgallocsize);
  226. ptxt:=msgtxt;
  227. reset(f);
  228. while not eof(f) do
  229. begin
  230. readln(f,s);
  231. if multiline then
  232. begin
  233. if s=']' then
  234. begin
  235. multiline:=false;
  236. { overwrite last eol }
  237. dec(ptxt);
  238. ptxt^:=#0;
  239. inc(ptxt);
  240. end
  241. else
  242. begin
  243. move(s[1],ptxt^,length(s));
  244. inc(ptxt,length(s));
  245. ptxt^:=#10;
  246. inc(ptxt);
  247. end;
  248. end
  249. else
  250. begin
  251. if (s<>'') and not(s[1] in ['#',';','%']) then
  252. begin
  253. i:=pos('=',s);
  254. if i>0 then
  255. begin
  256. j:=i+1;
  257. while (s[j] in ['0'..'9']) do
  258. inc(j);
  259. { multiline start then no txt }
  260. if s[j+1]='[' then
  261. begin
  262. s1:=Copy(s,i+1,j-i);
  263. move(s1[1],ptxt^,length(s1));
  264. inc(ptxt,length(s1));
  265. multiline:=true;
  266. end
  267. else
  268. begin
  269. { txt including number }
  270. s1:=Copy(s,i+1,255);
  271. move(s1[1],ptxt^,length(s1));
  272. inc(ptxt,length(s1));
  273. ptxt^:=#0;
  274. inc(ptxt);
  275. end;
  276. end;
  277. end;
  278. end;
  279. end;
  280. close(f);
  281. freemem(buf,bufsize);
  282. { now we can create the index, clear if the previous load was also
  283. an external file, because those can't be reused }
  284. if not msgintern then
  285. ClearIdx;
  286. CreateIdx;
  287. { set that we've loaded an external file }
  288. msgintern:=false;
  289. LoadExtern:=true;
  290. end;
  291. procedure TMessage.ClearIdx;
  292. var
  293. i : longint;
  294. begin
  295. { clear }
  296. for i:=1 to msgparts do
  297. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  298. end;
  299. procedure TMessage.CreateIdx;
  300. var
  301. hp1,
  302. hp,hpend : pchar;
  303. code : integer;
  304. num : longint;
  305. number : string[5];
  306. i : longint;
  307. numpart,numidx : longint;
  308. begin
  309. { process msgtxt buffer }
  310. number:='00000';
  311. hp:=msgtxt;
  312. hpend:=@msgtxt[msgsize];
  313. while (hp<hpend) do
  314. begin
  315. hp1:=hp;
  316. for i:=1 to 5 do
  317. begin
  318. number[i]:=hp1^;
  319. inc(hp1);
  320. end;
  321. val(number,num,code);
  322. numpart:=num div 1000;
  323. numidx:=num mod 1000;
  324. { check range }
  325. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  326. begin
  327. { skip _ }
  328. inc(hp1);
  329. { set default verbosity to off is '-' is found just after the '_' }
  330. if hp1^='-' then
  331. begin
  332. msgstates[numpart]^[numidx]:=ms_off_global;
  333. inc(hp1);
  334. end;
  335. { put the address in the idx, the numbers are already checked }
  336. msgidx[numpart]^[numidx]:=hp1;
  337. end;
  338. { next string }
  339. hp:=pchar(@hp[strlen(hp)+1]);
  340. end;
  341. end;
  342. function GetMsgLine(var p:pchar):string;
  343. var
  344. i : longint;
  345. begin
  346. i:=0;
  347. while not(p^ in [#0,#10]) and (i<256) do
  348. begin
  349. inc(i);
  350. GetMsgLine[i]:=p^;
  351. inc(p);
  352. end;
  353. { skip #10 }
  354. if p^=#10 then
  355. inc(p);
  356. { if #0 then set p to nil }
  357. if p^=#0 then
  358. p:=nil;
  359. { return string }
  360. GetMsgLine[0]:=chr(i);
  361. end;
  362. function TMessage.GetPChar(nr:longint):pchar;
  363. begin
  364. if (nr div 1000 < msgparts) and
  365. (nr mod 1000 < msgidxmax[nr div 1000]) then
  366. GetPChar:=msgidx[nr div 1000]^[nr mod 1000]
  367. else
  368. GetPChar:='';
  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):ansistring;
  400. var
  401. hp : pchar;
  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. Get:=MsgReplace(system.strpas(hp),args);
  412. end;
  413. procedure TMessage.ResetStates;
  414. var
  415. i,j,glob : longint;
  416. state : tmsgstate;
  417. begin
  418. if not has_local_changes then
  419. exit;
  420. for i:=1 to msgparts do
  421. for j:=0 to msgidxmax[i] - 1 do
  422. begin
  423. state:=msgstates[i]^[j];
  424. glob:=(ord(state) and ms_global_mask) shr ms_shift;
  425. state:=tmsgstate((glob shl ms_shift) or glob);
  426. msgstates[i]^[j]:=state;
  427. end;
  428. has_local_changes:=false;
  429. end;
  430. end.