Subversion Repositories spk

Rev

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