1 #include "All.h"
2
3 #ifdef IO_USE_WIN_FILE_IO
4
5 #include "WinFileIO.h"
6 #include <windows.h>
7 #include "CharacterHelper.h"
8
9 namespace APE
10 {
11
CWinFileIO()12 CWinFileIO::CWinFileIO()
13 {
14 m_hFile = INVALID_HANDLE_VALUE;
15 memset(m_cFileName, 0, MAX_PATH);
16 m_bReadOnly = false;
17 }
18
~CWinFileIO()19 CWinFileIO::~CWinFileIO()
20 {
21 Close();
22 }
23
Open(const wchar_t * pName,bool bOpenReadOnly)24 int CWinFileIO::Open(const wchar_t * pName, bool bOpenReadOnly)
25 {
26 Close();
27
28 if (wcslen(pName) >= MAX_PATH)
29 return -1;
30
31 #ifdef _UNICODE
32 CSmartPtr<wchar_t> spName((wchar_t *) pName, true, false);
33 #else
34 CSmartPtr<char> spName(GetANSIFromUTF16(pName), true);
35 #endif
36
37 // open (read / write)
38 if (!bOpenReadOnly)
39 m_hFile = ::CreateFile(spName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
40 if (m_hFile == INVALID_HANDLE_VALUE)
41 {
42 // open (read-only)
43 m_hFile = ::CreateFile(spName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
44 if (m_hFile == INVALID_HANDLE_VALUE)
45 {
46 return -1;
47 }
48 else
49 {
50 m_bReadOnly = true;
51 }
52 }
53 else
54 {
55 m_bReadOnly = false;
56 }
57
58 wcscpy_s(m_cFileName, pName);
59
60 return 0;
61 }
62
Close()63 int CWinFileIO::Close()
64 {
65 SAFE_FILE_CLOSE(m_hFile);
66
67 return 0;
68 }
69
Read(void * pBuffer,unsigned int nBytesToRead,unsigned int * pBytesRead)70 int CWinFileIO::Read(void * pBuffer, unsigned int nBytesToRead, unsigned int * pBytesRead)
71 {
72 unsigned int nTotalBytesRead = 0;
73 int nBytesLeft = nBytesToRead;
74 bool bRetVal = true;
75 unsigned char * pucBuffer = (unsigned char *) pBuffer;
76
77 *pBytesRead = 1;
78 while ((nBytesLeft > 0) && (*pBytesRead > 0) && bRetVal)
79 {
80 bRetVal = ::ReadFile(m_hFile, &pucBuffer[nBytesToRead - nBytesLeft], nBytesLeft, (unsigned long *) pBytesRead, NULL);
81 if (bRetVal && (*pBytesRead <= 0))
82 bRetVal = false;
83 if (bRetVal)
84 {
85 nBytesLeft -= *pBytesRead;
86 nTotalBytesRead += *pBytesRead;
87 }
88 }
89
90 *pBytesRead = nTotalBytesRead;
91
92 return bRetVal ? 0 : ERROR_IO_READ;
93 }
94
Write(const void * pBuffer,unsigned int nBytesToWrite,unsigned int * pBytesWritten)95 int CWinFileIO::Write(const void * pBuffer, unsigned int nBytesToWrite, unsigned int * pBytesWritten)
96 {
97 bool bRetVal = WriteFile(m_hFile, pBuffer, nBytesToWrite, (unsigned long *) pBytesWritten, NULL);
98
99 if ((bRetVal == 0) || (*pBytesWritten != nBytesToWrite))
100 return ERROR_IO_WRITE;
101 else
102 return 0;
103 }
104
Seek(intn nDistance,unsigned int nMoveMode)105 int CWinFileIO::Seek(intn nDistance, unsigned int nMoveMode)
106 {
107 SetFilePointer(m_hFile, (LONG) nDistance, NULL, nMoveMode);
108 return 0;
109 }
110
SetEOF()111 int CWinFileIO::SetEOF()
112 {
113 return SetEndOfFile(m_hFile) ? 0 : -1;
114 }
115
GetPosition()116 int CWinFileIO::GetPosition()
117 {
118 return SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT);
119 }
120
GetSize()121 unsigned int CWinFileIO::GetSize()
122 {
123 return GetFileSize(m_hFile, NULL);
124 }
125
GetName(wchar_t * pBuffer)126 int CWinFileIO::GetName(wchar_t * pBuffer)
127 {
128 wcscpy_s(pBuffer, MAX_PATH, m_cFileName);
129 return 0;
130 }
131
Create(const wchar_t * pName)132 int CWinFileIO::Create(const wchar_t * pName)
133 {
134 Close();
135
136 if (wcslen(pName) >= MAX_PATH)
137 return -1;
138
139 #ifdef _UNICODE
140 CSmartPtr<wchar_t> spName((wchar_t *) pName, true, false);
141 #else
142 CSmartPtr<char> spName(GetANSIFromUTF16(pName), true);
143 #endif
144
145 m_hFile = CreateFile(spName, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
146 if (m_hFile == INVALID_HANDLE_VALUE) { return -1; }
147
148 m_bReadOnly = false;
149
150 wcscpy_s(m_cFileName, pName);
151
152 return 0;
153 }
154
Delete()155 int CWinFileIO::Delete()
156 {
157 Close();
158
159 #ifdef _UNICODE
160 CSmartPtr<wchar_t> spName(m_cFileName, true, false);
161 #else
162 CSmartPtr<char> spName(GetANSIFromUTF16(m_cFileName), true);
163 #endif
164
165 return DeleteFile(spName) ? 0 : -1;
166 }
167
168 }
169
170 #endif // #ifdef IO_USE_WIN_FILE_IO
171