-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmini_fstream.h
218 lines (167 loc) · 3.97 KB
/
mini_fstream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef inc_mini_fstream_h
#define inc_mini_fstream_h
#include "stdarg.h"
#ifndef SEEK_SET
#define SEEK_SET 0 /* seek relative to beginning of file */
#define SEEK_CUR 1 /* seek relative to current file position */
#define SEEK_END 2 /* seek relative to end of file */
#define SEEK_MAX SEEK_END
#endif
#ifdef mini_buf
#ifndef mini_fstream
#define mini_fstream
#endif
#endif
//+header stdio.h
//+include
//+after read close
//+doc This does nothing, since minilib doesn't provide buffered streams yet.
//+doc In order to sync data to disc, please use fsync
//+inline
static inline int __attribute__((always_inline)) fflush( FILE *F ){
return(0);
}
#ifndef mini_fstream
//+doc Return the fd nummber of stdin,-out,-err.
// this is a boilerplate, in case, no minibuf and no streams are compiled.
//+def
static int fileno( FILE *F ){
if ( F==stdin )
return ( 0 );
if ( F==stdout )
return(1);
if ( F==stderr )
return(2);
exit(-1);
return(-1);
}
#else
int snprintf( char *buf, size_t size, const char *fmt, ... );
//+doc Return the fd nummber of stdin,-out,-err.
//+def
static int fileno( FILE *f ){
return( (f==0) ? 0 : (*f & FD_MASK) );
}
////+macro
#ifdef mini_fclose
//+depends close
//+inline
static inline int __attribute__((always_inline)) fclose( FILE* f ){
int fd = fileno(f);
*f = -1;
// empty garbage; go back to first closed stream
if ( f[1] == mlgl->stream[mlgl->pstream] )
for ( mlgl->pstream--; mlgl->stream[mlgl->pstream-1] == -1; mlgl->pstream-- );
return( close(fd) );
}
#endif
//+depends fprintf fileno snprintf vsnprintf write strlen
//+macro
#define printf(...) fprintf(stdout,__VA_ARGS__)
//+depends fprintf fileno snprintf vsnprintf write strlen
//+macro
#define fprint(...) fprintf(__VA_ARGS__)
//+depends fprintf fileno snprintf vsnprintf write strlen
//+macro
#define vfprintf(...) fprintf(__VA_ARGS__)
//+depends write
//+inline
static inline size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *f){
const void *p = ptr;
int a;
for ( a = 0; a<nmemb; a++ ){
if ( write( fileno(f), p, size ) != size ){
*f = *f | ERR_FLAG;
return(a);
}
p = p + size;
}
return(a);
}
#ifdef mini_lseek
//+depends lseek
//+inline
static inline long ftell(FILE *f){
return(lseek( fileno(f), 0, SEEK_CUR ));
}
//+depends ftell
//+inline
static inline void fgetpos(FILE *f, long *pos ){
*pos = ftell(f);
}
//+depends lseek
//+inline
static inline int fsetpos(FILE *f, int pos ){
int r = lseek( fileno(f), pos, SEEK_SET );
if ( r==pos ){ //
*f = *f & FD_MASK; // unset feof
return(r);
}
return(r); // todo set errno
}
//+depends lseek
//+inline
static inline int fseek(FILE *f, long offset, int whence ){
int r = lseek( fileno(f), offset, whence );
if ( r>0 )
*f = *f & FD_MASK; // clear feof
return(r);
}
//+depends fseek
//+inline
static inline void rewind( FILE *f ){
fseek(f, 0, SEEK_SET);
}
#endif
#ifdef mini_fread
//+depends read
//+inline
static inline size_t fread(void *ptr, size_t size, size_t nmemb, FILE *f){
void *p = ptr;
int a;
for ( a = 0; a<nmemb; a++ ){
if ( read( fileno(f), p, size ) != size ){
*f = *f | FEOF_FLAG ; // set feof. Could have also been another error.
return(a);
}
p = p + size;
}
return(a);
}
#endif
//todo: case eagain
///+depends _fread
///+macro
//+inline
static inline int feof(FILE *f){
if ( *f & FLAG_MASK )
return(1);
return(0);
}
//+inline
static inline int ferror(FILE *f){
if ( *f & FLAG_MASK )
return(1);
return(0);
}
//+inline
static inline void clearerr(FILE *f){
*f = *f & FD_MASK;
}
//+inline
static inline void clearerror(FILE *f){
*f = *f & FD_MASK;
}
//+doc dummy function.
// There is no buffering implemented for the streams yet.
//+def
static void setbuf(FILE *stream, char *buf){
}
//+doc dummy function
//+def
static int setvbuf(FILE *stream, char *buf, int mode, size_t size){
return(0);
}
// ifdef mini_fstream
#endif
#endif