root/src/BinaryFstream.cpp

////////////////////////////////////////////////////////////////////////
// Copyright (c) Nehmulos 2011-2014
// This file is part of N0 Strain Serialization Library.
//
// N0Strain-Serialization-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 3 of the License, or
// (at your option) any later version.
//
// N0Strain-Serialization-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 N0Strain-Serialization-Library.  If not, see https://gnu.org/licenses/lgpl-3.0
////////////////////////////////////////////////////////////////////////




#include "BinaryFstream.h"

namespace nw {

BinaryFstream::BinaryFstream(const char * filename, const char * mode)
{
        file = fopen(filename,mode);
        this->filepath = filename;
}

BinaryFstream::~BinaryFstream() {
        if(file != NULL)
                fclose(file);
}

void BinaryFstream::close()
{
        if(file != NULL)
        {
                fclose(file);
                file = NULL;
        }
}
bool BinaryFstream::isOpen()
{
        return file != NULL;
}

const char* BinaryFstream::getFilepath()
{
        return this->filepath;
}

void BinaryFstream::operator<<(String& value)
{
        unsigned int size = value.size();
        fwrite(&size,sizeof(size),1,file);
        char *content = new char[size];
        content = (char*)value.c_str();
        fwrite(content,size,1,file);
//      fwrite(value.data(),size,1,file);
}
void BinaryFstream::operator<<(int& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(char& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(unsigned char& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(unsigned int& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(float& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(double& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(bool value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(signed char& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(short& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(unsigned short& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(long& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(unsigned long& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(long long& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::operator<<(long double& value)
{
        fwrite(&value,sizeof(value),1,file);
}
void BinaryFstream::write(void *binaryBlob, unsigned int size)
{
        fwrite(binaryBlob,size,1,file);
}

/* Reading */
void BinaryFstream::operator>>(String& value)
{
        unsigned int size = 0;
        fread(&size,sizeof(size),1,file);
        char *content = new char[size];
        fread(content,size,1,file);
        value.assign(content,size);
}
void BinaryFstream::operator>>(int& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(char& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(unsigned char& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(unsigned int& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(float& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(double& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(bool& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(signed char& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(short& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(unsigned short& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(long& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(unsigned long& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(long long& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::operator>>(long double& value)
{
        fread(&value,sizeof(value),1,file);
}
void BinaryFstream::read(void *binaryBlob, unsigned int size)
{
        fread(binaryBlob,size,1,file);
}

}  // namespace nw