OpenCores
URL https://opencores.org/ocsvn/usb_fpga_1_2/usb_fpga_1_2/trunk

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [bmp/] [src/] [textbuf.pas] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
{*!
2
   bmp -- babel macro processor
3
   Copyright (C) 2008-2009 ZTEX e.K.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*}
18
 
19
{$mode objfpc}
20
unit textbuf;
21
 
22
interface
23
 
24
const maxspathc   = 1024;
25
      lineInfoPattern : shortstring = '';
26
 
27
type  pdword = ^dword;
28
 
29
      CTextBuf = class;
30
      CTextBuf = class
31
        buf         : pchar;    { character buffer }
32
        li          : pdword;   { line info lineNum shl 12 + file }
33
        lastbuf,d   : longint;
34
        allocsize   : longint;  { allocated size }
35
        last        : CTextBuf;
36
        killme      : boolean;
37
        constructor create(var tb:CTextBuf);
38
        constructor create(var tb:CTextBuf; asize:longint);
39
        constructor create(var tb:CTextBuf; cb:pchar; lib:pdword; bufsize:longint);
40
        destructor destroy; override;
41
        procedure endbuf; virtual;
42
        end;
43
 
44
      CFileBuf = class(CTextBuf)
45
        constructor create(var tb:CTextBuf; const fn,ep:ansistring);
46
        procedure endbuf; override;
47
       private
48
        fileNum : longint;
49
        line    : longint;
50
        fname   : ansistring;
51
        errStr  : ansistring; { error trefix }
52
        stat    : longint;
53
        f       : file;
54
        end;
55
 
56
var spath  : array[0..maxspathc-1] of ansistring;
57
    spathc : longint;
58
 
59
function getFileNum(const fn:ansistring):longint;
60
function lineInfoStr(li:dword):ansistring;
61
procedure lineInfoStr(var fo:text; li:dword);
62
 
63
implementation
64
 
65
uses dos,bmpsys;
66
const textbuf_size = 2048;
67
 
68
{ ********************************************************************* }
69
{ ****** CTextBuf ***************************************************** }
70
{ ********************************************************************* }
71
{ ****** create ******************************************************* }
72
constructor CTextBuf.create(var tb:CTextBuf);
73
begin
74
lastbuf:=-1;
75
d:=0;
76
killme:=false;
77
last:=tb;
78
tb:=self;
79
end;
80
 
81
constructor CTextBuf.create(var tb:CTextBuf; asize:longint);
82
begin
83
create(tb);
84
getmem(buf,asize);
85
getmem(li,asize*4);
86
allocsize:=asize;
87
end;
88
 
89
constructor CTextBuf.create(var tb:CTextBuf; cb:pchar; lib:pdword; bufsize:longint);
90
begin
91
create(tb);
92
buf:=cb;
93
li:=lib;
94
lastbuf:=bufsize-1;
95
allocsize:=0;
96
end;
97
 
98
{ ****** destroy ****************************************************** }
99
destructor CTextBuf.destroy;
100
begin
101
if allocsize>0 then
102
  begin
103
  freemem(buf,allocsize);
104
  freemem(li,allocsize*4);
105
  end;
106
end;
107
 
108
{ ****** endbuf ******************************************************* }
109
procedure CTextBuf.endbuf;
110
begin
111
killme:=true;
112
end;
113
 
114
{ ********************************************************************* }
115
{ ****** CFileBuf ***************************************************** }
116
{ ********************************************************************* }
117
{ ****** create ******************************************************* }
118
constructor CFileBuf.create(var tb:CTextBuf; const fn,ep:ansistring);
119
begin
120
inherited create(tb,textbuf_size);
121
stat:=0;
122
pointer(fname):=nil;
123
fname:=fn;
124
pointer(errStr):=nil;
125
errStr:=ep;
126
fileNum:=-1;
127
line:=0;
128
end;
129
 
130
{ ****** endbuf ******************************************************* }
131
procedure CFileBuf.endbuf;
132
var i,j  : longint;
133
    e    : dword;
134
    sr   : searchrec;
135
    dir  : dirstr;
136
    name : namestr;
137
    ext  : extstr;
138
    b    : boolean;
139
begin
140
lastbuf:=-1;
141
d:=0;
142
killme:=true;
143
 
144
if stat=0 then
145
  begin
146
  stat:=1;
147
  if fname<>'' then
148
     begin
149
     j:=-1;
150
//     write(fname+':  ');
151
     for i:=0 to spathc-1 do
152
       begin
153
//       write(spath[i]+'   ');
154
       findfirst(spath[i]+fname,$27,sr);
