messages.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. function Get(nr:longint):string;
  33. function Get3(nr:longint;const s1,s2,s3:string):string;
  34. function Get2(nr:longint;const s1,s2:string):string;
  35. function Get1(nr:longint;const s1:string):string;
  36. end;
  37. implementation
  38. uses
  39. strings;
  40. constructor TMessage.Init(p:pointer;n:longint);
  41. var
  42. hp : pchar;
  43. hpl : ppchar;
  44. begin
  45. hp:=pchar(p);
  46. msgtxt:=hp;
  47. msgsize:=0;
  48. msgs:=n;
  49. getmem(msgidx,msgs shl 2);
  50. hpl:=msgidx;
  51. n:=0;
  52. while (n<msgs) do
  53. begin
  54. hpl^:=hp;
  55. hpl:=pointer(longint(hpl)+4);
  56. inc(n);
  57. hp:=pchar(@hp[strlen(hp)+1]);
  58. end;
  59. end;
  60. constructor TMessage.InitExtern(const fn:string;n:longint);
  61. var
  62. f : file;
  63. bufread : word;
  64. i,j : longint;
  65. p : pchar;
  66. hpl : ppchar;
  67. begin
  68. msgs:=0;
  69. msgsize:=0;
  70. msgidx:=nil;
  71. {Read the message file}
  72. msgfilename:=fn;
  73. assign(f,fn);
  74. {$I-}
  75. reset(f,1);
  76. {$I+}
  77. if ioresult<>0 then
  78. begin
  79. WriteLn('*** message file '+msgfilename+' not found ***');
  80. exit;
  81. end;
  82. msgsize:=filesize(f);
  83. getmem(msgtxt,msgsize+1);
  84. blockread(f,msgtxt^,msgsize,bufread);
  85. msgtxt[msgsize]:=#10;
  86. close(f);
  87. inc(msgsize);
  88. {Parse buffer in msgtxt and create indexs}
  89. msgs:=n;
  90. getmem(msgidx,msgs shl 2);
  91. hpl:=msgidx;
  92. p:=msgtxt;
  93. i:=0;
  94. n:=0;
  95. while (i<bufread) and (n<msgs) do
  96. begin
  97. j:=0;
  98. while (i<bufread) and (not (p[j] in [#10,#13])) and (j<255) do
  99. begin
  100. inc(i);
  101. inc(j);
  102. end;
  103. if (i>=bufread) then
  104. break;
  105. if not (p[0] in [';','#']) then
  106. begin
  107. hpl^:=p;
  108. hpl:=pointer(longint(hpl)+4);
  109. inc(n);
  110. if (p[0]='<') and (p[1]='l') and (p[2]='f') and (p[3]='>') then
  111. p[0]:=#0
  112. else
  113. p[j]:=#0;
  114. end;
  115. repeat
  116. inc(i);
  117. inc(j);
  118. until (i>=bufread) or not(p[j] in [#10,#13]);
  119. p:=pchar(@p[j]);
  120. end;
  121. end;
  122. destructor TMessage.Done;
  123. begin
  124. if not (msgidx=nil) then
  125. freemem(msgidx,msgs shl 2);
  126. if msgsize>0 then
  127. freemem(msgtxt,msgsize);
  128. end;
  129. function TMessage.Get(nr:longint):string;
  130. var
  131. s : string[16];
  132. hp : pchar;
  133. begin
  134. if msgidx=nil then
  135. hp:=nil
  136. else
  137. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  138. if hp=nil then
  139. begin
  140. Str(nr,s);
  141. Get:='msg nr '+s;
  142. end
  143. else
  144. Get:=StrPas(hp);
  145. end;
  146. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  147. var
  148. i : longint;
  149. s : string;
  150. begin
  151. s:=Get(nr);
  152. { $1 -> s1 }
  153. repeat
  154. i:=pos('$1',s);
  155. if i>0 then
  156. begin
  157. Delete(s,i,2);
  158. Insert(s1,s,i);
  159. end;
  160. until i=0;
  161. { $2 -> s2 }
  162. repeat
  163. i:=pos('$2',s);
  164. if i>0 then
  165. begin
  166. Delete(s,i,2);
  167. Insert(s2,s,i);
  168. end;
  169. until i=0;
  170. { $3 -> s3 }
  171. repeat
  172. i:=pos('$3',s);
  173. if i>0 then
  174. begin
  175. Delete(s,i,2);
  176. Insert(s3,s,i);
  177. end;
  178. until i=0;
  179. Get3:=s;
  180. end;
  181. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  182. begin
  183. Get2:=Get3(nr,s1,s2,'');
  184. end;
  185. function TMessage.Get1(nr:longint;const s1:string):string;
  186. begin
  187. Get1:=Get3(nr,s1,'','');
  188. end;
  189. end.
  190. {
  191. $Log$
  192. Revision 1.2 1998-08-18 09:05:00 peter
  193. * fixed range errror
  194. }