cmsgs.pas 8.9 KB

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