155
       if doserror=0 then
156
          begin
157
          if j<>-1
158
             then bmp_warning(errstr+'`'+spath[i]+fname+''' found, using `'+spath[j]+fname+'''')
159
             else j:=i;
160
          end;
161
       end;
162
//     writeln(':  '+spath[j]+fname);
163
 
164
     if j=-1 then
165
        begin
166
        bmp_error(errstr+'Can not find file: `'+fname+'''');
167
        stat:=2;
168
        exit;
169
        end;
170
 
171
     fsplit(spath[j]+fname,dir,name,ext);
172
     b:=true;
173
     for i:=0 to spathc-1 do
174
       if dir=spath[i] then b:=false;
175
     if b then
176
        begin
177
        if spathc>=maxspathc
178
           then bmp_error(errstr+'Maximum amount of search paths reached')
179
           else begin
180
                spath[spathc]:=dir;
181
                spathc+=1;
182
                end;
183
        end;
184
 
185
     fileNum:=getFileNum(spath[j]+fname);
186
     line:=0;
187
     try
188
       filemode:=0;
189
       assign(f,spath[j]+fname);
190
       reset(f,1);
191
     except
192
       bmp_error(errstr+'Can not open file: '+spath[j]+fname);
193
       fname:='';
194
       stat:=2;
195
       end;
196
     end;
197
  end;
198
 
199
if stat=1 then
200
  begin
201
  if fname=''
202
     then begin
203
          lastbuf:=-1;
204
          while (not(eof(input))) and (lastbuf<textbuf_size-1) do
205
            begin
206
            lastbuf+=1;
207
            read(buf[lastbuf]);
208
            end;
209
          end
210
     else begin
211
          try
212
            blockread(f,buf^,textbuf_size,lastbuf);
213
            lastbuf-=1;
214
          except
215
            bmp_error(errstr+'Can read from file: '+fname);
216
            lastbuf:=-1;
217
            end;
218
          end;
219
 
220
  e:=(line shl 12) + fileNum;
221
  for i:=0 to lastbuf do
222
    begin
223
    if buf[i]=#10 then
224
      begin
225
      e+=4096;
226
      line+=1;
227
      end;
228
    li[i]:=e;
229
    end;
230
 
231
  if lastbuf<textbuf_size-1 then
232
    begin
233
    stat:=2;
234
    if fname<>'' then
235
      begin
236
      try
237
        close(f);
238
      except
239
        end;
240
      end;
241
    end;
242
  end;
243
 
244
if lastbuf>=0 then killme:=false;
245
end;
246
 
247
 
248
{ ********************************************************************* }
249
{ ****** FileCache **************************************************** }
250
{ ********************************************************************* }
251
const fileCount   : longint = 0;
252
var fileCache : array[0..4095] of ansistring;
253
 
254
{ ****** getFileNum *************************************************** }
255
function getFileNum(const fn:ansistring):longint;
256
var i  : longint;
257
begin
258
i:=0;
259
while (i<fileCount) and (i<4096) and (fn<>fileCache[i]) do
260
  i+=1;
261
if fn<>fileCache[i] then
262
  begin
263
  i:=fileCount and 4095;
264
  fileCache[i]:=fn;
265
  fileCount+=1;
266
  end;
267
result:=i;
268
end;
269
 
270
{ ****** lineInfoStr ************************************************* }
271
function lineInfoStr(li:dword):ansistring;
272
begin
273
result:=fileCache[li and 4095]+'('+int2str((li shr 12)+1)+')';
274
end;
275
 
276
procedure lineInfoStr(var fo:text; li:dword);
277
var s   : ansistring;
278
    i,j : longint;
279
begin
280
pointer(s):=nil;
281
i:=1;
282
while i<=length(lineInfoPattern) do
283
  begin
284
  if (lineInfoPattern[i]='%') and (i<length(lineInfoPattern))
285
    then case lineInfoPattern[i+1] of
286
           '1' : begin
287
                 for j:=1 to length(fileCache[li and 4095]) do
288
                   if fileCache[li and 4095][j]='\'
289
                     then s+='\\'
290
                     else s+=fileCache[li and 4095][j];
291
                 i+=1;
292
                 end;
293
           '2' : begin
294
                 s+=int2str(li shr 12);
295
                 i+=1;
296
                 end;
297
           '%' : begin
298
                 s+='%';
299
                 i+=1;
300
                 end;
301
           else s+='%';
302
         end
303
    else s+=lineInfoPattern[i];
304
  i+=1;
305
  end;
306
writeln(fo,s);
307
end;
308
 
309
 
310
end.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.