Subversion Repositories filter_foundry

Rev

Rev 193 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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