-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathvtkDICOMFile.cxx
560 lines (536 loc) · 12.8 KB
/
vtkDICOMFile.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*=========================================================================
Program: DICOM for VTK
Copyright (c) 2012-2024 David Gobbi
All rights reserved.
See Copyright.txt or http://dgobbi.github.io/bsd3.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkDICOMFile.h"
#include "vtkDICOMFilePath.h"
#if defined(VTK_DICOM_POSIX_IO)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#elif defined(VTK_DICOM_WIN32_IO)
#include <windows.h>
#else
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#define _unlink unlink
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <errno.h>
#endif
#include <string.h>
//----------------------------------------------------------------------------
vtkDICOMFile::vtkDICOMFile(const char *filename, Mode mode)
{
#if defined(VTK_DICOM_POSIX_IO)
this->Handle = -1;
this->Error = 0;
this->Eof = false;
if (mode == In)
{
this->Handle = open(filename, O_RDONLY);
}
else if (mode == Out)
{
this->Handle = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 000666);
}
if (this->Handle == -1)
{
int errorCode = errno;
if (errorCode == EACCES)
{
this->Error = AccessDenied;
}
else if (errorCode == EISDIR)
{
this->Error = FileIsDirectory;
}
else if (errorCode == ENOTDIR)
{
this->Error = (mode == Out ? ImpossiblePath : FileNotFound);
}
else if (errorCode == ENOENT)
{
this->Error = FileNotFound;
}
else if (errorCode == ENOSPC)
{
// some systems also have EDQUOT for "quota exceeded"
this->Error = OutOfSpace;
}
else
{
this->Error = UnknownError;
}
}
#elif defined(VTK_DICOM_WIN32_IO)
this->Handle = INVALID_HANDLE_VALUE;
this->Error = 0;
this->Eof = false;
vtkDICOMFilePath fpath(filename);
const wchar_t *wideFilename = fpath.Wide();
if (wideFilename)
{
if (mode == In)
{
this->Handle = CreateFileW(wideFilename,
GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
}
else if (mode == Out)
{
this->Handle = CreateFileW(wideFilename,
GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
}
if (this->Handle == INVALID_HANDLE_VALUE)
{
DWORD errorCode = GetLastError();
if (errorCode == ERROR_ACCESS_DENIED ||
errorCode == ERROR_SHARING_VIOLATION)
{
this->Error = AccessDenied;
if (wideFilename)
{
DWORD attr = GetFileAttributesW(wideFilename);
if (attr != INVALID_FILE_ATTRIBUTES &&
(attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
this->Error = FileIsDirectory;
}
}
}
else if (errorCode == ERROR_PATH_NOT_FOUND)
{
this->Error = (mode == Out ? ImpossiblePath : FileNotFound);
}
else if (errorCode == ERROR_FILE_NOT_FOUND)
{
this->Error = FileNotFound;
}
else if (errorCode == ERROR_DISK_FULL)
{
this->Error = OutOfSpace;
}
else
{
this->Error = UnknownError;
}
}
#else
this->Handle = nullptr;
this->Error = 0;
this->Eof = false;
if (mode == In)
{
this->Handle = fopen(filename, "rb");
}
else if (mode == Out)
{
this->Handle = fopen(filename, "wb");
}
if (this->Handle == nullptr)
{
this->Error = UnknownError;
}
#endif
}
//----------------------------------------------------------------------------
vtkDICOMFile::~vtkDICOMFile()
{
this->Close();
}
//----------------------------------------------------------------------------
void vtkDICOMFile::Close()
{
#if defined(VTK_DICOM_POSIX_IO)
if (this->Handle)
{
if (close(this->Handle) == 0)
{
this->Error = 0;
}
else if (errno != EINTR)
{
this->Error = UnknownError;
}
this->Handle = 0;
}
#elif defined(VTK_DICOM_WIN32_IO)
CloseHandle(this->Handle);
this->Handle = INVALID_HANDLE_VALUE;
#else
if (this->Handle)
{
fclose(static_cast<FILE *>(this->Handle));
}
this->Handle = nullptr;
#endif
}
//----------------------------------------------------------------------------
size_t vtkDICOMFile::Read(unsigned char *data, size_t len)
{
#if defined(VTK_DICOM_POSIX_IO)
ssize_t n;
while ((n = read(this->Handle, data, len)) == -1)
{
if (errno != EINTR)
{
break;
}
errno = 0;
}
if (n == 0)
{
this->Eof = true;
}
else if (n == -1)
{
this->Error = UnknownError;
n = 0;
}
return n;
#elif defined(VTK_DICOM_WIN32_IO)
// handle large requests in 1GB chunks
const size_t chunksize = 1024*1024*1024;
size_t n = 0;
while (n < len)
{
DWORD l = static_cast<DWORD>(len - n < chunksize ? len - n : chunksize);
DWORD r = 0;
if (ReadFile(this->Handle, &data[n], l, &r, nullptr) == FALSE)
{
this->Error = UnknownError;
break;
}
n += r;
if (n == 0)
{
this->Eof = true;
break;
}
else if (r < l)
{
break;
}
}
return n;
#else
size_t n = fread(data, 1, len, static_cast<FILE *>(this->Handle));
if (n != len || len == 0)
{
this->Eof = (feof(static_cast<FILE *>(this->Handle)) != 0);
this->Error = (ferror(static_cast<FILE *>(this->Handle)) == 0 ?
0 : UnknownError);
}
return n;
#endif
}
//----------------------------------------------------------------------------
size_t vtkDICOMFile::Write(const unsigned char *data, size_t len)
{
#if defined(VTK_DICOM_POSIX_IO)
ssize_t n;
while ((n = write(this->Handle, data, len)) == -1)
{
if (errno != EINTR)
{
break;
}
errno = 0;
}
if (n == -1)
{
this->Error = UnknownError;
n = 0;
}
return n;
#elif defined(VTK_DICOM_WIN32_IO)
// handle large requests in 1GB chunks
const size_t chunksize = 1024*1024*1024;
size_t n = 0;
while (n < len)
{
DWORD l = static_cast<DWORD>(len - n < chunksize ? len - n : chunksize);
DWORD r = 0;
if (WriteFile(this->Handle, &data[n], l, &r, nullptr) == FALSE)
{
DWORD errorCode = GetLastError();
if (errorCode == ERROR_HANDLE_DISK_FULL)
{
this->Error = OutOfSpace;
}
else
{
this->Error = UnknownError;
}
break;
}
n += r;
}
return n;
#else
size_t n = fwrite(data, 1, len, static_cast<FILE *>(this->Handle));
if (n != len || len == 0)
{
this->Error = (ferror(static_cast<FILE *>(this->Handle)) == 0 ?
0 : UnknownError);
}
return n;
#endif
}
//----------------------------------------------------------------------------
bool vtkDICOMFile::SetPosition(Size offset)
{
#if defined(VTK_DICOM_POSIX_IO)
#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
off64_t pos = lseek64(this->Handle, offset, SEEK_SET);
#else
off_t pos = lseek(this->Handle, offset, SEEK_SET);
#endif
if (pos == -1)
{
this->Error = UnknownError;
return false;
}
return true;
#elif defined(VTK_DICOM_WIN32_IO)
LONG lowerBits = static_cast<LONG>(offset);
LONG upperBits = static_cast<LONG>(offset >> 32);
DWORD pos = SetFilePointer(this->Handle, lowerBits, &upperBits, FILE_BEGIN);
if (pos == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
{
this->Error = UnknownError;
return false;
}
return true;
#else
return (fseek(static_cast<FILE *>(this->Handle), offset, SEEK_SET) == 0);
#endif
}
//----------------------------------------------------------------------------
vtkDICOMFile::Size vtkDICOMFile::GetSize()
{
#if defined(VTK_DICOM_POSIX_IO)
struct stat fs;
if (fstat(this->Handle, &fs) != 0)
{
this->Error = UnknownError;
return ~0ull;
}
return fs.st_size;
#elif defined(VTK_DICOM_WIN32_IO)
DWORD upperBits = 0;
DWORD lowerBits = GetFileSize(this->Handle, &upperBits);
if (lowerBits == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
{
this->Error = UnknownError;
return ~0ull;
}
return lowerBits | (static_cast<Size>(upperBits) << 32);
#else
FILE *fp = static_cast<FILE *>(this->Handle);
long long size = -1;
fpos_t pos;
if (fgetpos(fp, &pos) == 0)
{
if (fseek(fp, 0, SEEK_END) == 0)
{
size = ftell(fp);
}
if (fsetpos(fp, &pos) != 0)
{
this->Error = UnknownError;
}
}
if (size == -1)
{
this->Error = UnknownError;
return ~0ull;
}
return static_cast<Size>(size);
#endif
}
//----------------------------------------------------------------------------
int vtkDICOMFile::Access(const char *filename, Mode mode)
{
#ifdef _WIN32
int errorCode = UnknownError;
vtkDICOMFilePath fpath(filename);
const wchar_t *wideFilename = fpath.Wide();
if (wideFilename)
{
errorCode = 0;
DWORD code = GetFileAttributesW(wideFilename);
if (code == INVALID_FILE_ATTRIBUTES)
{
DWORD lastError = GetLastError();
if (lastError == ERROR_ACCESS_DENIED ||
lastError == ERROR_SHARING_VIOLATION)
{
errorCode = AccessDenied;
}
else if (lastError == ERROR_FILE_NOT_FOUND)
{
errorCode = FileNotFound;
}
else if (lastError == ERROR_PATH_NOT_FOUND)
{
errorCode = (mode == Out ? ImpossiblePath : FileNotFound);
}
else
{
errorCode = UnknownError;
}
}
else if (mode == Out && (code & FILE_ATTRIBUTE_READONLY) != 0)
{
errorCode = AccessDenied;
}
else if ((code & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
errorCode = FileIsDirectory;
}
}
return errorCode;
#else
int errorCode = 0;
struct stat fs;
if (stat(filename, &fs) != 0 ||
access(filename, (mode == In ? R_OK : W_OK)) != 0)
{
int e = errno;
if (e == EACCES || e == EPERM)
{
errorCode = AccessDenied;
}
else if (e == ENOENT)
{
errorCode = FileNotFound;
}
else if (e == ENOTDIR)
{
errorCode = (mode == Out ? ImpossiblePath : FileNotFound);
}
else
{
errorCode = UnknownError;
}
}
else if (S_ISDIR(fs.st_mode))
{
errorCode = FileIsDirectory;
}
return errorCode;
#endif
}
//----------------------------------------------------------------------------
int vtkDICOMFile::Remove(const char *filename)
{
#if defined(VTK_DICOM_WIN32_IO)
int errorCode = 0;
vtkDICOMFilePath fpath(filename);
const wchar_t *wideFilename = fpath.Wide();
if (wideFilename)
{
if (!DeleteFileW(wideFilename))
{
DWORD lastError = GetLastError();
if (lastError == ERROR_ACCESS_DENIED ||
lastError == ERROR_SHARING_VIOLATION)
{
errorCode = AccessDenied;
}
else if (lastError == ERROR_FILE_NOT_FOUND ||
lastError == ERROR_PATH_NOT_FOUND)
{
errorCode = FileNotFound;
}
else
{
errorCode = UnknownError;
}
}
}
return errorCode;
#else
int errorCode = 0;
if (unlink(filename) != 0)
{
int e = errno;
if (e == EACCES || e == EPERM)
{
errorCode = AccessDenied;
}
else if (e == ENOENT || e == ENOTDIR)
{
errorCode = FileNotFound;
}
else
{
errorCode = UnknownError;
}
}
return errorCode;
#endif
}
//----------------------------------------------------------------------------
bool vtkDICOMFile::SameFile(const char *file1, const char *file2)
{
// Two files are considered to be the same if:
// 1) they are on the same device
// 2) their index (inode number) is the same
bool result = false;
#ifdef _WIN32
vtkDICOMFilePath fpath1(file1);
const wchar_t *widepath = fpath1.Wide();
HANDLE h1 = CreateFileW(widepath,
GENERIC_READ, FILE_SHARE_READ , nullptr, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, nullptr);
vtkDICOMFilePath fpath2(file2);
widepath = fpath2.Wide();
HANDLE h2 = CreateFileW(widepath,
GENERIC_READ, FILE_SHARE_READ , nullptr, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (h1 != INVALID_HANDLE_VALUE && h2 != INVALID_HANDLE_VALUE)
{
BY_HANDLE_FILE_INFORMATION buf;
GetFileInformationByHandle(h1, &buf);
DWORD sn = buf.dwVolumeSerialNumber;
DWORD hi = buf.nFileIndexHigh;
DWORD li = buf.nFileIndexLow;
GetFileInformationByHandle(h2, &buf);
result = (buf.dwVolumeSerialNumber == sn);
result &= (buf.nFileIndexHigh == hi);
result &= (buf.nFileIndexLow == li);
}
if (h1 != INVALID_HANDLE_VALUE)
{
CloseHandle(h1);
}
if (h2 != INVALID_HANDLE_VALUE)
{
CloseHandle(h2);
}
#else
struct stat st1;
struct stat st2;
if (stat(file1, &st1) == 0 && stat(file2, &st2) == 0)
{
result = (memcmp(&st1.st_dev, &st2.st_dev, sizeof(st1.st_dev)) == 0);
result &= (memcmp(&st1.st_ino, &st2.st_ino, sizeof(st1.st_ino)) == 0);
}
#endif
return result;
}