148 |
cycrow |
1 |
#ifndef __FILE_IO_H__
|
|
|
2 |
#define __FILE_IO_H__
|
|
|
3 |
|
|
|
4 |
#pragma warning( push )
|
|
|
5 |
#pragma warning( disable : 4251)
|
|
|
6 |
|
|
|
7 |
#include <fstream>
|
|
|
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 |
namespace XLib
|
|
|
24 |
{
|
|
|
25 |
|
|
|
26 |
class C_File;
|
|
|
27 |
|
|
|
28 |
enum { FILEERR_NONE, FILEERR_NOFILE, FILEERR_NOOPEN, FILEERR_TOSMALL, FILEERR_NOWRITE };
|
|
|
29 |
|
|
|
30 |
class SPKEXPORT FileIO
|
|
|
31 |
{
|
|
|
32 |
public:
|
|
|
33 |
static bool Remove(const XLib::String& filename);
|
|
|
34 |
static bool Exists(const XLib::String& filename);
|
|
|
35 |
|
|
|
36 |
public:
|
|
|
37 |
FileIO();
|
|
|
38 |
FileIO(const XLib::String& sFilename, bool bBinary);
|
|
|
39 |
FileIO(C_File* file);
|
|
|
40 |
~FileIO();
|
|
|
41 |
|
|
|
42 |
bool startWrite();
|
|
|
43 |
bool startRead();
|
|
|
44 |
bool startModify();
|
|
|
45 |
bool startAppend();
|
|
|
46 |
bool atEnd() const;
|
|
|
47 |
|
|
|
48 |
size_t fileSize() const;
|
|
|
49 |
|
|
|
50 |
time_t modifiedTime();
|
|
|
51 |
time_t GetCreationTime();
|
|
|
52 |
void SetCreationTime(time_t time);
|
|
|
53 |
|
|
|
54 |
bool writeSize(size_t iSize);
|
|
|
55 |
bool write(FileIO& file, size_t iSize);
|
|
|
56 |
bool write(const unsigned char* buf, ...);
|
|
|
57 |
bool write(const char* buf, ...);
|
|
|
58 |
bool write(const char* buf, size_t iSize);
|
|
|
59 |
bool write(const unsigned char* buf, size_t iSize);
|
|
|
60 |
bool put(const unsigned char c);
|
|
|
61 |
|
|
|
62 |
void close();
|
|
|
63 |
|
|
|
64 |
XLib::String readEndOfLine();
|
|
|
65 |
|
|
|
66 |
void seek(size_t iPos);
|
|
|
67 |
void seekEnd(size_t iPos = 0);
|
|
|
68 |
void seekStart(size_t iPos = 0);
|
|
|
69 |
size_t position();
|
|
|
70 |
|
|
|
71 |
int readSize();
|
|
|
72 |
bool read(unsigned char* buf, size_t iSize, bool bEndChar = false);
|
|
|
73 |
unsigned char* read(size_t iAmount);
|
|
|
74 |
unsigned char* readAll(size_t* pSize = NULL);
|
|
|
75 |
|
|
|
76 |
std::fstream& stream();
|
|
|
77 |
|
|
|
78 |
StringList readLines() const;
|
|
|
79 |
void readLines(StringList &files) const;
|
|
|
80 |
|
|
|
81 |
bool writeFile(const StringList &lines) const;
|
|
|
82 |
bool writeFileUTF(const StringList& lines) const;
|
|
|
83 |
|
|
|
84 |
bool remove();
|
|
|
85 |
bool rename(const XLib::String &toFile);
|
|
|
86 |
bool copy(const XLib::String& toFile, bool keepTime = false);
|
|
|
87 |
|
|
|
88 |
bool isOpened() const;
|
|
|
89 |
//bool Open ( CyString filename, bool = true );
|
|
|
90 |
bool open(const XLib::String& sFilename, bool bBinary = true);
|
|
|
91 |
|
|
|
92 |
XLib::String baseName() const;
|
|
|
93 |
const XLib::String& fullFilename() const { return m_sFilename; }
|
|
|
94 |
const XLib::String& filename() const { return m_sFile; }
|
|
|
95 |
const XLib::String& dir() const { return m_sDirIO.dir(); }
|
|
|
96 |
DirIO& dirIO() { return m_sDirIO; }
|
|
|
97 |
|
|
|
98 |
bool noFile() { return m_sFilename.empty(); }
|
|
|
99 |
size_t getFilesize() { return m_lSize; }
|
|
|
100 |
|
|
|
101 |
char* ReadToData(size_t* size);
|
|
|
102 |
bool WriteData(const char* data, size_t size);
|
|
|
103 |
bool writeString(const XLib::String& data);
|
|
|
104 |
|
|
|
105 |
bool WritePartFile(size_t* offsets, size_t numOffset);
|
|
|
106 |
int TruncateFile(size_t offset, size_t datasize);
|
|
|
107 |
bool WipeFile();
|
|
|
108 |
|
|
|
109 |
bool exists() const;
|
|
|
110 |
bool ExistsOld() const;
|
|
|
111 |
bool appendFile(const XLib::String& filename);
|
|
|
112 |
bool AppendData(const char* d, size_t size);
|
|
|
113 |
bool AppendDataToPos(const char* d, size_t size, size_t start);
|
|
|
114 |
|
|
|
115 |
bool isFileExtension(const XLib::String& ext) { return (extension().Compare(ext)) ? true : false; }
|
|
|
116 |
|
|
|
117 |
XLib::String extension();
|
|
|
118 |
XLib::String windowsFilename();
|
|
|
119 |
XLib::String changeFileExtension(const XLib::String& ext) const;
|
|
|
120 |
|
|
|
121 |
void setDir(const XLib::String& dir);
|
|
|
122 |
void setAutoDelete(bool bDelete);
|
|
|
123 |
|
|
|
124 |
private:
|
|
|
125 |
int _in() const;
|
|
|
126 |
int _out() const;
|
|
|
127 |
int _append() const;
|
|
|
128 |
int _inout() const;
|
|
|
129 |
bool _start(int iFlags, bool bSeekP);
|
|
|
130 |
|
|
|
131 |
void _readFileSize();
|
|
|
132 |
void _seek(int iPos, int iFrom);
|
|
|
133 |
|
|
|
134 |
bool _write(const char* buf, va_list args);
|
|
|
135 |
|
|
|
136 |
private:
|
|
|
137 |
XLib::String m_sFilename;
|
|
|
138 |
XLib::String m_sFile;
|
|
|
139 |
DirIO m_sDirIO;
|
|
|
140 |
|
|
|
141 |
bool m_bSeekP;
|
|
|
142 |
bool m_bBinary;
|
|
|
143 |
bool m_bAutoDelete;
|
|
|
144 |
|
|
|
145 |
std::fstream m_fId;
|
|
|
146 |
|
|
|
147 |
size_t m_lSize;
|
|
|
148 |
};
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
#pragma warning( pop )
|
|
|
153 |
#endif //__FILE_IO_H__
|
|
|
154 |
|