messages.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (s<>'') and not(s[1] in ['#',';','%']) then
  95. begin
  96. i:=pos('=',s);
  97. if i>0 then
  98. begin
  99. {txt}
  100. move(s[i+1],ptxt^,length(s)-i);
  101. inc(ptxt,length(s)-i);
  102. ptxt^:=#0;
  103. inc(ptxt);
  104. end;
  105. end;
  106. end;
  107. close(f);
  108. freemem(buf,bufsize);
  109. { now we can create the index }
  110. CreateIdx;
  111. end;
  112. destructor TMessage.Done;
  113. begin
  114. if not (msgidx=nil) then
  115. freemem(msgidx,msgs shl 2);
  116. if msgsize>0 then
  117. freemem(msgtxt,msgsize);
  118. end;
  119. procedure TMessage.CreateIdx;
  120. var
  121. hp : pchar;
  122. hpl : ppchar;
  123. n : longint;
  124. begin
  125. getmem(msgidx,msgs shl 2);
  126. hpl:=msgidx;
  127. hp:=msgtxt;
  128. n:=0;
  129. while (n<msgs) do
  130. begin
  131. hpl^:=hp;
  132. hpl:=pointer(longint(hpl)+4);
  133. inc(n);
  134. hp:=pchar(@hp[strlen(hp)+1]);
  135. end;
  136. end;
  137. function TMessage.Get(nr:longint):string;
  138. var
  139. s : string[16];
  140. hp : pchar;
  141. begin
  142. if msgidx=nil then
  143. hp:=nil
  144. else
  145. hp:=pchar(pointer(longint(msgidx)+nr shl 2)^);
  146. if hp=nil then
  147. begin
  148. Str(nr,s);
  149. Get:='msg nr '+s;
  150. end
  151. else
  152. Get:=StrPas(hp);
  153. end;
  154. function TMessage.Get3(nr:longint;const s1,s2,s3:string):string;
  155. var
  156. i : longint;
  157. s : string;
  158. begin
  159. s:=Get(nr);
  160. { $1 -> s1 }
  161. repeat
  162. i:=pos('$1',s);
  163. if i>0 then
  164. begin
  165. Delete(s,i,2);
  166. Insert(s1,s,i);
  167. end;
  168. until i=0;
  169. { $2 -> s2 }
  170. repeat
  171. i:=pos('$2',s);
  172. if i>0 then
  173. begin
  174. Delete(s,i,2);
  175. Insert(s2,s,i);
  176. end;
  177. until i=0;
  178. { $3 -> s3 }
  179. repeat
  180. i:=pos('$3',s);
  181. if i>0 then
  182. begin
  183. Delete(s,i,2);
  184. Insert(s3,s,i);
  185. end;
  186. until i=0;
  187. Get3:=s;
  188. end;
  189. function TMessage.Get2(nr:longint;const s1,s2:string):string;
  190. begin
  191. Get2:=Get3(nr,s1,s2,'');
  192. end;
  193. function TMessage.Get1(nr:longint;const s1:string):string;
  194. begin
  195. Get1:=Get3(nr,s1,'','');
  196. end;
  197. end.
  198. {
  199. $Log$
  200. Revision 1.4 1998-09-14 10:44:08 peter
  201. * all internal RTL functions start with FPC_
  202. Revision 1.3 1998/08/29 13:52:31 peter
  203. + new messagefile
  204. * merged optione.msg into errore.msg
  205. Revision 1.2 1998/08/18 09:05:00 peter
  206. * fixed range errror
  207. }