1 // Common/C_FileIO.h |
|
2 |
|
3 #include "C_FileIO.h" |
|
4 |
|
5 #include <fcntl.h> |
|
6 #include <unistd.h> |
|
7 |
|
8 namespace NC { |
|
9 namespace NFile { |
|
10 namespace NIO { |
|
11 |
|
12 bool CFileBase::OpenBinary(const char *name, int flags) |
|
13 { |
|
14 #ifdef O_BINARY |
|
15 flags |= O_BINARY; |
|
16 #endif |
|
17 Close(); |
|
18 _handle = ::open(name, flags, 0666); |
|
19 return _handle != -1; |
|
20 } |
|
21 |
|
22 bool CFileBase::Close() |
|
23 { |
|
24 if(_handle == -1) |
|
25 return true; |
|
26 if (close(_handle) != 0) |
|
27 return false; |
|
28 _handle = -1; |
|
29 return true; |
|
30 } |
|
31 |
|
32 bool CFileBase::GetLength(UInt64 &length) const |
|
33 { |
|
34 off_t curPos = Seek(0, SEEK_CUR); |
|
35 off_t lengthTemp = Seek(0, SEEK_END); |
|
36 Seek(curPos, SEEK_SET); |
|
37 length = (UInt64)lengthTemp; |
|
38 return true; |
|
39 } |
|
40 |
|
41 off_t CFileBase::Seek(off_t distanceToMove, int moveMethod) const |
|
42 { |
|
43 return ::lseek(_handle, distanceToMove, moveMethod); |
|
44 } |
|
45 |
|
46 ///////////////////////// |
|
47 // CInFile |
|
48 |
|
49 bool CInFile::Open(const char *name) |
|
50 { |
|
51 return CFileBase::OpenBinary(name, O_RDONLY); |
|
52 } |
|
53 |
|
54 bool CInFile::OpenShared(const char *name, bool) |
|
55 { |
|
56 return Open(name); |
|
57 } |
|
58 |
|
59 ssize_t CInFile::Read(void *data, size_t size) |
|
60 { |
|
61 return read(_handle, data, size); |
|
62 } |
|
63 |
|
64 ///////////////////////// |
|
65 // COutFile |
|
66 |
|
67 bool COutFile::Create(const char *name, bool createAlways) |
|
68 { |
|
69 if (createAlways) |
|
70 { |
|
71 Close(); |
|
72 _handle = ::creat(name, 0666); |
|
73 return _handle != -1; |
|
74 } |
|
75 return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY); |
|
76 } |
|
77 |
|
78 bool COutFile::Open(const char *name, DWORD creationDisposition) |
|
79 { |
|
80 return Create(name, false); |
|
81 } |
|
82 |
|
83 ssize_t COutFile::Write(const void *data, size_t size) |
|
84 { |
|
85 return write(_handle, data, size); |
|
86 } |
|
87 |
|
88 }}} |
|