messages.pas 5.4 KB

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