/*! \file pifile.h
 * \brief Local file
*/
/*
    PIP - Platform Independent Primitives
    File
    Copyright (C) 2014  Ivan Pelipenko peri4ko@gmail.com

    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 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PIFILE_H
#define PIFILE_H

#include "piiodevice.h"
#include <cstdio>

class PIP_EXPORT PIFile: public PIIODevice
{
	PIIODEVICE(PIFile)
public:
	
	//! Constructs a file with path "path" nad open mode "type"
	PIFile(const PIString & path = PIString(), DeviceMode mode = ReadWrite): PIIODevice(path, mode) {setPrecision(5); if (!path.isEmpty()) openDevice();}
	
	~PIFile() {close();}
	
	//PIFile & operator =(const PIFile & f) {path_ = f.path_; type_ = f.type_; return *this;}
	
	
	//! Immediate write all buffered data to disk
	void flush() {if (opened_) fflush(fd);}
	
	//! Move read/write position to "position"
	void seek(llong position) {if (!opened_) return; fseek(fd, position, SEEK_SET); clearerr(fd);}
	
	//! Move read/write position to the begin of the file
	void seekToBegin() {if (!opened_) return; fseek(fd, 0, SEEK_SET); clearerr(fd);}
	
	//! Move read/write position to the end of the file
	void seekToEnd() {if (!opened_) return; fseek(fd, 0, SEEK_END); clearerr(fd);}
	
	//! Move read/write position to text line number "line"
	void seekToLine(llong line) {if (!opened_) return; seekToBegin(); piForTimes (line) readLine(); clearerr(fd);} // line 0 - begin of file
	//void fill(char c) {stream.fill(c);}
	
	//! Read one char and return it
	char readChar() {return (char)fgetc(fd);}
	
	//! Read one text line and return it
	PIString readLine();
	
	//! Read all file content to "data" and return readed bytes count. Position leaved unchanged
	llong readAll(void * data);
	
	//! Read all file content to byte array and return it. Position leaved unchanged
	PIByteArray readAll(bool forceRead = false);
	
	
	//! Set file path to "path" and reopen file if need
	void setPath(const PIString & path) {PIIODevice::setPath(path); if (opened_) openDevice();}
	
	//! Returns file size
	llong size();
	
	//! Returns read/write position
	llong pos() {if (!opened_) return -1; return ftell(fd);}
	
	//! Returns if position is at the end of file
	bool isEnd() {if (!opened_) return true; return (feof(fd) || ferror(fd));}
	
	//! Returns if file is empty
	bool isEmpty() {return (size() <= 0);}
	
	
	//! Returns float numbers write precision
	int precision() const {return prec_;}
	
	//! Set float numbers write precision to "prec_" digits
	void setPrecision(int prec) {prec_ = prec; if (prec_ >= 0) prec_str = "." + itos(prec_); else prec_str = "";}
	
	
	//! Read from file to "read_to" no more than "max_size" and return readed bytes count
	int read(void * read_to, int max_size) {if (!canRead() || fd == 0) return -1; return fread(read_to, 1, max_size, fd);}
	
	//! Write to file "data" with size "max_size" and return written bytes count
	int write(const void * data, int max_size) {if (!canWrite() || fd == 0) return -1; return fwrite(data, 1, max_size, fd);}
	
	PIFile & writeToBinLog(ushort id, const void * data, int size) {if (!isWriteable() || fd == 0) return *this; writeBinary(id).writeBinary((ushort)size); write(data, size); flush(); return *this;}
	
	
	//! Write to file binary content of "v"
	PIFile & writeBinary(const char v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const short v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const int v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const long v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const llong v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const uchar v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const ushort v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const uint v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const ulong v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const ullong v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const float v) {write(&v, sizeof(v)); return *this;}
	//! Write to file binary content of "v"
	PIFile & writeBinary(const double v) {write(&v, sizeof(v)); return *this;}
	
	PIFile & operator =(const PIFile & f) {PIIODevice::setPath(f.path()); mode_ = f.mode_; return *this;}
	
	//! Write to file text representation of "v"
	PIFile & operator <<(const char v) {if (canWrite() && fd != 0) write(&v, 1); return *this;}
	//PIFile & operator <<(const string & v) {write(v.c_str(), v.size()); return *this;}
	//! Write to file string "v"
	PIFile & operator <<(const PIString & v) {if (canWrite() && fd != 0) write(v.data(), v.lengthAscii()); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(const PIByteArray & v) {if (canWrite() && fd != 0) write(v.data(), v.size()); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(short v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%hd", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(int v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%d", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(long v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%ld", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(llong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%lld", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(uchar v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%u", int(v)); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(ushort v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%hu", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(uint v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%u", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(ulong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%lu", v); return *this;}
	//! Write to file text representation of "v"
	PIFile & operator <<(ullong v) {if (canWrite() && fd != 0) ret = fprintf(fd, "%llu", v); return *this;}
	//! Write to file text representation of "v" with precision \a precision()
	PIFile & operator <<(float v) {if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "f").c_str(), v); return *this;}
	//! Write to file text representation of "v" with precision \a precision()
	PIFile & operator <<(double v) {if (canWrite() && fd != 0) ret = fprintf(fd, ("%" + prec_str + "lf").c_str(), v); return *this;}
	
	
	//! Read from file text representation of "v"
	PIFile & operator >>(char & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(short & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(int & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(long & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(llong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(uchar & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hhn", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(ushort & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%hn", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(uint & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%n", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(ulong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%ln", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(ullong & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lln", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(float & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%f", &v); return *this;}
	//! Read from file text representation of "v"
	PIFile & operator >>(double & v) {if (canRead() && fd != 0) ret = fscanf(fd, "%lf", &v); return *this;}
	
	PIString constructFullPath() const;
	
	EVENT_HANDLER(void, clear) {close(); fd = fopen(path().data(), "w"); if (fd != 0) fclose(fd); fd = 0; opened_ = false; open();}
	EVENT_HANDLER0(void, remove) {close(); std::remove(path().data());}
	EVENT_HANDLER1(void, resize, llong, new_size) {resize(new_size, 0);}
	EVENT_HANDLER2(void, resize, llong, new_size, uchar, fill);
	
	
	//! Returns not opened temporary file with open mode "mode"
	static PIFile openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite) {return PIFile(PIString(tmpnam(0)), mode);}
	
	//! Returns if file with path "path" does exists
	static bool isExists(const PIString & path);
	
	//! Remove file with path "path" and returns if remove was successful
	static bool remove(const PIString & path) {return std::remove(path.data()) == 0;}
	
//! \handlers
//! \{

	//! \fn void clear()
	//! \brief Raise on new TCP connection received
	
	//! \fn void resize(llong new_size)
	//! \brief Resize file to "new_size" with "fill" filling
	
	//! \fn void resize(llong new_size, uchar fill)
	//! \brief Resize file to "new_size" with "fill" filling

	//! \fn void remove()
	//! \brief Remove file

//! \}
//! \ioparams
//! \{
#ifdef DOXYGEN
#endif
//! \}

protected:
	PIString fullPathPrefix() const {return "file";}
	void configureFromFullPath(const PIString & full_path);
	bool openDevice();
	bool closeDevice();

private:
	PIString strType(const PIIODevice::DeviceMode type) {switch (type) {case PIIODevice::ReadOnly: return "rb"; case WriteOnly: return "ab"; case ReadWrite: return "a+b";} return "rb";}

	FILE * fd;
	int ret, prec_;
	string prec_str;

};

#endif // PIFILE_H
