Subversion Repositories spk

Rev

Rev 57 | Rev 82 | 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
 
4
#include "CyString.h"
5
#include "StringList.h"
6
#include <fstream>
7
#include <vector>
8
#include "DirIO.h"
9
 
10
#include <time.h>
11
 
12
#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
13
#include <fcntl.h>
14
#include <io.h>
15
#define MY_SET_BINARY_MODE(file) setmode(fileno(file),O_BINARY)
16
#else
17
#define MY_SET_BINARY_MODE(file)
18
#endif
19
 
20
#define EPOCH_DIFF 0x019DB1DED53E8000LL /* 116444736000000000 nsecs */
21
#define RATE_DIFF 10000000 /* 100 nsecs */
22
 
23
class C_File;
24
 
25
enum {FILEERR_NONE, FILEERR_NOFILE, FILEERR_NOOPEN, FILEERR_TOSMALL, FILEERR_NOWRITE};
26
 
27
class SPKEXPORT CFileIO
28
{
29
public:
52 cycrow 30
	static bool Remove(const Utils::String &filename);
31
	static bool Exists(const Utils::String &filename);
32
 
33
public:
34
	CFileIO();
35
	//CFileIO(const Utils::String &sFilename, bool bBinary = true);
36
	CFileIO(CyString filename);
37
	CFileIO(C_File *file);
1 cycrow 38
	~CFileIO ();
39
 
52 cycrow 40
	bool startWrite();
41
	bool startRead();
42
	bool startModify();
43
	bool startAppend();
1 cycrow 44
	bool StartRead();
45
	void StopRead();
56 cycrow 46
	bool AtEnd() const;
47
	bool atEnd() const;
1 cycrow 48
 
52 cycrow 49
	size_t fileSize() const;
50
 
51
	time_t modifiedTime();
1 cycrow 52
	time_t GetCreationTime();
53
	void SetCreationTime(time_t time);
54
 
52 cycrow 55
	bool writeSize(unsigned int iSize);
56
	bool write(CFileIO &file, size_t iSize);
57
	bool write(const unsigned char *buf, ...);
58
	bool write(const char *buf, ...);
59
	bool write(const char *buf, unsigned int iSize);
60
	bool write(const unsigned char *buf, unsigned int iSize);
61
	bool put(const unsigned char c);
62
 
63
	void close();
64
 
65
	Utils::String readEndOfLine();
66
 
67
	void seek(unsigned int iPos);
53 cycrow 68
	void seekEnd(unsigned int iPos = 0);
52 cycrow 69
	void seekStart(unsigned int iPos = 0);
58 cycrow 70
	size_t position();
52 cycrow 71
 
56 cycrow 72
	int readSize();
52 cycrow 73
	bool read(unsigned char *buf, size_t iSize, bool bEndChar = false);
74
	unsigned char *read(size_t iAmount);
75
	unsigned char *readAll(size_t *pSize = NULL);
76
 
77
	std::fstream &stream();
78
 
1 cycrow 79
	std::vector<CyString> *ReadLines();
80
	CyStringList *ReadLinesStr();
81
	bool WriteFile(std::vector<CyString> *lines);
82
	bool WriteFileUTF(std::vector<CyString> *lines);
83
	bool WriteFile(CyStringList *lines);
84
 
52 cycrow 85
	bool remove();
1 cycrow 86
	bool Rename(CyString toFile);
52 cycrow 87
	bool copy(const Utils::String &toFile, bool keepTime = false);
1 cycrow 88
 
52 cycrow 89
	bool isOpened() const;
1 cycrow 90
	bool Open ( CyString filename, bool = true );
91
 
57 cycrow 92
	Utils::String baseName() const;
56 cycrow 93
	const Utils::String &fullFilename() const { return m_sFilename; }
94
	const Utils::String &filename() const { return m_sFile; }
1 cycrow 95
	CyString GetFullFilename () { return m_sFilename; }
96
	CyString GetFilename () { return m_sFile; }
97
	CyString GetDir() { return m_sDirIO.Dir(); }
98
	CDirIO &GetDirIO() { return m_sDirIO; }
99
 
51 cycrow 100
	bool NoFile () { return m_sFilename.empty(); }
1 cycrow 101
	size_t GetFilesize () { return m_lSize; }
102
 
103
	char *ReadToData ( size_t *size );
104
	bool WriteData ( const char *data, size_t size );
105
	bool WriteString ( CyString data );
106
 
107
	bool WritePartFile ( size_t *offsets, size_t numOffset );
108
	int TruncateFile ( size_t offset, size_t datasize );
109
	bool WipeFile();
110
 
52 cycrow 111
	bool exists() const;
112
	bool ExistsOld () const;
1 cycrow 113
	bool AppendFile ( CyString filename );
114
	bool AppendData ( const char *d, size_t size );
115
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
116
 
117
	bool CheckFileExtension(CyString ext) { return (GetFileExtension().Compare(ext)) ? true : false; }
58 cycrow 118
	Utils::String extension();
1 cycrow 119
	CyString GetFileExtension ();
120
	CyString ChangeFileExtension ( CyString ext );
121
	CyString GetWindowsFilename();
122
 
123
	void SetDir ( CyString dir );
52 cycrow 124
	void setAutoDelete(bool bDelete);
1 cycrow 125
 
126
private:
52 cycrow 127
	int _in() const;
128
	int _out() const;
129
	int _append() const;
130
	int _inout() const;
131
	bool _start(int iFlags, bool bSeekP);
51 cycrow 132
 
133
	void _readFileSize ();
52 cycrow 134
	void _seek(int iPos, int iFrom);
51 cycrow 135
 
52 cycrow 136
	bool _write(const char *buf, va_list args);
137
 
51 cycrow 138
private:
139
	Utils::String m_sFilename;
140
	Utils::String m_sFile;
1 cycrow 141
	CDirIO   m_sDirIO;
142
 
52 cycrow 143
	bool m_bSeekP;
1 cycrow 144
	bool m_bBinary;
52 cycrow 145
	bool m_bAutoDelete;
1 cycrow 146
 
52 cycrow 147
	std::fstream m_fId;
1 cycrow 148
 
149
	size_t m_lSize;
150
};
151
 
152
#endif //__FILE_IO_H__
153