#include <stdio.h>
#include <errno.h>
#include "binfile.h"
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;
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(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(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 = (char*)"wb";
if(mode & Append) modestr = (char*)"ab";
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(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 = (char*)"w+b"; int ferror = 0;
if(mode & NoCreate) {
if(!(mode & Append))
modestr[0] = 'r'; } else
if(mode & Append) modestr[0] = 'a';
f = fopen(filename, modestr);
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