view C++/Pack.cpp @ 299:24085fae3162

Update licenses
author David Demelier <markand@malikania.fr>
date Sat, 15 Nov 2014 13:11:38 +0100
parents 7433ebe6a8b0
children
line wrap: on
line source

/*
 * Pack.cpp -- binary data serialization
 *
 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "Pack.h"

namespace {

Pack::Endian checkMode()
{
	int i = 1;
	unsigned char *ptr = reinterpret_cast<unsigned char *>(&i);

	return (ptr[0] == 1) ? Pack::Little : Pack::Big;
}

} // !namespace

const Pack::Endian Pack::mode = checkMode();

/* --------------------------------------------------------
 * PackReader
 * -------------------------------------------------------- */

PackReader::PackReader(Pack::Endian endian)
	: m_endian(endian)
{
}

/* --------------------------------------------------------
 * PackFileReader
 * -------------------------------------------------------- */

PackFileReader::PackFileReader(const std::string &path, Pack::Endian endian)
	: PackReader(endian)
{
	m_in.open(path, std::ifstream::in);
}

std::istream &PackFileReader::stream()
{
	return m_in;
}

/* --------------------------------------------------------
 * PackStringReader
 * -------------------------------------------------------- */

PackStringReader::PackStringReader(std::string input, Pack::Endian endian)
	: PackReader(endian)
	, m_in(std::move(input))
{
}

std::istream &PackStringReader::stream()
{
	return m_in;
}

/* --------------------------------------------------------
 * PackWriter
 * -------------------------------------------------------- */

PackWriter::PackWriter(Pack::Endian endian)
	: m_endian(endian)
{
}

/* --------------------------------------------------------
 * PackFileWriter
 * -------------------------------------------------------- */

PackFileWriter::PackFileWriter(const std::string &path, Pack::Endian endian)
	: PackWriter(endian)
{
	m_out.open(path, std::ofstream::out);
}

std::ostream &PackFileWriter::stream()
{
	return m_out;
}

/* --------------------------------------------------------
 * PackStringWriter
 * -------------------------------------------------------- */

PackStringWriter::PackStringWriter(Pack::Endian endian)
	: PackWriter(endian)
{
}

std::ostream &PackStringWriter::stream()
{
	return m_out;
}

std::string PackStringWriter::buffer() const
{
	return m_out.str();
}