messages.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. const
  50. bufsize=8192;
  51. var
  52. f : text;
  53. line,i : longint;
  54. ptxt : pchar;
  55. s : string;
  56. buf : pointer;
  57. begin
  58. getmem(buf,bufsize);
  59. {Read the message file}
  60. assign(f,fn);
  61. {$I-}
  62. reset(f);
  63. {$I+}
  64. if ioresult<>0 then
  65. begin
  66. WriteLn('*** message file '+fn+' not found ***');
  67. exit;
  68. end;
  69. settextbuf(f,buf^,bufsize);
  70. { First parse the file and count bytes needed }
  71. line:=0;
  72. msgs:=n;
  73. msgsize:=0;
  74. while not eof(f) do
  75. begin
  76. readln(f,s);
  77. inc(line);
  78. if (s<>'') and not(s[1] in ['#',';','%']) then
  79. begin
  80. i:=pos('=',s);
  81. if i>0 then
  82. inc(msgsize,length(s)-i+1)
  83. else
  84. writeln('error in line: ',line,' skipping');
  85. end;
  86. end;
  87. { now read the buffer in mem }
  88. getmem(msgtxt,msgsize);
  89. ptxt:=msgtxt;
  90. reset(f);
  91. while not eof(f) do
  92. begin
  93. readln(f,s);
  94. inc(line);
  95. if (s<>'') and not(s[1] in ['#',';']) then
  96. begin
  97. i:=pos('=',s);
  98. if i>0 then
  99. begin
  100. {txt}
  101. move(s[i+1],ptxt^,length(s)-i);
  102. inc(ptxt,length(s)-i);
  103. ptxt^:=#0;
  104. inc(ptxt);
  105. end;
  106. end;
  107. end;
  108. close(f);
  109. freemem(buf,bufsize);
  110. { now we can create the index }
  111. CreateIdx;
  112. end;
  113. destructor TMessage.Done;
  114. begin
  115. if not (msgidx=nil) then
  116. freemem(msgidx,msgs shl 2);
  117. if msgsize>0 then
  118. freemem(msgtxt,msgsize);
  119. end;
  120. procedure TMessage.CreateIdx;
  121. var
  122. hp : pchar;
  123. hpl : ppchar;
  124. n : longint;
  125. begin
  126. getmem(msgidx,msgs shl 2);
  127. hpl:=msgidx;
  128. hp:=msgtxt;
  129. n:=0;
  130. while (n<msgs) do
  131. begin
  132. hpl^:=hp;
  133. hpl:=pointer(longint(hpl)+4);
  134. inc(n);
  135. hp:=pchar(@hp[strlen(hp)+1]);
  136. end;
  137. end;
  138. function TMessage.Get(nr:longint):string;
  139. var
  140. s : string[16];
  141. hp : pchar;
  142. begin
  143. if msgidx=nil then
  144. hp:=nil
  145. else
  146. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  147. if hp=nil then
  148. begin
  149. Str(nr,s);
  150. Get:='msg nr '+s;
  151. end
  152. else
  153. Get:=StrPas(hp);
  154. end;
  155. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  156. var
  157. i : longint;
  158. s : string;
  159. begin
  160. s:=Get(nr);
  161. { $1 -> s1 }
  162. repeat
  163. i:=pos('$1',s);
  164. if i>0 then
  165. begin
  166. Delete(s,i,2);
  167. Insert(s1,s,i);
  168. end;
  169. until i=0;
  170. { $2 -> s2 }
  171. repeat
  172. i:=pos('$2',s);
  173. if i>0 then
  174. begin
  175. Delete(s,i,2);
  176. Insert(s2,s,i);
  177. end;
  178. until i=0;
  179. { $3 -> s3 }
  180. repeat
  181. i:=pos('$3',s);
  182. if i>0 then
  183. begin
  184. Delete(s,i,2);
  185. Insert(s3,s,i);
  186. end;
  187. until i=0;
  188. Get3:=s;
  189. end;
  190. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  191. begin
  192. Get2:=Get3(nr,s1,s2,'');
  193. end;
  194. function TMessage.Get1(nr:longint;const s1:string):string;
  195. begin
  196. Get1:=Get3(nr,s1,'','');
  197. end;
  198. end.
  199. {
  200. $Log$
  201. Revision 1.3 1998-08-29 13:52:31 peter
  202. + new messagefile
  203. * merged optione.msg into errore.msg
  204. Revision 1.2 1998/08/18 09:05:00 peter
  205. * fixed range errror
  206. }