Debian Codegraph @

frmts/pcidsk/sdk/port/io_win32.cpp

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Purpose: Implementation of IO interface using Win32 API.
4 *
5 ******************************************************************************
6 * Copyright (c) 2009
7 * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada.
8 *
9 * SPDX-License-Identifier: MIT
10 ****************************************************************************/
11
12 #ifdef TRACK_DISKIO
13 // Keep these includes on top or else nccltype.h will conflict
14 // with type definitions from pcidsk_config.h.
15 #include "syslow/hdiskio.hh"
16 #include "syslow/htimer.hh"
17 #endif
18
19 #ifdef PCIMAJORVERSION
20 #include "syslow/winunicode.hh"
21 #endif
22
23 #include "pcidsk_io.h"
24 #include "pcidsk_exception.h"
25 #include <windows.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <io.h>
29 #include <fcntl.h>
30 #include <direct.h>
31
32 using namespace PCIDSK;
33
34 class Win32IOInterface final: public IOInterfaces
35 {
36 void *Open( const std::string& filename, const std::string& access ) const override;
37 uint64 Seek( void *io_handle, uint64 offset, int whence ) const override;
38 uint64 Tell( void *io_handle ) const override;
39 uint64 Read( void *buffer, uint64 size, uint64 nmemb, void *io_handle ) const override;
40 uint64 Write( const void *buffer, uint64 size, uint64 nmemb, void *io_handle ) const override;
41 int Eof( void *io_handle ) const override;
42 int Flush( void *io_handle ) const override;
43 int Close( void *io_handle ) const override;
44
45 // Dummy implementation for now
46 static const char *LastError();
47 };
48
49 typedef struct {
50 HANDLE hFile;
51 uint64 offset;
52 } FileInfo;
53
54 /************************************************************************/
55 /* GetDefaultIOInterfaces() */
56 /************************************************************************/
57
58 const IOInterfaces *PCIDSK::GetDefaultIOInterfaces()
59 {
60 static Win32IOInterface singleton_win32_interface;
61
62 return &singleton_win32_interface;
63 }
64
65 /************************************************************************/
66 /* Open() */
67 /************************************************************************/
68
69 void *
70 Win32IOInterface::Open( const std::string& filename, const std::string& access ) const
71
72 {
73 #ifdef TRACK_DISKIO
74 HTimer oTimer;
75 #endif
76
77 DWORD dwDesiredAccess, dwCreationDisposition, dwFlagsAndAttributes;
78 HANDLE hFile;
79
80 if( strchr(access.c_str(),'+') != NULL || strchr(access.c_str(),'w') != 0 )
81 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
82 else
83 dwDesiredAccess = GENERIC_READ;
84
85 if( strstr(access.c_str(), "w") != NULL )
86 dwCreationDisposition = CREATE_ALWAYS;
87 else
88 dwCreationDisposition = OPEN_EXISTING;
89
90 dwFlagsAndAttributes = (dwDesiredAccess == GENERIC_READ) ?
91 FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL;
92
93 #ifdef PCIMAJORVERSION
94 std::string oLongFilename = WINExtendedLengthPath(filename);
95
96 hFile = CreateFile((UTFTranscode) oLongFilename, dwDesiredAccess,
97 FILE_SHARE_READ | FILE_SHARE_WRITE,
98 NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
99 #else
100 hFile = CreateFileA(filename.c_str(), dwDesiredAccess,
101 FILE_SHARE_READ | FILE_SHARE_WRITE,
102 NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
103 #endif
104
105 if( hFile == INVALID_HANDLE_VALUE )
106 {
107 ThrowPCIDSKException( "Open(%s,%s) failed:\n%s",
108 filename.c_str(), access.c_str(), LastError() );
109 }
110
111 FileInfo *fi = new FileInfo();
112 fi->hFile = hFile;
113 fi->offset = 0;
114
115 #ifdef TRACK_DISKIO
116 HDiskIO::Open(filename, fi, oTimer.GetTimeInSeconds());
117 #endif
118
119 return fi;
120 }
121
122 /************************************************************************/
123 /* Seek() */
124 /************************************************************************/
125
126 uint64
127 Win32IOInterface::Seek( void *io_handle, uint64 offset, int whence ) const
128
129 {
130 #ifdef TRACK_DISKIO
131 HTimer oTimer;
132 #endif
133
134 FileInfo *fi = static_cast<FileInfo *>(io_handle);
135 uint32 dwMoveMethod, dwMoveHigh;
136 uint32 nMoveLow;
137 LARGE_INTEGER li;
138
139 // seeks that do nothing are still surprisingly expensive with MSVCRT.
140 // try and short circuit if possible.
141 if( whence == SEEK_SET && offset == fi->offset )
142 return 0;
143
144 switch(whence)
145 {
146 case SEEK_CUR:
147 dwMoveMethod = FILE_CURRENT;
148 break;
149 case SEEK_END:
150 dwMoveMethod = FILE_END;
151 break;
152 case SEEK_SET:
153 default:
154 dwMoveMethod = FILE_BEGIN;
155 break;
156 }
157
158 li.QuadPart = offset;
159 nMoveLow = li.LowPart;
160 dwMoveHigh = li.HighPart;
161
162 SetLastError( 0 );
163 SetFilePointer(fi->hFile, (LONG) nMoveLow, (PLONG)&dwMoveHigh,
164 dwMoveMethod);
165
166 if( GetLastError() != NO_ERROR )
167 {
168 ThrowPCIDSKException( "Seek(" PCIDSK_FRMT_UINT64 ",%d): %s (%d)",
169 offset, whence,
170 LastError(), GetLastError() );
171 return (uint64)-1;
172 }
173
174 /* -------------------------------------------------------------------- */
175 /* Update our offset. */
176 /* -------------------------------------------------------------------- */
177 if( whence == SEEK_SET )
178 fi->offset = offset;
179 else if( whence == SEEK_END )
180 {
181 LARGE_INTEGER li;
182
183 li.HighPart = 0;
184 li.LowPart = SetFilePointer( fi->hFile, 0, (PLONG) &(li.HighPart),
185 FILE_CURRENT );
186 fi->offset = li.QuadPart;
187 }
188 else if( whence == SEEK_CUR )
189 fi->offset += offset;
190
191 #ifdef TRACK_DISKIO
192 HDiskIO::Seek(io_handle, oTimer.GetTimeInSeconds());
193 #endif
194
195 return 0;
196 }
197
198 /************************************************************************/
199 /* Tell() */
200 /************************************************************************/
201
202 uint64 Win32IOInterface::Tell( void *io_handle ) const
203
204 {
205 FileInfo *fi = static_cast<FileInfo *>(io_handle);
206
207 return fi->offset;
208 }
209
210 /************************************************************************/
211 /* Read() */
212 /************************************************************************/
213
214 uint64 Win32IOInterface::Read( void *buffer, uint64 size, uint64 nmemb,
215 void *io_handle ) const
216
217 {
218 #ifdef TRACK_DISKIO
219 HTimer oTimer;
220 #endif
221
222 FileInfo *fi = static_cast<FileInfo *>(io_handle);
223
224 errno = 0;
225
226 DWORD dwSizeRead;
227 size_t result;
228
229 if( !ReadFile(fi->hFile, buffer, (DWORD)(size*nmemb), &dwSizeRead, NULL) )
230 {
231 result = 0;
232 }
233 else if( size == 0 )
234 result = 0;
235 else
236 result = (size_t) (dwSizeRead / size);
237
238 if( errno != 0 && result == 0 && nmemb != 0 )
239 ThrowPCIDSKException( "Read(" PCIDSK_FRMT_UINT64 "): %s",
240 size * nmemb,
241 LastError() );
242
243 fi->offset += size*result;
244
245 #ifdef TRACK_DISKIO
246 HDiskIO::Read(io_handle, (uint64) size * nmemb, oTimer.GetTimeInSeconds());
247 #endif
248
249 return result;
250 }
251
252 /************************************************************************/
253 /* Write() */
254 /************************************************************************/
255
256 uint64 Win32IOInterface::Write( const void *buffer, uint64 size, uint64 nmemb,
257 void *io_handle ) const
258
259 {
260 #ifdef TRACK_DISKIO
261 HTimer oTimer;
262 #endif
263
264 FileInfo *fi = static_cast<FileInfo *>(io_handle);
265
266 errno = 0;
267
268 DWORD dwSizeRead;
269 size_t result;
270
271 if( !WriteFile(fi->hFile, buffer, (DWORD)(size*nmemb), &dwSizeRead, NULL) )
272 {
273 result = 0;
274 }
275 else if( size == 0 )
276 result = 0;
277 else
278 result = (size_t) (dwSizeRead / size);
279
280 if( errno != 0 && result == 0 && nmemb != 0 )
281 ThrowPCIDSKException( "Write(" PCIDSK_FRMT_UINT64 "): %s",
282 size * nmemb,
283 LastError() );
284
285 fi->offset += size*result;
286
287 #ifdef TRACK_DISKIO
288 HDiskIO::Write(io_handle, (uint64) size * nmemb, oTimer.GetTimeInSeconds());
289 #endif
290
291 return result;
292 }
293
294 /************************************************************************/
295 /* Eof() */
296 /************************************************************************/
297
298 int Win32IOInterface::Eof( void *io_handle ) const
299
300 {
301 uint64 nCur, nEnd;
302
303 nCur = Tell( io_handle );
304 Seek( io_handle, 0, SEEK_END );
305 nEnd = Tell( io_handle );
306 Seek( io_handle, nCur, SEEK_SET );
307
308 return (nCur == nEnd);
309 }
310
311 /************************************************************************/
312 /* Flush() */
313 /************************************************************************/
314
315 int Win32IOInterface::Flush( void *io_handle ) const
316
317 {
318 FileInfo *fi = static_cast<FileInfo *>(io_handle);
319
320 FlushFileBuffers( fi->hFile );
321
322 return 0;
323 }
324
325 /************************************************************************/
326 /* Close() */
327 /************************************************************************/
328
329 int Win32IOInterface::Close( void *io_handle ) const
330
331 {
332 #ifdef TRACK_DISKIO
333 HTimer oTimer;
334 #endif
335
336 FileInfo *fi = static_cast<FileInfo *>(io_handle);
337
338 int result = CloseHandle( fi->hFile ) ? 0 : -1;
339 delete fi;
340
341 #ifdef TRACK_DISKIO
342 HDiskIO::Close(io_handle, oTimer.GetTimeInSeconds());
343 #endif
344
345 return result;
346 }
347
348 /************************************************************************/
349 /* LastError() */
350 /* */
351 /* Return a string representation of the last error. */
352 /************************************************************************/
353
354 /* static */
355 const char *Win32IOInterface::LastError()
356
357 {
358 return "";
359 }