2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * binfile.h - Binary file I/O
17 * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
25 /***** binfbase *****/
34 if(f != NULL) close();
37 void binfbase::close()
40 if(fclose(f) == EOF) err |= Fatal; else f = NULL;
45 void binfbase::seek(long pos, Offset offs)
49 if(f == NULL) { err |= NotOpen; return; }
52 case Set: error = fseek(f, pos, SEEK_SET); break;
53 case Add: error = fseek(f, pos, SEEK_CUR); break;
54 case End: error = fseek(f, pos, SEEK_END); break;
57 if(error == -1) err |= Fatal;
64 if(f == NULL) { err |= NotOpen; return 0; }
75 /***** binifstream *****/
77 binifstream::binifstream()
81 binifstream::binifstream(const char *filename, const Mode mode)
86 #if BINIO_ENABLE_STRING
87 binifstream::binifstream(const std::string &filename, const Mode mode)
93 binifstream::~binifstream()
97 void binifstream::open(const char *filename, const Mode mode)
99 f = fopen(filename, "rb");
103 case ENOENT: err |= NotFound; break;
104 case EACCES: err |= Denied; break;
105 default: err |= NotOpen; break;
109 #if BINIO_ENABLE_STRING
110 void binifstream::open(const std::string &filename, const Mode mode)
112 open(filename.c_str(), mode);
116 binifstream::Byte binifstream::getByte()
122 if(read == EOF) err |= Eof;
130 /***** binofstream *****/
132 binofstream::binofstream()
136 binofstream::binofstream(const char *filename, const Mode mode)
138 open(filename, mode);
141 #if BINIO_ENABLE_STRING
142 binofstream::binofstream(const std::string &filename, const Mode mode)
144 open(filename, mode);
148 binofstream::~binofstream()
152 void binofstream::open(const char *filename, const Mode mode)
154 char *modestr = "wb";
156 // Check if append mode is desired
157 if(mode & Append) modestr = "ab";
159 f = fopen(filename, modestr);
168 case ENOENT: err |= NotFound; break;
169 default: err |= NotOpen; break;
173 #if BINIO_ENABLE_STRING
174 void binofstream::open(const std::string &filename, const Mode mode)
176 open(filename.c_str(), mode);
180 void binofstream::putByte(Byte b)
182 if(f == NULL) { err |= NotOpen; return; }
184 if(fputc(b, f) == EOF)
188 /***** binfstream *****/
190 binfstream::binfstream()
194 binfstream::binfstream(const char *filename, const Mode mode)
196 open(filename, mode);
199 #if BINIO_ENABLE_STRING
200 binfstream::binfstream(const std::string &filename, const Mode mode)
202 open(filename, mode);
206 binfstream::~binfstream()
210 void binfstream::open(const char *filename, const Mode mode)
212 char *modestr = "w+b"; // Create & at beginning
215 // Apply desired mode
216 if(mode & NoCreate) {
218 modestr[0] = 'r'; // NoCreate & at beginning
220 if(mode & Append) // Create & append
223 f = fopen(filename, modestr);
225 // NoCreate & append (emulated -- not possible with standard C fopen())
226 if(f != NULL && (mode & Append) && (mode & NoCreate))
227 ferror = fseek(f, 0, SEEK_END);
229 if(f == NULL || ferror == -1) {
236 case ENOENT: err |= NotFound; break;
237 default: err |= NotOpen; break;
242 #if BINIO_ENABLE_STRING
243 void binfstream::open(const std::string &filename, const Mode mode)
245 open(filename.c_str(), mode);