Subversion Repositories spk

Rev

Rev 123 | Rev 160 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#ifndef __FILE_IO_H__
#define __FILE_IO_H__

#pragma warning( push )
#pragma warning( disable : 4251)

#include "CyString.h"
#include "StringList.h"
#include "Utils/StringList.h"
#include <fstream>
#include <vector>
#include "DirIO.h"

#include <time.h>

#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
#include <fcntl.h>
#include <io.h>
#define MY_SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
#else
#define MY_SET_BINARY_MODE(file)
#endif

#define EPOCH_DIFF 0x019DB1DED53E8000LL /* 116444736000000000 nsecs */
#define RATE_DIFF 10000000 /* 100 nsecs */

class C_File;

enum {FILEERR_NONE, FILEERR_NOFILE, FILEERR_NOOPEN, FILEERR_TOSMALL, FILEERR_NOWRITE};

class SPKEXPORT CFileIO
{
public:
        static bool Remove(const Utils::String &filename);
        static bool Exists(const Utils::String &filename);

public:
        CFileIO();
        CFileIO(const Utils::String &sFilename, bool bBinary);
        CFileIO(CyString filename);
        CFileIO(C_File *file);
        ~CFileIO ();

        bool startWrite();
        bool startRead();
        bool startModify();
        bool startAppend();
        bool atEnd() const;

        size_t fileSize() const;

        time_t modifiedTime();
        time_t GetCreationTime();
        void SetCreationTime(time_t time);

        bool writeSize(size_t iSize);
        bool write(CFileIO &file, size_t iSize);
        bool write(const unsigned char *buf, ...);
        bool write(const char *buf, ...);
        bool write(const char *buf, size_t iSize);
        bool write(const unsigned char *buf, size_t iSize);
        bool put(const unsigned char c);

        void close();

        Utils::String readEndOfLine();

        void seek(size_t iPos);
        void seekEnd(size_t iPos = 0);
        void seekStart(size_t iPos = 0);
        size_t position();

        int readSize();
        bool read(unsigned char *buf, size_t iSize, bool bEndChar = false);
        unsigned char *read(size_t iAmount);
        unsigned char *readAll(size_t *pSize = NULL);

        std::fstream &stream();

        std::vector<Utils::String> *readLines();
        std::vector<CyString> *ReadLines();
        CyStringList *ReadLinesStr();
        Utils::CStringList *readLinesStr();

        bool WriteFile(std::vector<CyString> *lines);
        bool WriteFileUTF(std::vector<CyString> *lines);
        bool WriteFile(CyStringList *lines);
        bool writeFile(std::vector<Utils::String> *lines);
        bool writeFileUTF(std::vector<Utils::String> *lines);
        bool writeFile(Utils::CStringList *lines);

        bool remove();
        bool Rename(CyString toFile);
        bool copy(const Utils::String &toFile, bool keepTime = false);

        bool isOpened() const;
        //bool Open ( CyString filename, bool = true );
        bool open(const Utils::String &sFilename, bool bBinary = true);

        Utils::String baseName() const;
        const Utils::String &fullFilename() const { return m_sFilename; }
        const Utils::String &filename() const { return m_sFile; }
        const Utils::String &dir() const { return m_sDirIO.dir(); }
        CyString GetFilename () { return m_sFile; }
        CDirIO &GetDirIO() { return m_sDirIO; }

        bool NoFile () { return m_sFilename.empty(); }
        size_t GetFilesize () { return m_lSize; }

        char *ReadToData ( size_t *size );
        bool WriteData ( const char *data, size_t size );
        bool writeString ( const Utils::String &data );

        bool WritePartFile ( size_t *offsets, size_t numOffset );
        int TruncateFile ( size_t offset, size_t datasize );
        bool WipeFile();
        
        bool exists() const;
        bool ExistsOld () const;
        bool AppendFile ( CyString filename );
        bool appendFile ( const Utils::String &filename );
        bool AppendData ( const char *d, size_t size );
        bool AppendDataToPos ( const char *d, size_t size, size_t start );

        bool CheckFileExtension(CyString ext) { return (GetFileExtension().Compare(ext)) ? true : false; }
        bool isFileExtension(const Utils::String &ext) { return (extension().Compare(ext)) ? true : false; }

        Utils::String extension();
        CyString GetFileExtension ();
        CyString ChangeFileExtension(CyString ext) const;
        CyString GetWindowsFilename();
        Utils::String changeFileExtension(const Utils::String &ext) const;

        void setDir(const Utils::String &dir);
        void SetDir ( CyString dir );
        void setAutoDelete(bool bDelete);

private:
        int _in() const;
        int _out() const;
        int _append() const;
        int _inout() const;
        bool _start(int iFlags, bool bSeekP);

        void _readFileSize ();
        void _seek(int iPos, int iFrom);

        bool _write(const char *buf, va_list args);

private:
        Utils::String m_sFilename;
        Utils::String m_sFile;
        CDirIO   m_sDirIO;

        bool m_bSeekP;
        bool m_bBinary;
        bool m_bAutoDelete;

        std::fstream m_fId;

        size_t m_lSize;
};

#pragma warning( pop ) 
#endif //__FILE_IO_H__