messages.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 (not (p[j] in [#10,#13])) and (j<255) and (i<bufread) do
  99. begin
  100. inc(i);
  101. inc(j);
  102. end;
  103. if not (p[0] in [';','#']) then
  104. begin
  105. hpl^:=p;
  106. hpl:=pointer(longint(hpl)+4);
  107. inc(n);
  108. if (p[0]='<') and (p[1]='l') and (p[2]='f') and (p[3]='>') then
  109. p[0]:=#0
  110. else
  111. p[j]:=#0;
  112. end;
  113. repeat
  114. inc(i);
  115. inc(j);
  116. until not (p[j] in [#10,#13]);
  117. p:=pchar(@p[j]);
  118. end;
  119. end;
  120. destructor TMessage.Done;
  121. begin
  122. if not (msgidx=nil) then
  123. freemem(msgidx,msgs shl 2);
  124. if msgsize>0 then
  125. freemem(msgtxt,msgsize);
  126. end;
  127. function TMessage.Get(nr:longint):string;
  128. var
  129. s : string[16];
  130. hp : pchar;
  131. begin
  132. if msgidx=nil then
  133. hp:=nil
  134. else
  135. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  136. if hp=nil then
  137. begin
  138. Str(nr,s);
  139. Get:='msg nr '+s;
  140. end
  141. else
  142. Get:=StrPas(hp);
  143. end;
  144. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  145. var
  146. i : longint;
  147. s : string;
  148. begin
  149. s:=Get(nr);
  150. { $1 -> s1 }
  151. repeat
  152. i:=pos('$1',s);
  153. if i>0 then
  154. begin
  155. Delete(s,i,2);
  156. Insert(s1,s,i);
  157. end;
  158. until i=0;
  159. { $2 -> s2 }
  160. repeat
  161. i:=pos('$2',s);
  162. if i>0 then
  163. begin
  164. Delete(s,i,2);
  165. Insert(s2,s,i);
  166. end;
  167. until i=0;
  168. { $3 -> s3 }
  169. repeat
  170. i:=pos('$3',s);
  171. if i>0 then
  172. begin
  173. Delete(s,i,2);
  174. Insert(s3,s,i);
  175. end;
  176. until i=0;
  177. Get3:=s;
  178. end;
  179. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  180. begin
  181. Get2:=Get3(nr,s1,s2,'');
  182. end;
  183. function TMessage.Get1(nr:longint;const s1:string):string;
  184. begin
  185. Get1:=Get3(nr,s1,'','');
  186. end;
  187. end.
  188. {
  189. $Log$
  190. Revision 1.1 1998-03-25 11:18:13 root
  191. Initial revision
  192. Revision 1.3 1998/03/10 01:17:20 peter
  193. * all files have the same header
  194. * messages are fully implemented, EXTDEBUG uses Comment()
  195. + AG... files for the Assembler generation
  196. Revision 1.2 1998/03/05 02:44:12 peter
  197. * options cleanup and use of .msg file
  198. Revision 1.1 1998/03/02 01:55:19 peter
  199. + Initial implementation
  200. }