Subversion Repositories filter_foundry

Rev

Rev 193 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 193 Rev 259
Line 1... Line 1...
1
/*
1
/*
2
        This file is part of a common library
2
        This file is part of a common library
3
    Copyright (C) 2002-2010 Toby Thain, toby@telegraphics.com.au
3
    Copyright (C) 2002-2010 Toby Thain, toby@telegraphics.com.au
4
 
4
 
5
    This program is free software; you can redistribute it and/or modify
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  
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
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
8
    (at your option) any later version.
9
 
9
 
10
    This program is distributed in the hope that it will be useful,
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
13
    GNU General Public License for more details.
14
 
14
 
15
    You should have received a copy of the GNU General Public License  
15
    You should have received a copy of the GNU General Public License  
16
    along with this program; if not, write to the Free Software
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
18
*/
19
 
19
 
20
#include <stdio.h>
20
#include <stdio.h>
21
#include <string.h>
21
#include <string.h>
22
 
22
 
23
#include "bufio.h"
23
#include "bufio.h"
24
#include "dbg.h"
24
#include "dbg.h"
25
 
25
 
26
unsigned char buf[BUFSIZE];
26
unsigned char buf[BUFSIZE];
27
long bytesinbuf = 0,bufptr = 0;
27
long bytesinbuf = 0,bufptr = 0;
28
int readeof;
28
int readeof;
29
OSErr buferr;
29
OSErr buferr;
30
 
30
 
31
OSErr refillbuffer(FILEREF r);
31
OSErr refillbuffer(FILEREF r);
32
 
32
 
33
OSErr refillbuffer(FILEREF r){
33
OSErr refillbuffer(FILEREF r){
34
        FILECOUNT count = BUFSIZE;
34
        FILECOUNT count = BUFSIZE;
35
        OSErr e = buferr = FSREAD(r,&count,buf);
35
        OSErr e = buferr = FSREAD(r,&count,buf);
36
        if(count){
36
        if(count){
37
//              sprintf(s,"refilled with %d bytes",count);dbg(s);
37
//              sprintf(s,"refilled with %d bytes",count);dbg(s);
38
                bufptr = 0;
38
                bufptr = 0;
39
                bytesinbuf = count;
39
                bytesinbuf = count;
40
                return noErr;
40
                return noErr;
41
        }else{
41
        }else{
42
//              sprintf(s,"failed to refill (%d)",e);dbg(s);
42
//              sprintf(s,"failed to refill (%d)",e);dbg(s);
43
                return e;
43
                return e;
44
        }
44
        }
45
}
45
}
46
 
46
 
47
OSErr flushbuffer(FILEREF r){
47
OSErr flushbuffer(FILEREF r){
48
        FILECOUNT count = bufptr;
48
        FILECOUNT count = bufptr;
49
 
49
 
50
        if(count){
50
        if(count){
51
                //sprintf(s,"flushing %d bytes",count);dbg(s);
51
                //sprintf(s,"flushing %d bytes",count);dbg(s);
52
                bufptr = 0;
52
                bufptr = 0;
53
                return buferr = FSWRITE(r,&count,buf);
53
                return buferr = FSWRITE(r,&count,buf);
54
        }else return noErr;
54
        }else return noErr;
55
}
55
}
56
 
56
 
57
OSErr bufgetbytes(FILEREF r,long n,char *p){
57
OSErr bufgetbytes(FILEREF r,long n,char *p){
58
        OSErr e;
58
        OSErr e;
59
        long chunk;
59
        long chunk;
60
        for( ; n > 0 ; n -= chunk ){
60
        for( ; n > 0 ; n -= chunk ){
61
                if( !bytesinbuf && (e = refillbuffer(r)) )
61
                if( !bytesinbuf && (e = refillbuffer(r)) )
62
                        return e;
62
                        return e;
63
                chunk = n < bytesinbuf ? n : bytesinbuf;
63
                chunk = n < bytesinbuf ? n : bytesinbuf;
64
//              sprintf(s,"copying chunk of %d bytes",chunk);dbg(s);
64
//              sprintf(s,"copying chunk of %d bytes",chunk);dbg(s);
65
                memcpy(p,buf+bufptr,chunk);
65
                memcpy(p,buf+bufptr,chunk);
66
                p += chunk;
66
                p += chunk;
67
                bufptr += chunk;
67
                bufptr += chunk;
68
                bytesinbuf -= chunk;
68
                bytesinbuf -= chunk;
69
        }
69
        }
70
        return noErr;
70
        return noErr;
71
}
71
}
72
 
72
 
73
OSErr bufputbytes(FILEREF r,long n,char *p){
73
OSErr bufputbytes(FILEREF r,long n,char *p){
74
        OSErr e;
74
        OSErr e;
75
        long chunk;
75
        long chunk;
76
 
76
 
77
        for( ; n > 0 ; n -= chunk ){
77
        for( ; n > 0 ; n -= chunk ){
78
                chunk = n < (BUFSIZE-bufptr) ? n : (BUFSIZE-bufptr);
78
                chunk = n < (BUFSIZE-bufptr) ? n : (BUFSIZE-bufptr);
79
                //sprintf(s,"appending %d bytes to buffer; bufptr = %d",chunk,bufptr);dbg(s);
79
                //sprintf(s,"appending %d bytes to buffer; bufptr = %d",chunk,bufptr);dbg(s);
80
                memcpy(buf+bufptr,p,chunk);
80
                memcpy(buf+bufptr,p,chunk);
81
                p += chunk;
81
                p += chunk;
82
                bufptr += chunk;
82
                bufptr += chunk;
83
                if( bufptr == BUFSIZE && (e = flushbuffer(r)) )
83
                if( bufptr == BUFSIZE && (e = flushbuffer(r)) )
84
                        return e;
84
                        return e;
85
        }
85
        }
86
        return noErr;
86
        return noErr;
87
}
87
}
88
 
88
 
89
int bufgetc(FILEREF r){
89
int bufgetc(FILEREF r){
90
        return ( !bytesinbuf && refillbuffer(r) ) ? EOF : (--bytesinbuf,buf[bufptr++]);
90
        return ( !bytesinbuf && refillbuffer(r) ) ? EOF : (--bytesinbuf,buf[bufptr++]);
91
}
91
}
92
 
92
 
93
void bufungetc(){
93
void bufungetc(){
94
        if(bufptr)
94
        if(bufptr)
95
                --bufptr;
95
                --bufptr;
96
}
96
}
97
 
97
 
98
OSErr bufputc(FILEREF r,int v){
98
OSErr bufputc(FILEREF r,int v){
99
        buf[bufptr++] = v;
99
        buf[bufptr++] = v;
100
        return bufptr == BUFSIZE ? flushbuffer(r) : noErr;
100
        return bufptr == BUFSIZE ? flushbuffer(r) : noErr;
101
}
101
}
102
 
102
 
103
int bufread2L(FILEREF r){
103
int bufread2L(FILEREF r){
104
        int v = bufgetc(r);
104
        int v = bufgetc(r);
105
        return v | (bufgetc(r)<<8);
105
        return v | (bufgetc(r)<<8);
106
}
106
}
107
 
107
 
108
long bufread4L(FILEREF r){
108
long bufread4L(FILEREF r){
109
        long v = bufgetc(r);
109
        long v = bufgetc(r);
110
        v |= (bufgetc(r)<<8);
110
        v |= (bufgetc(r)<<8);
111
        v |= (bufgetc(r)<<16);
111
        v |= (bufgetc(r)<<16);
112
        return v | (bufgetc(r)<<24);
112
        return v | (bufgetc(r)<<24);
113
}
113
}