123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- /*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * binfile.h - Binary file I/O
- * Copyright (C) 2002, 2003 Simon Peter <dn.tlp@gmx.net>
- */
- #include <stdio.h>
- #include <errno.h>
- #include "binfile.h"
- /***** binfbase *****/
- binfbase::binfbase()
- : f(NULL) {
- }
- binfbase::~binfbase() {
- if (f != NULL) close();
- }
- void binfbase::close() {
- if (f != NULL) {
- if (fclose(f) == EOF) err |= Fatal;
- else f = NULL;
- } else
- err |= NotOpen;
- }
- void binfbase::seek(long pos, Offset offs) {
- int error = -1;
- if (f == NULL) {
- err |= NotOpen;
- return;
- }
- switch (offs) {
- case Set:
- error = fseek(f, pos, SEEK_SET);
- break;
- case Add:
- error = fseek(f, pos, SEEK_CUR);
- break;
- case End:
- error = fseek(f, pos, SEEK_END);
- break;
- }
- if (error == -1)
- err |= Fatal;
- }
- long binfbase::pos() {
- long pos;
- if (f == NULL) {
- err |= NotOpen;
- return 0;
- }
- pos = ftell(f);
- if (pos == -1) {
- err |= Fatal;
- return 0;
- } else
- return pos;
- }
- /***** binifstream *****/
- binifstream::binifstream() {
- }
- binifstream::binifstream(const char *filename, const Mode mode) {
- open(filename, mode);
- }
- #if BINIO_ENABLE_STRING
- binifstream::binifstream(const std::string &filename, const Mode mode) {
- open(filename, mode);
- }
- #endif
- binifstream::~binifstream() {
- }
- void binifstream::open(const char *filename, const Mode mode) {
- f = fopen(filename, "rb");
- if (f == NULL)
- switch (errno) {
- case ENOENT:
- err |= NotFound;
- break;
- case EACCES:
- err |= Denied;
- break;
- default:
- err |= NotOpen;
- break;
- }
- }
- #if BINIO_ENABLE_STRING
- void binifstream::open(const std::string &filename, const Mode mode) {
- open(filename.c_str(), mode);
- }
- #endif
- binifstream::Byte binifstream::getByte() {
- int read;
- if (f != NULL) {
- read = fgetc(f);
- if (read == EOF) err |= Eof;
- return (Byte)read;
- } else {
- err |= NotOpen;
- return 0;
- }
- }
- /***** binofstream *****/
- binofstream::binofstream() {
- }
- binofstream::binofstream(const char *filename, const Mode mode) {
- open(filename, mode);
- }
- #if BINIO_ENABLE_STRING
- binofstream::binofstream(const std::string &filename, const Mode mode) {
- open(filename, mode);
- }
- #endif
- binofstream::~binofstream() {
- }
- void binofstream::open(const char *filename, const Mode mode) {
- char modestr[] = "wb";
- // Check if append mode is desired
- if (mode & Append) modestr[0] = 'a';
- f = fopen(filename, modestr);
- if (f == NULL)
- switch (errno) {
- case EEXIST:
- case EACCES:
- case EROFS:
- err |= Denied;
- break;
- case ENOENT:
- err |= NotFound;
- break;
- default:
- err |= NotOpen;
- break;
- }
- }
- #if BINIO_ENABLE_STRING
- void binofstream::open(const std::string &filename, const Mode mode) {
- open(filename.c_str(), mode);
- }
- #endif
- void binofstream::putByte(Byte b) {
- if (f == NULL) {
- err |= NotOpen;
- return;
- }
- if (fputc(b, f) == EOF)
- err |= Fatal;
- }
- /***** binfstream *****/
- binfstream::binfstream() {
- }
- binfstream::binfstream(const char *filename, const Mode mode) {
- open(filename, mode);
- }
- #if BINIO_ENABLE_STRING
- binfstream::binfstream(const std::string &filename, const Mode mode) {
- open(filename, mode);
- }
- #endif
- binfstream::~binfstream() {
- }
- void binfstream::open(const char *filename, const Mode mode) {
- char modestr[] = "w+b"; // Create & at beginning
- int ferror = 0;
- // Apply desired mode
- if (mode & NoCreate) {
- if (!(mode & Append))
- modestr[0] = 'r'; // NoCreate & at beginning
- } else
- if (mode & Append) // Create & append
- modestr[0] = 'a';
- f = fopen(filename, modestr);
- // NoCreate & append (emulated -- not possible with standard C fopen())
- if (f != NULL && (mode & Append) && (mode & NoCreate))
- ferror = fseek(f, 0, SEEK_END);
- if (f == NULL || ferror == -1) {
- switch (errno) {
- case EEXIST:
- case EACCES:
- case EROFS:
- err |= Denied;
- break;
- case ENOENT:
- err |= NotFound;
- break;
- default:
- err |= NotOpen;
- break;
- }
- }
- }
- #if BINIO_ENABLE_STRING
- void binfstream::open(const std::string &filename, const Mode mode) {
- open(filename.c_str(), mode);
- }
- #endif
|