/*! \file piiostream.h
 * \ingroup IO
 * \~\brief
 * \~english PIBinaryStream functionality for PIIODevice
 * \~russian Функциональность PIBinaryStream для PIIODevice
 */
/*
	PIP - Platform Independent Primitives
	PIBinaryStream functionality for PIIODevice
	Ivan Pelipenko peri4ko@yandex.ru

	This program 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.

	This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIIOSTREAM_H
#define PIIOSTREAM_H

#include "piiostring.h"
#include "pitextstream.h"


//! \ingroup IO
//! \~\brief
//! \~english PIBinaryStream functionality for PIIODevice.
//! \~russian Функциональность PIBinaryStream для PIIODevice.
//! \~english See details \ref iostream
//! \~russian Подробнее \ref iostream
class PIP_EXPORT PIIOBinaryStream: public PIBinaryStream<PIIOBinaryStream> {
public:

	//! \~english Contructs %PIIOBinaryStream for "device" device
	//! \~russian Создает %PIIOBinaryStream для устройства "device"
	PIIOBinaryStream(PIIODevice * device = nullptr): dev(device) {}

	//! \~english Assign "device" device
	//! \~russian Назначает устройство "device"
	void setDevice(PIIODevice * device) {dev = device;}

	bool binaryStreamAppendImp(const void * d, size_t s) {
		if (!dev) return false;
		return (dev->write(d, s) == (int)s);
	}

	bool binaryStreamTakeImp(void * d, size_t s) {
		if (!dev) return false;
		return (dev->read(d, s) == (int)s);
	}

	ssize_t binaryStreamSizeImp() const {
		if (!dev) return 0;
		return dev->bytesAvailable();
	}

private:
	PIIODevice * dev;

};


//! \ingroup IO
//! \~\brief
//! \~english PITextStream functionality for PIIODevice.
//! \~russian Функциональность PITextStream для PIIODevice.
class PIP_EXPORT PIIOTextStream: public PITextStream<PIIOBinaryStream> {
public:

	//! \~english Contructs %PIIOTextStream for "device" device
	//! \~russian Создает %PIIOTextStream для устройства "device"
	PIIOTextStream(PIIODevice * device): PITextStream<PIIOBinaryStream>(&bin_stream), bin_stream(device) {}

	//! \~english Contructs %PIIOTextStream for "string" string
	//! \~russian Создает %PIIOTextStream для строки "string"
	PIIOTextStream(PIString * string): PITextStream<PIIOBinaryStream>(&bin_stream) {
		io_string = new PIIOString(string);
		bin_stream.setDevice(io_string);
	}

	~PIIOTextStream() {
		if (io_string)
			delete io_string;
	}

	void setDevice(PIIODevice * device) {
		bin_stream = PIIOBinaryStream(device);
		setStream(&bin_stream);
	}

private:
	PIIOString * io_string = nullptr;
	PIIOBinaryStream bin_stream;

};


#endif // PIIOSTREAM_H
