Subversion Repositories spk

Rev

Rev 211 | Rev 222 | 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
 
196 cycrow 7
#include "Utils/WStringList.h"
111 cycrow 8
#include "Utils/StringList.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::CStringList* lines);
92
	bool writeFile(Utils::WStringList* lines);
1 cycrow 93
 
52 cycrow 94
	bool remove();
196 cycrow 95
	bool Rename(const Utils::WString &toFile);
96
	bool copy(const Utils::WString &toFile, bool keepTime = false);
1 cycrow 97
 
52 cycrow 98
	bool isOpened() const;
196 cycrow 99
	bool open(const Utils::WString& sFilename, bool bBinary = true);
1 cycrow 100
 
196 cycrow 101
	Utils::WString baseName() const;
102
	const Utils::WString& fullFilename() const;
103
	const Utils::WString& filename() const;
104
	const Utils::WString& dir() const { return _sDirIO.dir(); }
105
 
106
	//TODO: remove these
107
	const Utils::String fullFilenameStr() const;
108
	const Utils::String filenameStr() const;
109
	const Utils::String dirStr() const { return _sDirIO.dir().toString(); }
160 cycrow 110
	CDirIO &GetDirIO() { return _sDirIO; }
1 cycrow 111
 
160 cycrow 112
	bool NoFile () { return _sFilename.empty(); }
1 cycrow 113
	size_t GetFilesize () { return m_lSize; }
114
 
115
	char *ReadToData ( size_t *size );
116
	bool WriteData ( const char *data, size_t size );
82 cycrow 117
	bool writeString ( const Utils::String &data );
1 cycrow 118
 
119
	bool WritePartFile ( size_t *offsets, size_t numOffset );
120
	int TruncateFile ( size_t offset, size_t datasize );
121
	bool WipeFile();
122
 
52 cycrow 123
	bool exists() const;
124
	bool ExistsOld () const;
82 cycrow 125
	bool appendFile ( const Utils::String &filename );
1 cycrow 126
	bool AppendData ( const char *d, size_t size );
127
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
128
 
196 cycrow 129
	bool isFileExtension(const Utils::WString &ext) { return (extension().Compare(ext)) ? true : false; }
119 cycrow 130
 
196 cycrow 131
	Utils::WString extension() const;
132
	Utils::WString getWindowsFilename() const;
133
	Utils::WString changeFileExtension(const Utils::WString &ext) const;
1 cycrow 134
 
196 cycrow 135
	void setDir(const Utils::WString &dir);
52 cycrow 136
	void setAutoDelete(bool bDelete);
1 cycrow 137
 
138
private:
52 cycrow 139
	int _in() const;
140
	int _out() const;
141
	int _append() const;
142
	int _inout() const;
143
	bool _start(int iFlags, bool bSeekP);
51 cycrow 144
 
145
	void _readFileSize ();
52 cycrow 146
	void _seek(int iPos, int iFrom);
51 cycrow 147
 
197 cycrow 148
	bool _write(const char* buf, va_list args);
149
	bool _write(const wchar_t* buf, va_list args);
52 cycrow 150
 
160 cycrow 151
	void _updateFilename();
152
 
197 cycrow 153
	std::string _fileData() const;
154
	static std::string _toFileData(const Utils::WString &str);
196 cycrow 155
 
51 cycrow 156
private:
196 cycrow 157
	Utils::WString	_sFilename;
158
	Utils::WString	_sFile;
159
	Utils::WString	_sExt;
160 cycrow 160
	CDirIO			_sDirIO;
1 cycrow 161
 
52 cycrow 162
	bool m_bSeekP;
1 cycrow 163
	bool m_bBinary;
52 cycrow 164
	bool m_bAutoDelete;
1 cycrow 165
 
52 cycrow 166
	std::fstream m_fId;
1 cycrow 167
 
168
	size_t m_lSize;
169
};
170
 
121 cycrow 171
#pragma warning( pop ) 
1 cycrow 172
#endif //__FILE_IO_H__
173