messages.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. line,i : longint;
  82. ptxt : pchar;
  83. s,s1 : string;
  84. buf : pointer;
  85. begin
  86. getmem(buf,bufsize);
  87. {Read the message file}
  88. assign(f,fn);
  89. {$I-}
  90. reset(f);
  91. {$I+}
  92. if ioresult<>0 then
  93. begin
  94. WriteLn('*** message file '+fn+' not found ***');
  95. exit;
  96. end;
  97. settextbuf(f,buf^,bufsize);
  98. { First parse the file and count bytes needed }
  99. line:=0;
  100. msgs:=n;
  101. msgsize:=0;
  102. while not eof(f) do
  103. begin
  104. readln(f,s);
  105. inc(line);
  106. if (s<>'') and not(s[1] in ['#',';','%']) then
  107. begin
  108. i:=pos('=',s);
  109. if i>0 then
  110. inc(msgsize,length(s)-i+1)
  111. else
  112. writeln('error in line: ',line,' skipping');
  113. end;
  114. end;
  115. { now read the buffer in mem }
  116. msgallocsize:=msgsize;
  117. getmem(msgtxt,msgallocsize);
  118. ptxt:=msgtxt;
  119. reset(f);
  120. while not eof(f) do
  121. begin
  122. readln(f,s);
  123. if (s<>'') and not(s[1] in ['#',';','%']) then
  124. begin
  125. i:=pos('=',s);
  126. if i>0 then
  127. begin
  128. {txt}
  129. s1:=Copy(s,i+1,255);
  130. { support <lf> for empty lines }
  131. if s1='<lf>' then
  132. begin
  133. s1:='';
  134. { update the msgsize also! }
  135. dec(msgsize,4);
  136. end;
  137. {txt}
  138. move(s1[1],ptxt^,length(s1));
  139. inc(ptxt,length(s1));
  140. ptxt^:=#0;
  141. inc(ptxt);
  142. end;
  143. end;
  144. end;
  145. close(f);
  146. freemem(buf,bufsize);
  147. { now we can create the index }
  148. CreateIdx;
  149. end;
  150. destructor TMessage.Done;
  151. begin
  152. if assigned(msgidx) then
  153. begin
  154. freemem(msgidx,msgs shl 2);
  155. msgidx:=nil;
  156. end;
  157. if msgallocsize>0 then
  158. begin
  159. freemem(msgtxt,msgallocsize);
  160. msgtxt:=nil;
  161. msgallocsize:=0;
  162. end;
  163. end;
  164. procedure TMessage.CreateIdx;
  165. var
  166. hp : pchar;
  167. hpl : ppchar;
  168. n : longint;
  169. begin
  170. getmem(msgidx,msgs shl 2);
  171. hpl:=msgidx;
  172. hp:=msgtxt;
  173. n:=0;
  174. while (n<msgs) do
  175. begin
  176. hpl^:=hp;
  177. hpl:=pointer(longint(hpl)+4);
  178. inc(n);
  179. hp:=pchar(@hp[strlen(hp)+1]);
  180. end;
  181. end;
  182. function TMessage.Get(nr:longint):string;
  183. var
  184. s : string[16];
  185. hp : pchar;
  186. begin
  187. if msgidx=nil then
  188. hp:=nil
  189. else
  190. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  191. if hp=nil then
  192. begin
  193. Str(nr,s);
  194. Get:='msg nr '+s;
  195. end
  196. else
  197. Get:=StrPas(hp);
  198. end;
  199. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  200. var
  201. i : longint;
  202. s : string;
  203. begin
  204. s:=Get(nr);
  205. { $1 -> s1 }
  206. if s1<>'$1' then
  207. repeat
  208. i:=pos('$1',s);
  209. if i>0 then
  210. begin
  211. Delete(s,i,2);
  212. Insert(s1,s,i);
  213. end;
  214. until i=0;
  215. { $2 -> s2 }
  216. if s1<>'$2' then
  217. repeat
  218. i:=pos('$2',s);
  219. if i>0 then
  220. begin
  221. Delete(s,i,2);
  222. Insert(s2,s,i);
  223. end;
  224. until i=0;
  225. { $3 -> s3 }
  226. if s1<>'S3' then
  227. repeat
  228. i:=pos('$3',s);
  229. if i>0 then
  230. begin
  231. Delete(s,i,2);
  232. Insert(s3,s,i);
  233. end;
  234. until i=0;
  235. Get3:=s;
  236. end;
  237. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  238. begin
  239. Get2:=Get3(nr,s1,s2,'');
  240. end;
  241. function TMessage.Get1(nr:longint;const s1:string):string;
  242. begin
  243. Get1:=Get3(nr,s1,'','');
  244. end;
  245. end.
  246. {
  247. $Log$
  248. Revision 1.11 2000-02-09 13:22:54 peter
  249. * log truncated
  250. Revision 1.10 2000/01/23 16:32:08 peter
  251. * fixed wrong freemem size when loading message file
  252. Revision 1.9 2000/01/07 01:14:27 peter
  253. * updated copyright to 2000
  254. }