Subversion Repositories spk

Rev

Rev 51 | Rev 53 | 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();
46
	bool AtEnd();
52 cycrow 47
	bool atEnd();
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);
68
	void seekEnd();
69
	void seekStart(unsigned int iPos = 0);
70
 
71
	bool read(unsigned char *buf, size_t iSize, bool bEndChar = false);
72
	unsigned char *read(size_t iAmount);
73
	unsigned char *readAll(size_t *pSize = NULL);
74
 
75
	std::fstream &stream();
76
 
1 cycrow 77
	std::vector<CyString> *ReadLines();
78
	CyStringList *ReadLinesStr();
79
	bool WriteFile(std::vector<CyString> *lines);
80
	bool WriteFileUTF(std::vector<CyString> *lines);
81
	bool WriteFile(CyStringList *lines);
82
 
52 cycrow 83
	bool remove();
1 cycrow 84
	bool Rename(CyString toFile);
52 cycrow 85
	bool copy(const Utils::String &toFile, bool keepTime = false);
1 cycrow 86
 
52 cycrow 87
	bool isOpened() const;
1 cycrow 88
	bool Open ( CyString filename, bool = true );
89
 
90
	CyString GetBaseName();
91
	CyString GetFullFilename () { return m_sFilename; }
92
	CyString GetFilename () { return m_sFile; }
93
	CyString GetDir() { return m_sDirIO.Dir(); }
94
	CDirIO &GetDirIO() { return m_sDirIO; }
95
 
51 cycrow 96
	bool NoFile () { return m_sFilename.empty(); }
1 cycrow 97
	size_t GetFilesize () { return m_lSize; }
98
 
99
	char *ReadToData ( size_t *size );
100
	bool WriteData ( const char *data, size_t size );
101
	bool WriteString ( CyString data );
102
 
103
	bool WritePartFile ( size_t *offsets, size_t numOffset );
104
	int TruncateFile ( size_t offset, size_t datasize );
105
	bool WipeFile();
106
 
52 cycrow 107
	bool exists() const;
108
	bool ExistsOld () const;
1 cycrow 109
	bool AppendFile ( CyString filename );
110
	bool AppendData ( const char *d, size_t size );
111
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
112
 
113
	bool CheckFileExtension(CyString ext) { return (GetFileExtension().Compare(ext)) ? true : false; }
114
	CyString GetFileExtension ();
115
	CyString ChangeFileExtension ( CyString ext );
116
	CyString GetWindowsFilename();
117
 
118
	void SetDir ( CyString dir );
52 cycrow 119
	void setAutoDelete(bool bDelete);
1 cycrow 120
 
121
private:
52 cycrow 122
	int _in() const;
123
	int _out() const;
124
	int _append() const;
125
	int _inout() const;
126
	bool _start(int iFlags, bool bSeekP);
51 cycrow 127
 
128
	void _readFileSize ();
52 cycrow 129
	void _seek(int iPos, int iFrom);
51 cycrow 130
 
52 cycrow 131
	bool _write(const char *buf, va_list args);
132
 
51 cycrow 133
private:
134
	Utils::String m_sFilename;
135
	Utils::String m_sFile;
1 cycrow 136
	CDirIO   m_sDirIO;
137
 
52 cycrow 138
	bool m_bSeekP;
1 cycrow 139
	bool m_bBinary;
52 cycrow 140
	bool m_bAutoDelete;
1 cycrow 141
 
52 cycrow 142
	std::fstream m_fId;
1 cycrow 143
 
144
	size_t m_lSize;
145
};
146
 
147
#endif //__FILE_IO_H__
148