cmsgs.pas 8.6 KB

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