cmsgs.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. SysUtils,
  51. cutils;
  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. const
  121. bufsize=8192;
  122. var
  123. f : text;
  124. error,multiline : boolean;
  125. line,i,j : longint;
  126. ptxt : pchar;
  127. s,s1 : string;
  128. buf : pointer;
  129. procedure err(const msgstr:string);
  130. begin
  131. writeln('*** PPC, file ',fn,', error in line ',line,': ',msgstr);
  132. error:=true;
  133. end;
  134. begin
  135. LoadExtern:=false;
  136. getmem(buf,bufsize);
  137. { Read the message file }
  138. assign(f,fn);
  139. {$I-}
  140. reset(f);
  141. {$I+}
  142. if ioresult<>0 then
  143. begin
  144. WriteLn('*** PPC, can not open message file ',fn);
  145. exit;
  146. end;
  147. settextbuf(f,buf^,bufsize);
  148. { First parse the file and count bytes needed }
  149. error:=false;
  150. line:=0;
  151. multiline:=false;
  152. msgsize:=0;
  153. while not eof(f) do
  154. begin
  155. readln(f,s);
  156. inc(line);
  157. if multiline then
  158. begin
  159. if s=']' then
  160. multiline:=false
  161. else
  162. inc(msgsize,length(s)+1); { +1 for linebreak }
  163. end
  164. else
  165. begin
  166. if (s<>'') and not(s[1] in ['#',';','%']) then
  167. begin
  168. i:=pos('=',s);
  169. if i>0 then
  170. begin
  171. j:=i+1;
  172. if not(s[j] in ['0'..'9']) then
  173. err('no number found')
  174. else
  175. begin
  176. while (s[j] in ['0'..'9']) do
  177. inc(j);
  178. end;
  179. if j-i-1<>5 then
  180. err('number length is not 5');
  181. if s[j+1]='[' then
  182. begin
  183. inc(msgsize,j-i);
  184. multiline:=true
  185. end
  186. else
  187. inc(msgsize,length(s)-i+1);
  188. end
  189. else
  190. err('no = found');
  191. end;
  192. end;
  193. end;
  194. if multiline then
  195. err('still in multiline mode');
  196. if error then
  197. begin
  198. freemem(buf,bufsize);
  199. close(f);
  200. exit;
  201. end;
  202. { now read the buffer in mem }
  203. msgallocsize:=msgsize;
  204. getmem(msgtxt,msgallocsize);
  205. ptxt:=msgtxt;
  206. reset(f);
  207. while not eof(f) do
  208. begin
  209. readln(f,s);
  210. if multiline then
  211. begin
  212. if s=']' then
  213. begin
  214. multiline:=false;
  215. { overwrite last eol }
  216. dec(ptxt);
  217. ptxt^:=#0;
  218. inc(ptxt);
  219. end
  220. else
  221. begin
  222. move(s[1],ptxt^,length(s));
  223. inc(ptxt,length(s));
  224. ptxt^:=#10;
  225. inc(ptxt);
  226. end;
  227. end
  228. else
  229. begin
  230. if (s<>'') and not(s[1] in ['#',';','%']) then
  231. begin
  232. i:=pos('=',s);
  233. if i>0 then
  234. begin
  235. j:=i+1;
  236. while (s[j] in ['0'..'9']) do
  237. inc(j);
  238. { multiline start then no txt }
  239. if s[j+1]='[' then
  240. begin
  241. s1:=Copy(s,i+1,j-i);
  242. move(s1[1],ptxt^,length(s1));
  243. inc(ptxt,length(s1));
  244. multiline:=true;
  245. end
  246. else
  247. begin
  248. { txt including number }
  249. s1:=Copy(s,i+1,255);
  250. move(s1[1],ptxt^,length(s1));
  251. inc(ptxt,length(s1));
  252. ptxt^:=#0;
  253. inc(ptxt);
  254. end;
  255. end;
  256. end;
  257. end;
  258. end;
  259. close(f);
  260. freemem(buf,bufsize);
  261. { now we can create the index, clear if the previous load was also
  262. an external file, because those can't be reused }
  263. if not msgintern then
  264. ClearIdx;
  265. CreateIdx;
  266. { set that we've loaded an external file }
  267. msgintern:=false;
  268. LoadExtern:=true;
  269. end;
  270. procedure TMessage.ClearIdx;
  271. var
  272. i : longint;
  273. begin
  274. { clear }
  275. for i:=1 to msgparts do
  276. fillchar(msgidx[i]^,msgidxmax[i]*sizeof(pointer),0);
  277. end;
  278. procedure TMessage.CreateIdx;
  279. var
  280. hp1,
  281. hp,hpend : pchar;
  282. code : integer;
  283. num : longint;
  284. number : string[5];
  285. i : longint;
  286. numpart,numidx : longint;
  287. begin
  288. { process msgtxt buffer }
  289. number:='00000';
  290. hp:=msgtxt;
  291. hpend:=@msgtxt[msgsize];
  292. while (hp<hpend) do
  293. begin
  294. hp1:=hp;
  295. for i:=1 to 5 do
  296. begin
  297. number[i]:=hp1^;
  298. inc(hp1);
  299. end;
  300. val(number,num,code);
  301. numpart:=num div 1000;
  302. numidx:=num mod 1000;
  303. { check range }
  304. if (numpart <= msgparts) and (numidx < msgidxmax[numpart]) then
  305. begin
  306. { skip _ }
  307. inc(hp1);
  308. { put the address in the idx, the numbers are already checked }
  309. msgidx[numpart]^[numidx]:=hp1;
  310. end;
  311. { next string }
  312. hp:=pchar(@hp[strlen(hp)+1]);
  313. end;
  314. end;
  315. function GetMsgLine(var p:pchar):string;
  316. var
  317. i : longint;
  318. begin
  319. i:=0;
  320. while not(p^ in [#0,#10]) and (i<255) do
  321. begin
  322. inc(i);
  323. GetMsgLine[i]:=p^;
  324. inc(p);
  325. end;
  326. { skip #10 }
  327. if p^=#10 then
  328. inc(p);
  329. { if #0 then set p to nil }
  330. if p^=#0 then
  331. p:=nil;
  332. { return string }
  333. GetMsgLine[0]:=chr(i);
  334. end;
  335. function TMessage.GetPChar(nr:longint):pchar;
  336. begin
  337. GetPChar:=msgidx[nr div 1000]^[nr mod 1000];
  338. end;
  339. function TMessage.Get(nr:longint;const args:array of string):string;
  340. var
  341. hp : pchar;
  342. begin
  343. hp:=msgidx[nr div 1000]^[nr mod 1000];
  344. if hp=nil then
  345. Get:='msg nr '+tostr(nr)
  346. else
  347. Get:=MsgReplace(strpas(hp),args);
  348. end;
  349. end.