| 1 | cycrow | 1 | #ifndef __FILE_IO_H__
 | 
        
           |  |  | 2 | #define __FILE_IO_H__
 | 
        
           |  |  | 3 |   | 
        
           | 121 | cycrow | 4 | #pragma warning( push )
 | 
        
           |  |  | 5 | #pragma warning( disable : 4251)
 | 
        
           |  |  | 6 |   | 
        
           | 222 | cycrow | 7 | #include "Utils/String.h"
 | 
        
           | 196 | cycrow | 8 | #include "Utils/WStringList.h"
 | 
        
           | 1 | cycrow | 9 | #include <fstream>
 | 
        
           |  |  | 10 | #include <vector>
 | 
        
           |  |  | 11 | #include "DirIO.h"
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 | #include <time.h>
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 | #if defined(_WIN32) || defined(OS2) || defined(MSDOS)
 | 
        
           |  |  | 16 | #include <fcntl.h>
 | 
        
           |  |  | 17 | #include <io.h>
 | 
        
           |  |  | 18 | #define MY_SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
 | 
        
           |  |  | 19 | #else
 | 
        
           |  |  | 20 | #define MY_SET_BINARY_MODE(file)
 | 
        
           |  |  | 21 | #endif
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | #define EPOCH_DIFF 0x019DB1DED53E8000LL /* 116444736000000000 nsecs */
 | 
        
           |  |  | 24 | #define RATE_DIFF 10000000 /* 100 nsecs */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | class C_File;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | enum {FILEERR_NONE, FILEERR_NOFILE, FILEERR_NOOPEN, FILEERR_TOSMALL, FILEERR_NOWRITE};
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | class SPKEXPORT CFileIO
 | 
        
           |  |  | 31 | {
 | 
        
           |  |  | 32 | public:
 | 
        
           | 196 | cycrow | 33 | 	static bool Remove(const Utils::WString &filename);
 | 
        
           | 197 | cycrow | 34 | 	static bool Exists(const Utils::WString& filename);
 | 
        
           |  |  | 35 | 	static bool Rename(const Utils::WString& from, const Utils::WString& to);
 | 
        
           | 52 | cycrow | 36 |   | 
        
           |  |  | 37 | public:
 | 
        
           |  |  | 38 | 	CFileIO();
 | 
        
           | 196 | cycrow | 39 | 	CFileIO(const Utils::WString &sFilename, bool bBinary);
 | 
        
           |  |  | 40 | 	CFileIO(const Utils::WString &filename);
 | 
        
           | 52 | cycrow | 41 | 	CFileIO(C_File *file);
 | 
        
           | 1 | cycrow | 42 | 	~CFileIO ();
 | 
        
           |  |  | 43 |   | 
        
           | 52 | cycrow | 44 | 	bool startWrite();
 | 
        
           |  |  | 45 | 	bool startRead();
 | 
        
           |  |  | 46 | 	bool startModify();
 | 
        
           |  |  | 47 | 	bool startAppend();
 | 
        
           | 56 | cycrow | 48 | 	bool atEnd() const;
 | 
        
           | 1 | cycrow | 49 |   | 
        
           | 52 | cycrow | 50 | 	size_t fileSize() const;
 | 
        
           |  |  | 51 |   | 
        
           | 160 | cycrow | 52 | 	time_t modifiedTime() const;
 | 
        
           |  |  | 53 | 	time_t creationTime() const;
 | 
        
           |  |  | 54 | 	void setCreationTime(time_t time);
 | 
        
           | 1 | cycrow | 55 |   | 
        
           | 123 | cycrow | 56 | 	bool writeSize(size_t iSize);
 | 
        
           | 52 | cycrow | 57 | 	bool write(CFileIO &file, size_t iSize);
 | 
        
           |  |  | 58 | 	bool write(const unsigned char *buf, ...);
 | 
        
           |  |  | 59 | 	bool write(const char *buf, ...);
 | 
        
           | 123 | cycrow | 60 | 	bool write(const char *buf, size_t iSize);
 | 
        
           |  |  | 61 | 	bool write(const unsigned char *buf, size_t iSize);
 | 
        
           | 52 | cycrow | 62 | 	bool put(const unsigned char c);
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 | 	void close();
 | 
        
           |  |  | 65 |   | 
        
           | 197 | cycrow | 66 | 	Utils::String readEndOfLineStr();
 | 
        
           |  |  | 67 | 	Utils::WString readEndOfLine();
 | 
        
           | 52 | cycrow | 68 |   | 
        
           | 123 | cycrow | 69 | 	void seek(size_t iPos);
 | 
        
           |  |  | 70 | 	void seekEnd(size_t iPos = 0);
 | 
        
           |  |  | 71 | 	void seekStart(size_t iPos = 0);
 | 
        
           | 58 | cycrow | 72 | 	size_t position();
 | 
        
           | 52 | cycrow | 73 |   | 
        
           | 56 | cycrow | 74 | 	int readSize();
 | 
        
           | 197 | cycrow | 75 | 	bool read(unsigned char* buf, size_t iSize, bool bEndChar = false);
 | 
        
           |  |  | 76 | 	bool read(wchar_t* buf, size_t iSize, bool bEndChar = false);
 | 
        
           | 52 | cycrow | 77 | 	unsigned char *read(size_t iAmount);
 | 
        
           |  |  | 78 | 	unsigned char *readAll(size_t *pSize = NULL);
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 | 	std::fstream &stream();
 | 
        
           |  |  | 81 |   | 
        
           | 166 | cycrow | 82 | 	std::vector<Utils::String>* readLines() const;
 | 
        
           | 170 | cycrow | 83 | 	size_t readLines(std::vector<Utils::String>& to) const;
 | 
        
           | 197 | cycrow | 84 | 	size_t readLines(std::vector<Utils::WString>& to) const;
 | 
        
           | 211 | cycrow | 85 | 	size_t readLines(Utils::WStringList& to) const;
 | 
        
           | 111 | cycrow | 86 |   | 
        
           | 197 | cycrow | 87 | 	bool writeFile(std::vector<Utils::String>* lines);
 | 
        
           |  |  | 88 | 	bool writeFile(const std::vector<Utils::WString> &lines) const;
 | 
        
           |  |  | 89 | 	bool writeFileUTF(std::vector<Utils::String>* lines);
 | 
        
           |  |  | 90 | 	bool writeFileUTF(std::vector<Utils::WString>* lines);
 | 
        
           | 196 | cycrow | 91 | 	bool writeFile(Utils::WStringList* lines);
 | 
        
           | 1 | cycrow | 92 |   | 
        
           | 52 | cycrow | 93 | 	bool remove();
 | 
        
           | 196 | cycrow | 94 | 	bool Rename(const Utils::WString &toFile);
 | 
        
           |  |  | 95 | 	bool copy(const Utils::WString &toFile, bool keepTime = false);
 | 
        
           | 1 | cycrow | 96 |   | 
        
           | 52 | cycrow | 97 | 	bool isOpened() const;
 | 
        
           | 196 | cycrow | 98 | 	bool open(const Utils::WString& sFilename, bool bBinary = true);
 | 
        
           | 1 | cycrow | 99 |   | 
        
           | 196 | cycrow | 100 | 	Utils::WString baseName() const;
 | 
        
           |  |  | 101 | 	const Utils::WString& fullFilename() const;
 | 
        
           |  |  | 102 | 	const Utils::WString& filename() const;
 | 
        
           |  |  | 103 | 	const Utils::WString& dir() const { return _sDirIO.dir(); }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 | 	//TODO: remove these
 | 
        
           |  |  | 106 | 	const Utils::String fullFilenameStr() const;
 | 
        
           |  |  | 107 | 	const Utils::String filenameStr() const;
 | 
        
           |  |  | 108 | 	const Utils::String dirStr() const { return _sDirIO.dir().toString(); }
 | 
        
           | 160 | cycrow | 109 | 	CDirIO &GetDirIO() { return _sDirIO; }
 | 
        
           | 1 | cycrow | 110 |   | 
        
           | 160 | cycrow | 111 | 	bool NoFile () { return _sFilename.empty(); }
 | 
        
           | 1 | cycrow | 112 | 	size_t GetFilesize () { return m_lSize; }
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 | 	char *ReadToData ( size_t *size );
 | 
        
           |  |  | 115 | 	bool WriteData ( const char *data, size_t size );
 | 
        
           | 82 | cycrow | 116 | 	bool writeString ( const Utils::String &data );
 | 
        
           | 1 | cycrow | 117 |   | 
        
           |  |  | 118 | 	bool WritePartFile ( size_t *offsets, size_t numOffset );
 | 
        
           |  |  | 119 | 	int TruncateFile ( size_t offset, size_t datasize );
 | 
        
           |  |  | 120 | 	bool WipeFile();
 | 
        
           |  |  | 121 |   | 
        
           | 52 | cycrow | 122 | 	bool exists() const;
 | 
        
           |  |  | 123 | 	bool ExistsOld () const;
 | 
        
           | 82 | cycrow | 124 | 	bool appendFile ( const Utils::String &filename );
 | 
        
           | 1 | cycrow | 125 | 	bool AppendData ( const char *d, size_t size );
 | 
        
           |  |  | 126 | 	bool AppendDataToPos ( const char *d, size_t size, size_t start );
 | 
        
           |  |  | 127 |   | 
        
           | 196 | cycrow | 128 | 	bool isFileExtension(const Utils::WString &ext) { return (extension().Compare(ext)) ? true : false; }
 | 
        
           | 119 | cycrow | 129 |   | 
        
           | 196 | cycrow | 130 | 	Utils::WString extension() const;
 | 
        
           |  |  | 131 | 	Utils::WString getWindowsFilename() const;
 | 
        
           |  |  | 132 | 	Utils::WString changeFileExtension(const Utils::WString &ext) const;
 | 
        
           | 1 | cycrow | 133 |   | 
        
           | 196 | cycrow | 134 | 	void setDir(const Utils::WString &dir);
 | 
        
           | 52 | cycrow | 135 | 	void setAutoDelete(bool bDelete);
 | 
        
           | 1 | cycrow | 136 |   | 
        
           |  |  | 137 | private:
 | 
        
           | 52 | cycrow | 138 | 	int _in() const;
 | 
        
           |  |  | 139 | 	int _out() const;
 | 
        
           |  |  | 140 | 	int _append() const;
 | 
        
           |  |  | 141 | 	int _inout() const;
 | 
        
           |  |  | 142 | 	bool _start(int iFlags, bool bSeekP);
 | 
        
           | 51 | cycrow | 143 |   | 
        
           |  |  | 144 | 	void _readFileSize ();
 | 
        
           | 52 | cycrow | 145 | 	void _seek(int iPos, int iFrom);
 | 
        
           | 51 | cycrow | 146 |   | 
        
           | 197 | cycrow | 147 | 	bool _write(const char* buf, va_list args);
 | 
        
           |  |  | 148 | 	bool _write(const wchar_t* buf, va_list args);
 | 
        
           | 52 | cycrow | 149 |   | 
        
           | 160 | cycrow | 150 | 	void _updateFilename();
 | 
        
           |  |  | 151 |   | 
        
           | 197 | cycrow | 152 | 	std::string _fileData() const;
 | 
        
           |  |  | 153 | 	static std::string _toFileData(const Utils::WString &str);
 | 
        
           | 196 | cycrow | 154 |   | 
        
           | 51 | cycrow | 155 | private:
 | 
        
           | 196 | cycrow | 156 | 	Utils::WString	_sFilename;
 | 
        
           |  |  | 157 | 	Utils::WString	_sFile;
 | 
        
           |  |  | 158 | 	Utils::WString	_sExt;
 | 
        
           | 160 | cycrow | 159 | 	CDirIO			_sDirIO;
 | 
        
           | 1 | cycrow | 160 |   | 
        
           | 52 | cycrow | 161 | 	bool m_bSeekP;
 | 
        
           | 1 | cycrow | 162 | 	bool m_bBinary;
 | 
        
           | 52 | cycrow | 163 | 	bool m_bAutoDelete;
 | 
        
           | 1 | cycrow | 164 |   | 
        
           | 52 | cycrow | 165 | 	std::fstream m_fId;
 | 
        
           | 1 | cycrow | 166 |   | 
        
           |  |  | 167 | 	size_t m_lSize;
 | 
        
           |  |  | 168 | };
 | 
        
           |  |  | 169 |   | 
        
           | 121 | cycrow | 170 | #pragma warning( pop ) 
 | 
        
           | 1 | cycrow | 171 | #endif //__FILE_IO_H__
 | 
        
           |  |  | 172 |   |