2
0

messages.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 Messages;
  19. interface
  20. type
  21. ppchar=^pchar;
  22. PMessage=^TMessage;
  23. TMessage=object
  24. msgfilename : string;
  25. msgallocsize,
  26. msgsize,
  27. msgs : longint;
  28. msgtxt : pchar;
  29. msgidx : ppchar;
  30. constructor Init(p:pointer;n:longint);
  31. constructor InitExtern(const fn:string;n:longint);
  32. destructor Done;
  33. procedure CreateIdx;
  34. function Get(nr:longint):string;
  35. function Get3(nr:longint;const s1,s2,s3:string):string;
  36. function Get2(nr:longint;const s1,s2:string):string;
  37. function Get1(nr:longint;const s1:string):string;
  38. end;
  39. implementation
  40. uses
  41. {$ifdef DELPHI}
  42. sysutils;
  43. {$else DELPHI}
  44. strings;
  45. {$endif DELPHI}
  46. constructor TMessage.Init(p:pointer;n:longint);
  47. begin
  48. msgtxt:=pchar(p);
  49. msgallocsize:=0;
  50. msgsize:=0;
  51. msgs:=n;
  52. CreateIdx;
  53. end;
  54. constructor TMessage.InitExtern(const fn:string;n:longint);
  55. {$ifndef FPC}
  56. procedure readln(var t:text;var s:string);
  57. var
  58. c : char;
  59. i : longint;
  60. begin
  61. c:=#0;
  62. i:=0;
  63. while (not eof(t)) and (c<>#10) do
  64. begin
  65. read(t,c);
  66. if c<>#10 then
  67. begin
  68. inc(i);
  69. s[i]:=c;
  70. end;
  71. end;
  72. if (i>0) and (s[i]=#13) then
  73. dec(i);
  74. s[0]:=chr(i);
  75. end;
  76. {$endif}
  77. const
  78. bufsize=8192;
  79. var
  80. f : text;
  81. msgsread,
  82. line,i : longint;
  83. ptxt : pchar;
  84. s,s1 : string;
  85. buf : pointer;
  86. begin
  87. getmem(buf,bufsize);
  88. {Read the message file}
  89. assign(f,fn);
  90. {$I-}
  91. reset(f);
  92. {$I+}
  93. if ioresult<>0 then
  94. begin
  95. WriteLn('*** message file '+fn+' not found ***');
  96. fail;
  97. end;
  98. settextbuf(f,buf^,bufsize);
  99. { First parse the file and count bytes needed }
  100. line:=0;
  101. msgs:=n;
  102. msgsize:=0;
  103. msgsread:=0;
  104. while not eof(f) do
  105. begin
  106. readln(f,s);
  107. inc(line);
  108. if (s<>'') and not(s[1] in ['#',';','%']) then
  109. begin
  110. i:=pos('=',s);
  111. if i>0 then
  112. begin
  113. inc(msgsize,length(s)-i+1);
  114. inc(msgsread);
  115. end
  116. else
  117. writeln('error in line: ',line,' skipping');
  118. end;
  119. end;
  120. { check amount of messages }
  121. if msgsread<>msgs then
  122. begin
  123. WriteLn('*** message file '+fn+' is corrupt: read ',msgsread,' of ',msgs,' msgs ***');
  124. close(f);
  125. freemem(buf,bufsize);
  126. fail;
  127. end;
  128. { now read the buffer in mem }
  129. msgallocsize:=msgsize;
  130. getmem(msgtxt,msgallocsize);
  131. ptxt:=msgtxt;
  132. reset(f);
  133. while not eof(f) do
  134. begin
  135. readln(f,s);
  136. if (s<>'') and not(s[1] in ['#',';','%']) then
  137. begin
  138. i:=pos('=',s);
  139. if i>0 then
  140. begin
  141. {txt}
  142. s1:=Copy(s,i+1,255);
  143. { support <lf> for empty lines }
  144. if s1='<lf>' then
  145. begin
  146. s1:='';
  147. { update the msgsize also! }
  148. dec(msgsize,4);
  149. end;
  150. {txt}
  151. move(s1[1],ptxt^,length(s1));
  152. inc(ptxt,length(s1));
  153. ptxt^:=#0;
  154. inc(ptxt);
  155. end;
  156. end;
  157. end;
  158. close(f);
  159. freemem(buf,bufsize);
  160. { now we can create the index }
  161. CreateIdx;
  162. end;
  163. destructor TMessage.Done;
  164. begin
  165. if assigned(msgidx) then
  166. begin
  167. freemem(msgidx,msgs shl 2);
  168. msgidx:=nil;
  169. end;
  170. if msgallocsize>0 then
  171. begin
  172. freemem(msgtxt,msgallocsize);
  173. msgtxt:=nil;
  174. msgallocsize:=0;
  175. end;
  176. end;
  177. procedure TMessage.CreateIdx;
  178. var
  179. hp : pchar;
  180. hpl : ppchar;
  181. n : longint;
  182. begin
  183. getmem(msgidx,msgs shl 2);
  184. hpl:=msgidx;
  185. hp:=msgtxt;
  186. n:=0;
  187. while (n<msgs) do
  188. begin
  189. hpl^:=hp;
  190. hpl:=pointer(longint(hpl)+4);
  191. inc(n);
  192. hp:=pchar(@hp[strlen(hp)+1]);
  193. end;
  194. end;
  195. function TMessage.Get(nr:longint):string;
  196. var
  197. s : string[16];
  198. hp : pchar;
  199. begin
  200. if msgidx=nil then
  201. hp:=nil
  202. else
  203. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  204. if hp=nil then
  205. begin
  206. Str(nr,s);
  207. Get:='msg nr '+s;
  208. end
  209. else
  210. Get:=StrPas(hp);
  211. end;
  212. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  213. var
  214. i : longint;
  215. s : string;
  216. begin
  217. s:=Get(nr);
  218. { $1 -> s1 }
  219. if s1<>'$1' then
  220. repeat
  221. i:=pos('$1',s);
  222. if i>0 then
  223. begin
  224. Delete(s,i,2);
  225. Insert(s1,s,i);
  226. end;
  227. until i=0;
  228. { $2 -> s2 }
  229. if s1<>'$2' then
  230. repeat
  231. i:=pos('$2',s);
  232. if i>0 then
  233. begin
  234. Delete(s,i,2);
  235. Insert(s2,s,i);
  236. end;
  237. until i=0;
  238. { $3 -> s3 }
  239. if s1<>'S3' then
  240. repeat
  241. i:=pos('$3',s);
  242. if i>0 then
  243. begin
  244. Delete(s,i,2);
  245. Insert(s3,s,i);
  246. end;
  247. until i=0;
  248. Get3:=s;
  249. end;
  250. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  251. begin
  252. Get2:=Get3(nr,s1,s2,'');
  253. end;
  254. function TMessage.Get1(nr:longint;const s1:string):string;
  255. begin
  256. Get1:=Get3(nr,s1,'','');
  257. end;
  258. end.
  259. {
  260. $Log$
  261. Revision 1.12 2000-03-01 22:29:18 peter
  262. * message files are check for amount of msgs found. If not correct a
  263. line is written to stdout and switched to internal messages
  264. Revision 1.11 2000/02/09 13:22:54 peter
  265. * log truncated
  266. Revision 1.10 2000/01/23 16:32:08 peter
  267. * fixed wrong freemem size when loading message file
  268. Revision 1.9 2000/01/07 01:14:27 peter
  269. * updated copyright to 2000
  270. }