/**
 * \file file.h Interface for File class
 *
 *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * File class provides a platform-independant way to access files on disk in
 * full 64bit mode.
 */
class File {
public:
	File();
	File(const std::string &name, OpenFlags flags);
	~File();

	void open(const std::string &name, OpenFlags flags);
	void close();
	void seek(uint64_t offset);
	std::string read(uint64_t len);
	std::string read(uint64_t offset, uint64_t len);
	void write(const std::string &data);
	void write(uint64_t offset, const std::string &data);
private:
	boost::scoped_ptr<FileImplBase> m_impl;
};

/**
 * Abstract base implementation class for File class; declares only pure virtual
 * methods that actual implementations shall implement.
 */
class FileImplBase {
public:
	FileImplBase();
	FileImplBase(const std::string &name);
	virtual ~FileImplBase() = 0;

	virtual void open(const std::string &name) = 0;
	virtual void close() = 0;
	virtual void seek(uint64_t offset) = 0;
	virtual std::string read(uint64_t len) = 0;
	virtual std::string read(uint64_t offset, uint64_t len) = 0;
	virtual void write(const std::string &data) = 0;
	virtual void write(uint64_t offset, const std::string &data) = 0;
};

/**
 * Implements FileImplBase for win32 and win64 platforms
 */
class FileImplWin {
public:
	FileImplWin();
	FileImplWin(const std::string &name);
	virtual ~FileImplWin();

	virtual void open(const std::string &name);
	virtual void close();
	virtual void seek(uint64_t offset);
	virtual std::string read(uint64_t len);
	virtual std::string read(uint64_t offset, uint64_t len);
	virtual void write(const std::string &data);
	virtual void write(uint64_t offset, const std::string &data);
private:
	int m_fd;
};

/**
 * Implements FileImplBase class for Posix platforms
 */
class FileImplPosix {
public:
	FileImplPosix();
	FileImplPosix(const std::string &name);
	virtual ~FileImplPosix();

	virtual void open(const std::string &name);
	virtual void close();
	virtual void seek(uint64_t offset);
	virtual std::string read(uint64_t len);
	virtual std::string read(uint64_t offset, uint64_t len);
	virtual void write(const std::string &data);
	virtual void write(uint64_t offset, const std::string &data);
private:
	int m_fd;
};