Subversion Repositories spk

Rev

Rev 223 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
170 cycrow 82
	size_t readLines(std::vector<Utils::String>& to) const;
197 cycrow 83
	size_t readLines(std::vector<Utils::WString>& to) const;
227 cycrow 84
	size_t readLinesUTF(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);
227 cycrow 88
	bool writeFile(const std::vector<Utils::WString>& lines) const;
196 cycrow 89
	bool writeFile(Utils::WStringList* lines);
227 cycrow 90
	bool writeFileUTF(const std::vector<Utils::WString>* lines);
91
	bool writeFileUTF(const 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
 
160 cycrow 105
	CDirIO &GetDirIO() { return _sDirIO; }
1 cycrow 106
 
160 cycrow 107
	bool NoFile () { return _sFilename.empty(); }
1 cycrow 108
	size_t GetFilesize () { return m_lSize; }
109
 
110
	char *ReadToData ( size_t *size );
111
	bool WriteData ( const char *data, size_t size );
82 cycrow 112
	bool writeString ( const Utils::String &data );
1 cycrow 113
 
114
	bool WritePartFile ( size_t *offsets, size_t numOffset );
115
	int TruncateFile ( size_t offset, size_t datasize );
116
	bool WipeFile();
117
 
52 cycrow 118
	bool exists() const;
82 cycrow 119
	bool appendFile ( const Utils::String &filename );
1 cycrow 120
	bool AppendData ( const char *d, size_t size );
121
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
122
 
196 cycrow 123
	bool isFileExtension(const Utils::WString &ext) { return (extension().Compare(ext)) ? true : false; }
119 cycrow 124
 
196 cycrow 125
	Utils::WString extension() const;
126
	Utils::WString getWindowsFilename() const;
127
	Utils::WString changeFileExtension(const Utils::WString &ext) const;
1 cycrow 128
 
196 cycrow 129
	void setDir(const Utils::WString &dir);
52 cycrow 130
	void setAutoDelete(bool bDelete);
1 cycrow 131
 
132
private:
52 cycrow 133
	int _in() const;
134
	int _out() const;
135
	int _append() const;
136
	int _inout() const;
137
	bool _start(int iFlags, bool bSeekP);
51 cycrow 138
 
139
	void _readFileSize ();
52 cycrow 140
	void _seek(int iPos, int iFrom);
51 cycrow 141
 
197 cycrow 142
	bool _write(const char* buf, va_list args);
143
	bool _write(const wchar_t* buf, va_list args);
52 cycrow 144
 
160 cycrow 145
	void _updateFilename();
146
 
197 cycrow 147
	std::string _fileData() const;
148
	static std::string _toFileData(const Utils::WString &str);
196 cycrow 149
 
51 cycrow 150
private:
196 cycrow 151
	Utils::WString	_sFilename;
152
	Utils::WString	_sFile;
153
	Utils::WString	_sExt;
160 cycrow 154
	CDirIO			_sDirIO;
1 cycrow 155
 
52 cycrow 156
	bool m_bSeekP;
1 cycrow 157
	bool m_bBinary;
52 cycrow 158
	bool m_bAutoDelete;
1 cycrow 159
 
52 cycrow 160
	std::fstream m_fId;
1 cycrow 161
 
162
	size_t m_lSize;
163
};
164
 
121 cycrow 165
#pragma warning( pop ) 
1 cycrow 166
#endif //__FILE_IO_H__
167