cmsgs.pas 8.9 KB

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