Subversion Repositories spk

Rev

Rev 178 | Rev 185 | 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
 
1 cycrow 7
#include "CyString.h"
8
#include "StringList.h"
111 cycrow 9
#include "Utils/StringList.h"
1 cycrow 10
#include <fstream>
11
#include <vector>
12
#include "DirIO.h"
13
 
14
#include <time.h>
15
 
16
#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
17
#include <fcntl.h>
18
#include <io.h>
19
#define MY_SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
20
#else
21
#define MY_SET_BINARY_MODE(file)
22
#endif
23
 
24
#define EPOCH_DIFF 0x019DB1DED53E8000LL /* 116444736000000000 nsecs */
25
#define RATE_DIFF 10000000 /* 100 nsecs */
26
 
27
class C_File;
28
 
29
enum {FILEERR_NONE, FILEERR_NOFILE, FILEERR_NOOPEN, FILEERR_TOSMALL, FILEERR_NOWRITE};
30
 
31
class SPKEXPORT CFileIO
32
{
33
public:
52 cycrow 34
	static bool Remove(const Utils::String &filename);
35
	static bool Exists(const Utils::String &filename);
36
 
37
public:
38
	CFileIO();
121 cycrow 39
	CFileIO(const Utils::String &sFilename, bool bBinary);
52 cycrow 40
	CFileIO(CyString filename);
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
 
66
	Utils::String readEndOfLine();
67
 
123 cycrow 68
	void seek(size_t iPos);
69
	void seekEnd(size_t iPos = 0);
70
	void seekStart(size_t iPos = 0);
58 cycrow 71
	size_t position();
52 cycrow 72
 
56 cycrow 73
	int readSize();
52 cycrow 74
	bool read(unsigned char *buf, size_t iSize, bool bEndChar = false);
75
	unsigned char *read(size_t iAmount);
76
	unsigned char *readAll(size_t *pSize = NULL);
77
 
78
	std::fstream &stream();
79
 
166 cycrow 80
	std::vector<Utils::String>* readLines() const;
170 cycrow 81
	size_t readLines(std::vector<Utils::String>& to) const;
82
	size_t readLines(Utils::CStringList& to) const;
111 cycrow 83
	Utils::CStringList *readLinesStr();
84
 
121 cycrow 85
	bool writeFile(std::vector<Utils::String> *lines);
86
	bool writeFileUTF(std::vector<Utils::String> *lines);
87
	bool writeFile(Utils::CStringList *lines);
1 cycrow 88
 
52 cycrow 89
	bool remove();
158 cycrow 90
	bool Rename(const Utils::String &toFile);
52 cycrow 91
	bool copy(const Utils::String &toFile, bool keepTime = false);
1 cycrow 92
 
52 cycrow 93
	bool isOpened() const;
82 cycrow 94
	bool open(const Utils::String &sFilename, bool bBinary = true);
1 cycrow 95
 
57 cycrow 96
	Utils::String baseName() const;
160 cycrow 97
	const Utils::String &fullFilename() const { return _sFilename; }
98
	const Utils::String &filename() const { return _sFile; }
99
	const Utils::String &dir() const { return _sDirIO.dir(); }
100
	CDirIO &GetDirIO() { return _sDirIO; }
1 cycrow 101
 
160 cycrow 102
	bool NoFile () { return _sFilename.empty(); }
1 cycrow 103
	size_t GetFilesize () { return m_lSize; }
104
 
105
	char *ReadToData ( size_t *size );
106
	bool WriteData ( const char *data, size_t size );
82 cycrow 107
	bool writeString ( const Utils::String &data );
1 cycrow 108
 
109
	bool WritePartFile ( size_t *offsets, size_t numOffset );
110
	int TruncateFile ( size_t offset, size_t datasize );
111
	bool WipeFile();
112
 
52 cycrow 113
	bool exists() const;
114
	bool ExistsOld () const;
82 cycrow 115
	bool appendFile ( const Utils::String &filename );
1 cycrow 116
	bool AppendData ( const char *d, size_t size );
117
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
118
 
119 cycrow 119
	bool isFileExtension(const Utils::String &ext) { return (extension().Compare(ext)) ? true : false; }
120
 
160 cycrow 121
	Utils::String extension() const;
122
	Utils::String getWindowsFilename() const;
124 cycrow 123
	Utils::String changeFileExtension(const Utils::String &ext) const;
1 cycrow 124
 
118 cycrow 125
	void setDir(const Utils::String &dir);
52 cycrow 126
	void setAutoDelete(bool bDelete);
1 cycrow 127
 
128
private:
52 cycrow 129
	int _in() const;
130
	int _out() const;
131
	int _append() const;
132
	int _inout() const;
133
	bool _start(int iFlags, bool bSeekP);
51 cycrow 134
 
135
	void _readFileSize ();
52 cycrow 136
	void _seek(int iPos, int iFrom);
51 cycrow 137
 
52 cycrow 138
	bool _write(const char *buf, va_list args);
139
 
160 cycrow 140
	void _updateFilename();
141
 
51 cycrow 142
private:
160 cycrow 143
	Utils::String	_sFilename;
144
	Utils::String	_sFile;
145
	Utils::String	_sExt;
146
	CDirIO			_sDirIO;
1 cycrow 147
 
52 cycrow 148
	bool m_bSeekP;
1 cycrow 149
	bool m_bBinary;
52 cycrow 150
	bool m_bAutoDelete;
1 cycrow 151
 
52 cycrow 152
	std::fstream m_fId;
1 cycrow 153
 
154
	size_t m_lSize;
155
};
156
 
121 cycrow 157
#pragma warning( pop ) 
1 cycrow 158
#endif //__FILE_IO_H__
159