Subversion Repositories spk

Rev

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