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