root/src/BinaryReader.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 "BinaryReader.h"

namespace nw {

BinaryReader::BinaryReader(const String filename) : Describer(), file(BinaryFstream(filename.c_str(), "rb")) {
        if(!hasValidStart())
        {
                std::cout << "Warning: " << file.getFilepath() << " is either corrupted"
                                "or was not created with " << N0Slib_Name <<  " (invalid header)." << std::endl;
                //TODO throw exception
        }
        describePointers();
}

BinaryReader::~BinaryReader() {
        if(file.isOpen())
                close();
}
void BinaryReader::close() {
        if(!hasValidEnd())
            //std::cout << "Warning: " << file.getFilepath() << " closed unexpectedly (with invalid footer)" << std::endl;
        file.close();
}

bool BinaryReader::isInWriteMode() { return false; }

bool BinaryReader::push(const String tagName)
{
        bool doesExists;
        file >> doesExists;
        return doesExists;
}
void BinaryReader::describeArray(const String arrayName, const String elementsName, unsigned int size)
{
        file >> size;
        openArrays.push(size);
}
void BinaryReader::describeValueArray(const String arrayName, unsigned int size)
{
        describeArray("","",size);
}
bool BinaryReader::enterNextElement(unsigned int iterator)
{
        if(iterator < openArrays.top())
        {
                return true;
        }
        else
        {
                openArrays.pop();
                return false;
        }
}
void BinaryReader::describeName(const String name)
{

}
void BinaryReader::describeValue(String& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(double& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(char& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(unsigned char& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(int& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(unsigned int& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(float& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue(bool& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( signed char& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( short& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( unsigned short& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( long& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( unsigned long& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( long long& ref)
{
        this->file >> ref;
}
void BinaryReader::describeValue( long double& ref)
{
        this->file >> ref;
}

void BinaryReader::describeValue( Pointer ref)
{
        unsigned int id;
        this->file >> id;
        ref = getSerializedObjectForId(id);
}
void BinaryReader::describeBlob(const String blobName, nw::Pointer blob, unsigned int blobSize)
{
        this->file.read(blob, blobSize);
}

void BinaryReader::comment(const String text) {}

void BinaryReader::pop() {}

//Binary Validation
bool BinaryReader::isValid()
{
        if(!hasValidStart() || !hasValidEnd())
        {
                std::cout << "Important Warning: "<< file.getFilepath() << " FileStartByte and EndByte are not intact!" << std::endl;
                return false;
        }
        return true;
}

//      bool isSmallEndian();
//      bool isBigEndian();
//      void swapEndians();
        bool BinaryReader::hasValidStart()
        {
                String validationByte;
                file >> validationByte;
                if(validationByte == (N0Slib_Binary_ValidationBytes) )
                        return true;
                return false;
        }

        bool BinaryReader::hasValidEnd()
        {
                String validationByte;
                file >> validationByte;
                if(validationByte == (N0Slib_Binary_ValidationBytes) )
                        return true;
                return false;
                //TODO 1.2 find fileEnd and read last byte BEFORE WE CLOSE THE FILE
        }

}  // namespace nw