messages.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. msgsize,
  26. msgs : longint;
  27. msgtxt : pchar;
  28. msgidx : ppchar;
  29. constructor Init(p:pointer;n:longint);
  30. constructor InitExtern(const fn:string;n:longint);
  31. destructor Done;
  32. procedure CreateIdx;
  33. function Get(nr:longint):string;
  34. function Get3(nr:longint;const s1,s2,s3:string):string;
  35. function Get2(nr:longint;const s1,s2:string):string;
  36. function Get1(nr:longint;const s1:string):string;
  37. end;
  38. implementation
  39. uses
  40. {$ifdef DELPHI}
  41. sysutils;
  42. {$else DELPHI}
  43. strings;
  44. {$endif DELPHI}
  45. constructor TMessage.Init(p:pointer;n:longint);
  46. begin
  47. msgtxt:=pchar(p);
  48. msgsize:=0;
  49. msgs:=n;
  50. CreateIdx;
  51. end;
  52. constructor TMessage.InitExtern(const fn:string;n:longint);
  53. {$ifndef FPC}
  54. procedure readln(var t:text;var s:string);
  55. var
  56. c : char;
  57. i : longint;
  58. begin
  59. c:=#0;
  60. i:=0;
  61. while (not eof(t)) and (c<>#10) do
  62. begin
  63. read(t,c);
  64. if c<>#10 then
  65. begin
  66. inc(i);
  67. s[i]:=c;
  68. end;
  69. end;
  70. if (i>0) and (s[i]=#13) then
  71. dec(i);
  72. s[0]:=chr(i);
  73. end;
  74. {$endif}
  75. const
  76. bufsize=8192;
  77. var
  78. f : text;
  79. line,i : longint;
  80. ptxt : pchar;
  81. s,s1 : string;
  82. buf : pointer;
  83. begin
  84. getmem(buf,bufsize);
  85. {Read the message file}
  86. assign(f,fn);
  87. {$I-}
  88. reset(f);
  89. {$I+}
  90. if ioresult<>0 then
  91. begin
  92. WriteLn('*** message file '+fn+' not found ***');
  93. exit;
  94. end;
  95. settextbuf(f,buf^,bufsize);
  96. { First parse the file and count bytes needed }
  97. line:=0;
  98. msgs:=n;
  99. msgsize:=0;
  100. while not eof(f) do
  101. begin
  102. readln(f,s);
  103. inc(line);
  104. if (s<>'') and not(s[1] in ['#',';','%']) then
  105. begin
  106. i:=pos('=',s);
  107. if i>0 then
  108. inc(msgsize,length(s)-i+1)
  109. else
  110. writeln('error in line: ',line,' skipping');
  111. end;
  112. end;
  113. { now read the buffer in mem }
  114. getmem(msgtxt,msgsize);
  115. ptxt:=msgtxt;
  116. reset(f);
  117. while not eof(f) do
  118. begin
  119. readln(f,s);
  120. if (s<>'') and not(s[1] in ['#',';','%']) then
  121. begin
  122. i:=pos('=',s);
  123. if i>0 then
  124. begin
  125. {txt}
  126. s1:=Copy(s,i+1,255);
  127. { support <lf> for empty lines }
  128. if s1='<lf>' then
  129. begin
  130. s1:='';
  131. { update the msgsize also! }
  132. dec(msgsize,4);
  133. end;
  134. {txt}
  135. move(s1[1],ptxt^,length(s1));
  136. inc(ptxt,length(s1));
  137. ptxt^:=#0;
  138. inc(ptxt);
  139. end;
  140. end;
  141. end;
  142. close(f);
  143. freemem(buf,bufsize);
  144. { now we can create the index }
  145. CreateIdx;
  146. end;
  147. destructor TMessage.Done;
  148. begin
  149. if not (msgidx=nil) then
  150. freemem(msgidx,msgs shl 2);
  151. if msgsize>0 then
  152. freemem(msgtxt,msgsize);
  153. end;
  154. procedure TMessage.CreateIdx;
  155. var
  156. hp : pchar;
  157. hpl : ppchar;
  158. n : longint;
  159. begin
  160. getmem(msgidx,msgs shl 2);
  161. hpl:=msgidx;
  162. hp:=msgtxt;
  163. n:=0;
  164. while (n<msgs) do
  165. begin
  166. hpl^:=hp;
  167. hpl:=pointer(longint(hpl)+4);
  168. inc(n);
  169. hp:=pchar(@hp[strlen(hp)+1]);
  170. end;
  171. end;
  172. function TMessage.Get(nr:longint):string;
  173. var
  174. s : string[16];
  175. hp : pchar;
  176. begin
  177. if msgidx=nil then
  178. hp:=nil
  179. else
  180. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  181. if hp=nil then
  182. begin
  183. Str(nr,s);
  184. Get:='msg nr '+s;
  185. end
  186. else
  187. Get:=StrPas(hp);
  188. end;
  189. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  190. var
  191. i : longint;
  192. s : string;
  193. begin
  194. s:=Get(nr);
  195. { $1 -> s1 }
  196. if s1<>'$1' then
  197. repeat
  198. i:=pos('$1',s);
  199. if i>0 then
  200. begin
  201. Delete(s,i,2);
  202. Insert(s1,s,i);
  203. end;
  204. until i=0;
  205. { $2 -> s2 }
  206. if s1<>'$2' then
  207. repeat
  208. i:=pos('$2',s);
  209. if i>0 then
  210. begin
  211. Delete(s,i,2);
  212. Insert(s2,s,i);
  213. end;
  214. until i=0;
  215. { $3 -> s3 }
  216. if s1<>'S3' then
  217. repeat
  218. i:=pos('$3',s);
  219. if i>0 then
  220. begin
  221. Delete(s,i,2);
  222. Insert(s3,s,i);
  223. end;
  224. until i=0;
  225. Get3:=s;
  226. end;
  227. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  228. begin
  229. Get2:=Get3(nr,s1,s2,'');
  230. end;
  231. function TMessage.Get1(nr:longint;const s1:string):string;
  232. begin
  233. Get1:=Get3(nr,s1,'','');
  234. end;
  235. end.
  236. {
  237. $Log$
  238. Revision 1.8 1999-07-18 10:19:55 florian
  239. * made it compilable with Dlephi 4 again
  240. + fixed problem with large stack allocations on win32
  241. Revision 1.7 1999/05/01 12:27:51 peter
  242. * fixed endless loop with replace $1 with $1
  243. Revision 1.6 1998/12/11 00:03:20 peter
  244. + globtype,tokens,version unit splitted from globals
  245. Revision 1.5 1998/09/16 16:41:42 peter
  246. * merged fixes
  247. Revision 1.3.2.1 1998/09/16 16:11:04 peter
  248. * unix lf support for messagefile for not FPC compiled compiler
  249. Revision 1.4 1998/09/14 10:44:08 peter
  250. * all internal RTL functions start with FPC_
  251. Revision 1.3 1998/08/29 13:52:31 peter
  252. + new messagefile
  253. * merged optione.msg into errore.msg
  254. Revision 1.2 1998/08/18 09:05:00 peter
  255. * fixed range errror
  256. }