cmsgs.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit implements the message object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cmsgs;
  19. {$i fpcdefs.inc}
  20. interface
  21. const
  22. maxmsgidxparts = 20;
  23. type
  24. ppchar=^pchar;
  25. TArrayOfPChar = array[0..1000] of pchar;
  26. PArrayOfPChar = ^TArrayOfPChar;
  27. PMessage=^TMessage;
  28. TMessage=object
  29. msgfilename : string;
  30. msgintern : boolean;
  31. msgallocsize,
  32. msgsize,
  33. msgparts,
  34. msgs : longint;
  35. msgtxt : pchar;
  36. msgidx : array[1..maxmsgidxparts] of PArrayOfPChar;
  37. msgidxmax : array[1..maxmsgidxparts] of longint;
  38. constructor Init(n:longint;const idxmax:array of longint);
  39. destructor Done;
  40. function LoadIntern(p:pointer;n:longint):boolean;
  41. function LoadExtern(const fn:string):boolean;
  42. procedure ClearIdx;
  43. procedure CreateIdx;
  44. function GetPChar(nr:longint):pchar;
  45. function Get(nr:longint;const args:array of string):string;
  46. end;
  47. { this will read a line until #10 or #0 and also increase p }
  48. function GetMsgLine(var p:pchar):string;
  49. implementation
  50. uses
  51. cutils,
  52. strings;
  53. function MsgReplace(const s:string;const args:array of string):string;
  54. var
  55. last,
  56. i : longint;
  57. hs : string;
  58. begin
  59. if s='' then
  60. begin
  61. MsgReplace:='';
  62. exit;
  63. end;
  64. hs:='';
  65. i:=0;
  66. last:=0;
  67. while (i<length(s)-1) do
  68. begin
  69. inc(i);
  70. if (s[i]='$') and (s[i+1] in ['1'..'9']) then
  71. begin
  72. hs:=hs+copy(s,last+1,i-last-1)+args[byte(s[i+1])-byte('1')];
  73. inc(i);
  74. last:=i;
  75. end;
  76. end;
  77. MsgReplace:=hs+copy(s,last+1,length(s)-last);;
  78. end;
  79. constructor TMessage.Init(n:longint;const idxmax:array of longint);
  80. var
  81. i : longint;
  82. begin
  83. msgtxt:=nil;
  84. msgsize:=0;
  85. msgparts:=n;
  86. if n<>high(idxmax)+1 then
  87. fail;
  88. for i:=1 to n do
  89. begin
  90. msgidxmax[i]:=idxmax[i-1];
  91. getmem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  92. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  93. end;
  94. end;
  95. destructor TMessage.Done;
  96. var
  97. i : longint;
  98. begin
  99. for i:=1 to msgparts do
  100. freemem(msgidx[i],msgidxmax[i]*sizeof(pointer));
  101. if msgallocsize>0 then
  102. begin
  103. freemem(msgtxt,msgsize);
  104. msgallocsize:=0;
  105. end;
  106. msgtxt:=nil;
  107. msgsize:=0;
  108. msgparts:=0;
  109. end;
  110. function TMessage.LoadIntern(p:pointer;n:longint):boolean;
  111. begin
  112. msgtxt:=pchar(p);
  113. msgsize:=n;
  114. msgallocsize:=0;
  115. msgintern:=true;
  116. ClearIdx;
  117. CreateIdx;
  118. LoadIntern:=true;
  119. end;
  120. function TMessage.LoadExtern(const fn:string):boolean;
  121. {$ifndef FPC}
  122. procedure readln(var t:text;var s:string);
  123. var
  124. c : char;
  125. i : longint;
  126. begin
  127. c:=#0;
  128. i:=0;
  129. while (not eof(t)) and (c<>#10) do
  130. begin
  131. read(t,c);
  132. if c<>#10 then
  133. begin
  134. inc(i);
  135. s[i]:=c;
  136. end;
  137. end;
  138. if (i>0) and (s[i]=#13) then
  139. dec(i);
  140. s[0]:=chr(i);
  141. end;
  142. {$endif}
  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:string);
  153. begin
  154. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  155. error:=true;
  156. end;
  157. begin
  158. LoadExtern:=false;
  159. getmem(buf,bufsize);
  160. { Read the message file }
  161. assign(f,fn);
  162. {$I-}
  163. reset(f);
  164. {$I+}
  165. if ioresult<>0 then
  166. begin
  167. WriteLn('*** PPC, can not open message file ',fn);
  168. exit;
  169. end;
  170. settextbuf(f,buf^,bufsize);
  171. { First parse the file and count bytes needed }
  172. error:=false;
  173. line:=0;
  174. multiline:=false;
  175. msgsize:=0;
  176. while not eof(f) do
  177. begin
  178. readln(f,s);
  179. inc(line);
  180. if multiline then
  181. begin
  182. if s=']' then
  183. multiline:=false
  184. else
  185. inc(msgsize,length(s)+1); { +1 for linebreak }
  186. end
  187. else
  188. begin
  189. if (s<>'') and not(s[1] in ['#',';','%']) then
  190. begin
  191. i:=pos('=',s);
  192. if i>0 then
  193. begin
  194. j:=i+1;
  195. if not(s[j] in ['0'..'9']) then
  196. err('no number found')
  197. else
  198. begin
  199. while (s[j] in ['0'..'9']) do
  200. inc(j);
  201. end;
  202. if j-i-1<>5 then
  203. err('number length is not 5');
  204. if s[j+1]='[' then
  205. begin
  206. inc(msgsize,j-i);
  207. multiline:=true
  208. end
  209. else
  210. inc(msgsize,length(s)-i+1);
  211. end
  212. else
  213. err('no = found');
  214. end;
  215. end;
  216. end;
  217. if multiline then
  218. err('still in multiline mode');
  219. if error then
  220. begin
  221. freemem(buf,bufsize);
  222. close(f);
  223. exit;
  224. end;
  225. { now read the buffer in mem }
  226. msgallocsize:=msgsize;
  227. getmem(msgtxt,msgallocsize);
  228. ptxt:=msgtxt;
  229. reset(f);
  230. while not eof(f) do
  231. begin
  232. readln(f,s);
  233. if multiline then
  234. begin
  235. if s=']' then
  236. begin
  237. multiline:=false;
  238. { overwrite last eol }
  239. dec(ptxt);
  240. ptxt^:=#0;
  241. inc(ptxt);
  242. end
  243. else
  244. begin
  245. move(s[1],ptxt^,length(s));
  246. inc(ptxt,length(s));
  247. ptxt^:=#10;
  248. inc(ptxt);
  249. end;
  250. end
  251. else
  252. begin
  253. if (s<>'') and not(s[1] in ['#',';','%']) then
  254. begin
  255. i:=pos('=',s);
  256. if i>0 then
  257. begin
  258. j:=i+1;
  259. while (s[j] in ['0'..'9']) do
  260. inc(j);
  261. { multiline start then no txt }
  262. if s[j+1]='[' then
  263. begin
  264. s1:=Copy(s,i+1,j-i);
  265. move(s1[1],ptxt^,length(s1));
  266. inc(ptxt,length(s1));
  267. multiline:=true;
  268. end
  269. else
  270. begin
  271. { txt including number }
  272. s1:=Copy(s,i+1,255);
  273. move(s1[1],ptxt^,length(s1));
  274. inc(ptxt,length(s1));
  275. ptxt^:=#0;
  276. inc(ptxt);
  277. end;
  278. end;
  279. end;
  280. end;
  281. end;
  282. close(f);
  283. freemem(buf,bufsize);
  284. { now we can create the index, clear if the previous load was also
  285. an external file, because those can't be reused }
  286. if not msgintern then
  287. ClearIdx;
  288. CreateIdx;
  289. { set that we've loaded an external file }
  290. msgintern:=false;
  291. LoadExtern:=true;
  292. end;
  293. procedure TMessage.ClearIdx;
  294. var
  295. i : longint;
  296. begin
  297. { clear }
  298. for i:=1 to msgparts do
  299. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  300. end;
  301. procedure TMessage.CreateIdx;
  302. var
  303. hp1,
  304. hp,hpend : pchar;
  305. code : integer;
  306. num : longint;
  307. number : string[5];
  308. i : longint;
  309. numpart,numidx : longint;
  310. begin
  311. { process msgtxt buffer }
  312. number:='00000';
  313. hp:=msgtxt;
  314. hpend:=@msgtxt[msgsize];
  315. while (hp<hpend) do
  316. begin
  317. hp1:=hp;
  318. for i:=1 to 5 do
  319. begin
  320. number[i]:=hp1^;
  321. inc(hp1);
  322. end;
  323. val(number,num,code);
  324. numpart:=num div 1000;
  325. numidx:=num mod 1000;
  326. { check range }
  327. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  328. begin
  329. { skip _ }
  330. inc(hp1);
  331. { put the address in the idx, the numbers are already checked }
  332. msgidx[numpart]^[numidx]:=hp1;
  333. end;
  334. { next string }
  335. hp:=pchar(@hp[strlen(hp)+1]);
  336. end;
  337. end;
  338. function GetMsgLine(var p:pchar):string;
  339. var
  340. i : longint;
  341. begin
  342. i:=0;
  343. while not(p^ in [#0,#10]) and (i<255) do
  344. begin
  345. inc(i);
  346. GetMsgLine[i]:=p^;
  347. inc(p);
  348. end;
  349. { skip #10 }
  350. if p^=#10 then
  351. inc(p);
  352. { if #0 then set p to nil }
  353. if p^=#0 then
  354. p:=nil;
  355. { return string }
  356. GetMsgLine[0]:=chr(i);
  357. end;
  358. function TMessage.GetPChar(nr:longint):pchar;
  359. begin
  360. GetPChar:=msgidx[nr div 1000]^[nr mod 1000];
  361. end;
  362. function TMessage.Get(nr:longint;const args:array of string):string;
  363. var
  364. hp : pchar;
  365. begin
  366. hp:=msgidx[nr div 1000]^[nr mod 1000];
  367. if hp=nil then
  368. Get:='msg nr '+tostr(nr)
  369. else
  370. Get:=MsgReplace(strpas(hp),args);
  371. end;
  372. end.
  373. {
  374. $Log$
  375. Revision 1.14 2005-02-14 17:13:06 peter
  376. * truncate log
  377. }