Annotation of elwix/tools/oldlzma/SRC/Windows/FileIO.cpp, revision 1.1.1.1
1.1 misho 1: // Windows/FileIO.cpp
2:
3: #include "StdAfx.h"
4:
5: #include "FileIO.h"
6: #include "Defs.h"
7: #ifndef _UNICODE
8: #include "../Common/StringConvert.h"
9: #endif
10:
11: namespace NWindows {
12: namespace NFile {
13: namespace NIO {
14:
15: CFileBase::~CFileBase()
16: {
17: Close();
18: }
19:
20: bool CFileBase::Create(LPCTSTR fileName, DWORD desiredAccess,
21: DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
22: {
23: Close();
24: _handle = ::CreateFile(fileName, desiredAccess, shareMode,
25: (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
26: flagsAndAttributes, (HANDLE) NULL);
27: _fileIsOpen = _handle != INVALID_HANDLE_VALUE;
28: return _fileIsOpen;
29: }
30:
31: #ifndef _UNICODE
32: bool CFileBase::Create(LPCWSTR fileName, DWORD desiredAccess,
33: DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
34: {
35: Close();
36: // MessageBoxW(0, fileName, 0, 0);
37: // ::SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
38: _handle = ::CreateFileW(fileName, desiredAccess, shareMode,
39: (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
40: flagsAndAttributes, (HANDLE) NULL);
41: if ((_handle == INVALID_HANDLE_VALUE || _handle == 0) &&
42: ::GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
43: return Create(UnicodeStringToMultiByte(fileName, ::AreFileApisANSI() ? CP_ACP : CP_OEMCP),
44: desiredAccess, shareMode, creationDisposition, flagsAndAttributes);
45: return (_fileIsOpen = _handle != INVALID_HANDLE_VALUE);
46: }
47: #endif
48:
49: bool CFileBase::Close()
50: {
51: if(!_fileIsOpen)
52: return true;
53: bool result = BOOLToBool(::CloseHandle(_handle));
54: _fileIsOpen = !result;
55: return result;
56: }
57:
58: bool CFileBase::GetPosition(UINT64 &position) const
59: {
60: return Seek(0, FILE_CURRENT, position);
61: }
62:
63: bool CFileBase::GetLength(UINT64 &length) const
64: {
65: DWORD sizeHigh;
66: DWORD sizeLow = ::GetFileSize(_handle, &sizeHigh);
67: if(sizeLow == 0xFFFFFFFF)
68: if(::GetLastError() != NO_ERROR )
69: return false;
70: length = (((UINT64)sizeHigh) << 32) + sizeLow;
71: return true;
72: }
73:
74: bool CFileBase::Seek(INT64 distanceToMove, DWORD moveMethod, UINT64 &newPosition) const
75: {
76: LARGE_INTEGER *pointer = (LARGE_INTEGER *)&distanceToMove;
77: pointer->LowPart = ::SetFilePointer(_handle, pointer->LowPart,
78: &pointer->HighPart, moveMethod);
79: if (pointer->LowPart == 0xFFFFFFFF)
80: if(::GetLastError() != NO_ERROR)
81: return false;
82: newPosition = *((UINT64 *)pointer);
83: return true;
84: }
85:
86: bool CFileBase::Seek(UINT64 position, UINT64 &newPosition)
87: {
88: return Seek(position, FILE_BEGIN, newPosition);
89: }
90:
91: bool CFileBase::SeekToBegin()
92: {
93: UINT64 newPosition;
94: return Seek(0, newPosition);
95: }
96:
97: bool CFileBase::SeekToEnd(UINT64 &newPosition)
98: {
99: return Seek(0, FILE_END, newPosition);
100: }
101:
102: bool CFileBase::GetFileInformation(CByHandleFileInfo &fileInfo) const
103: {
104: BY_HANDLE_FILE_INFORMATION winFileInfo;
105: if(!::GetFileInformationByHandle(_handle, &winFileInfo))
106: return false;
107: fileInfo.Attributes = winFileInfo.dwFileAttributes;
108: fileInfo.CreationTime = winFileInfo.ftCreationTime;
109: fileInfo.LastAccessTime = winFileInfo.ftLastAccessTime;
110: fileInfo.LastWriteTime = winFileInfo.ftLastWriteTime;
111: fileInfo.VolumeSerialNumber = winFileInfo.dwFileAttributes;
112: fileInfo.Size = (((UINT64)winFileInfo.nFileSizeHigh) << 32) +
113: winFileInfo.nFileSizeLow;
114: fileInfo.NumberOfLinks = winFileInfo.nNumberOfLinks;
115: fileInfo.FileIndex = (((UINT64)winFileInfo.nFileIndexHigh) << 32) +
116: winFileInfo.nFileIndexLow;
117: return true;
118: }
119:
120: /////////////////////////
121: // CInFile
122:
123: bool CInFile::Open(LPCTSTR fileName, DWORD shareMode,
124: DWORD creationDisposition, DWORD flagsAndAttributes)
125: {
126: return Create(fileName, GENERIC_READ, shareMode,
127: creationDisposition, flagsAndAttributes);
128: }
129:
130: bool CInFile::Open(LPCTSTR fileName)
131: {
132: return Open(fileName, FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
133: }
134:
135: #ifndef _UNICODE
136: bool CInFile::Open(LPCWSTR fileName, DWORD shareMode,
137: DWORD creationDisposition, DWORD flagsAndAttributes)
138: {
139: return Create(fileName, GENERIC_READ, shareMode,
140: creationDisposition, flagsAndAttributes);
141: }
142:
143: bool CInFile::Open(LPCWSTR fileName)
144: {
145: return Open(fileName, FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
146: }
147: #endif
148:
149: bool CInFile::Read(void *data, UINT32 size, UINT32 &processedSize)
150: {
151: return BOOLToBool(::ReadFile(_handle, data, size,
152: (DWORD *)&processedSize, NULL));
153: }
154:
155: /////////////////////////
156: // COutFile
157:
158: bool COutFile::Open(LPCTSTR fileName, DWORD shareMode,
159: DWORD creationDisposition, DWORD flagsAndAttributes)
160: {
161: return CFileBase::Create(fileName, GENERIC_WRITE, shareMode,
162: creationDisposition, flagsAndAttributes);
163: }
164:
165: static inline DWORD GetCreationDisposition(bool createAlways)
166: { return createAlways? CREATE_ALWAYS: CREATE_NEW; }
167:
168: bool COutFile::Open(LPCTSTR fileName, DWORD creationDisposition)
169: {
170: return Open(fileName, FILE_SHARE_READ,
171: creationDisposition, FILE_ATTRIBUTE_NORMAL);
172: }
173:
174: bool COutFile::Create(LPCTSTR fileName, bool createAlways)
175: {
176: return Open(fileName, GetCreationDisposition(createAlways));
177: }
178:
179: #ifndef _UNICODE
180:
181: bool COutFile::Open(LPCWSTR fileName, DWORD shareMode,
182: DWORD creationDisposition, DWORD flagsAndAttributes)
183: {
184: return CFileBase::Create(fileName, GENERIC_WRITE, shareMode,
185: creationDisposition, flagsAndAttributes);
186: }
187:
188: bool COutFile::Open(LPCWSTR fileName, DWORD creationDisposition)
189: {
190: return Open(fileName, FILE_SHARE_READ,
191: creationDisposition, FILE_ATTRIBUTE_NORMAL);
192: }
193:
194: bool COutFile::Create(LPCWSTR fileName, bool createAlways)
195: {
196: return Open(fileName, GetCreationDisposition(createAlways));
197: }
198:
199: #endif
200:
201: bool COutFile::SetTime(const FILETIME *creationTime,
202: const FILETIME *lastAccessTime, const FILETIME *lastWriteTime)
203: {
204: return BOOLToBool(::SetFileTime(_handle, creationTime,
205: lastAccessTime, lastWriteTime));
206: }
207:
208: bool COutFile::SetLastWriteTime(const FILETIME *lastWriteTime)
209: {
210: return SetTime(NULL, NULL, lastWriteTime);
211: }
212:
213: bool COutFile::Write(const void *data, UINT32 size, UINT32 &processedSize)
214: {
215: return BOOLToBool(::WriteFile(_handle, data, size,
216: (DWORD *)&processedSize, NULL));
217: }
218:
219: bool COutFile::SetEndOfFile()
220: {
221: return BOOLToBool(::SetEndOfFile(_handle));
222: }
223:
224: bool COutFile::SetLength(UINT64 length)
225: {
226: UINT64 newPosition;
227: if(!Seek(length, newPosition))
228: return false;
229: if(newPosition != length)
230: return false;
231: return SetEndOfFile();
232: }
233:
234: }}}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>