Subversion Repositories spk

Rev

Rev 52 | Rev 56 | 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);
53 cycrow 68
	void seekEnd(unsigned int iPos = 0);
52 cycrow 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();
53 cycrow 91
	const Utils::String &filename() const { return m_sFilename; }
1 cycrow 92
	CyString GetFullFilename () { return m_sFilename; }
93
	CyString GetFilename () { return m_sFile; }
94
	CyString GetDir() { return m_sDirIO.Dir(); }
95
	CDirIO &GetDirIO() { return m_sDirIO; }
96
 
51 cycrow 97
	bool NoFile () { return m_sFilename.empty(); }
1 cycrow 98
	size_t GetFilesize () { return m_lSize; }
99
 
100
	char *ReadToData ( size_t *size );
101
	bool WriteData ( const char *data, size_t size );
102
	bool WriteString ( CyString data );
103
 
104
	bool WritePartFile ( size_t *offsets, size_t numOffset );
105
	int TruncateFile ( size_t offset, size_t datasize );
106
	bool WipeFile();
107
 
52 cycrow 108
	bool exists() const;
109
	bool ExistsOld () const;
1 cycrow 110
	bool AppendFile ( CyString filename );
111
	bool AppendData ( const char *d, size_t size );
112
	bool AppendDataToPos ( const char *d, size_t size, size_t start );
113
 
114
	bool CheckFileExtension(CyString ext) { return (GetFileExtension().Compare(ext)) ? true : false; }
115
	CyString GetFileExtension ();
116
	CyString ChangeFileExtension ( CyString ext );
117
	CyString GetWindowsFilename();
118
 
119
	void SetDir ( CyString dir );
52 cycrow 120
	void setAutoDelete(bool bDelete);
1 cycrow 121
 
122
private:
52 cycrow 123
	int _in() const;
124
	int _out() const;
125
	int _append() const;
126
	int _inout() const;
127
	bool _start(int iFlags, bool bSeekP);
51 cycrow 128
 
129
	void _readFileSize ();
52 cycrow 130
	void _seek(int iPos, int iFrom);
51 cycrow 131
 
52 cycrow 132
	bool _write(const char *buf, va_list args);
133
 
51 cycrow 134
private:
135
	Utils::String m_sFilename;
136
	Utils::String m_sFile;
1 cycrow 137
	CDirIO   m_sDirIO;
138
 
52 cycrow 139
	bool m_bSeekP;
1 cycrow 140
	bool m_bBinary;
52 cycrow 141
	bool m_bAutoDelete;
1 cycrow 142
 
52 cycrow 143
	std::fstream m_fId;
1 cycrow 144
 
145
	size_t m_lSize;
146
};
147
 
148
#endif //__FILE_IO_H__
149