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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
|
/*=========================================================================
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 "vtkDICOMFilePath.h"
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
//----------------------------------------------------------------------------
vtkDICOMFilePath::vtkDICOMFilePath(const std::string& path)
: Path(path), Separator('/')
{
#ifdef _WIN32
size_t l = path.length();
for (size_t i = 0; i < l; i++)
{
if (IsSeparator(path[i]))
{
this->Separator = path[i];
break;
}
}
this->WidePath = nullptr;
this->LocalPath = nullptr;
#endif
StripTrailingSlash(&this->Path);
}
//----------------------------------------------------------------------------
#ifdef _WIN32
vtkDICOMFilePath::vtkDICOMFilePath(const std::wstring& path)
{
char *filename = ConvertToUTF8(path.c_str());
this->Path = filename;
this->Separator = '/';
delete [] filename;
size_t l = path.length();
for (size_t i = 0; i < l; i++)
{
if (IsSeparator(path[i]))
{
this->Separator = path[i];
break;
}
}
this->WidePath = nullptr;
this->LocalPath = nullptr;
StripTrailingSlash(&this->Path);
}
#endif
//----------------------------------------------------------------------------
vtkDICOMFilePath::vtkDICOMFilePath(const vtkDICOMFilePath& other) :
Path(other.Path), Separator(other.Separator)
{
#ifdef _WIN32
this->WidePath = nullptr;
this->LocalPath = nullptr;
#endif
}
//----------------------------------------------------------------------------
vtkDICOMFilePath::~vtkDICOMFilePath()
{
#ifdef _WIN32
delete [] this->WidePath;
delete [] this->LocalPath;
#endif
}
//----------------------------------------------------------------------------
vtkDICOMFilePath& vtkDICOMFilePath::operator=(const vtkDICOMFilePath& other)
{
if (this != &other)
{
this->Path = other.Path;
this->Separator = other.Separator;
}
return *this;
}
//----------------------------------------------------------------------------
std::string vtkDICOMFilePath::Join(const std::string& second) const
{
#ifdef _WIN32
std::string path = this->Path;
char sep = this->Separator;
// if extended prefix is present, then all forward slashes must be
// converted into back slashes, and "." and ".." must be removed
bool extended = false;
if (HasExtendedPrefix(path))
{
sep = '\\';
extended = true;
}
// check whether the second part might be an absolute path
size_t pos = 0;
size_t r = RootLength(second);
if (r > 0)
{
// if second drive letter mismatches the first, paths can't be joined
char drive1 = DriveLetter(path);
char drive2 = DriveLetter(second);
if (HasExtendedPrefix(second))
{
// if second part uses extended prefix, ignore the first part
path.resize(0);
}
else if (drive2 && drive1 == drive2 && r != 3)
{
// skip the drive letter in the second path
pos = 2;
}
else if (extended && drive2 && r >= 2)
{
// move the drive letter from the second path to the first
pos = 2;
path = "\\\\\?\\";
path.append(second, 0, pos);
pos += (r >= 3 && IsSeparator(second[pos]));
}
else if (drive1 && drive2 == 0 && r == 1)
{
// keep the drive letter, but use absolute path from second part
sep = second[0];
path.resize(4*extended + 2);
}
else if (extended && r >= 2 &&
IsSeparator(second[0]) && IsSeparator(second[1]))
{
// create an extended UNC path
pos = 2;
path = "\\\\\?\\UNC\\";
}
else
{
// ignore the first path completely
path.resize(0);
}
}
// append a separator before appending the second part
size_t l = path.length();
if (l > 0 && !IsSeparator(path[l-1]) && !(l == 2 && path[1] == ':'))
{
path.push_back(sep);
}
// append one part at a time
while (pos != second.length())
{
if (IsSeparator(second[pos]))
{
path.push_back(sep);
pos++;
}
size_t endpos = pos;
while (endpos != second.length() && !IsSeparator(second[endpos]))
{
endpos++;
}
size_t n = endpos - pos;
if (n > 0)
{
if (extended)
{
if (n == 1 && second[pos] == '.')
{
// Ignore the '.' and the following slash
if (endpos != second.length() && IsSeparator(second[endpos]))
{
endpos++;
}
}
else if (n == 2 && second[pos] == '.' && second[pos+1] == '.')
{
// Ignore the '..' and the following slash
if (endpos != second.length() && IsSeparator(second[endpos]))
{
endpos++;
}
// Remove the preceding part of the path
l = path.length();
size_t m = RootLength(path);
if (l > m && path[l-1] == sep)
{
l--;
while (l > m && path[l-1] != sep)
{
l--;
}
path.resize(l);
}
}
else if (n > 2 && second[pos + 1] == ':')
{
path.append(second, pos, 2);
path.push_back(sep);
path.append(second, pos+2, n-2);
}
else
{
path.append(second, pos, n);
}
}
else
{
path.append(second, pos, n);
}
}
pos = endpos;
}
#else
if (this->Path.length() == 0)
{
// first part is empty, so just return second
return second;
}
if (second.length() > 0 && IsSeparator(second[0]))
{
// second part is an absolute path
return second;
}
// append the second part to the first
std::string path = this->Path;
size_t l = path.length();
if (l > 0 && !IsSeparator(path[l-1]))
{
path.push_back(this->Separator);
}
path.append(second);
#endif
StripTrailingSlash(&path);
return path;
}
//----------------------------------------------------------------------------
std::string vtkDICOMFilePath::GetBack() const
{
size_t l = this->Path.length();
size_t r = RootLength(this->Path);
#ifdef _WIN32
if (r >= 4 && HasExtendedPrefix(this->Path))
{
while (l > r && this->Path[l-1] != '\\')
{
--l;
}
}
else
#endif
{
while (l > r && !IsSeparator(this->Path[l-1]))
{
--l;
}
}
return this->Path.substr(l);
}
//----------------------------------------------------------------------------
std::string vtkDICOMFilePath::GetExtension() const
{
size_t i = ExtensionPosition(this->Path);
return this->Path.substr(i);
}
//----------------------------------------------------------------------------
void vtkDICOMFilePath::PopExtension()
{
size_t i = ExtensionPosition(this->Path);
this->Path.resize(i);
}
//----------------------------------------------------------------------------
void vtkDICOMFilePath::PushExtension(const std::string& ext)
{
size_t l = ext.length();
if (l == 0 || (l == 1 && ext[0] == '.'))
{
return;
}
for (size_t i = 0; i < l; i++)
{
if (IsSeparator(ext[i]))
{
return;
}
}
if (l > 0 && ext[0] != '.')
{
this->Path.push_back('.');
}
this->Path.append(ext);
}
//----------------------------------------------------------------------------
size_t vtkDICOMFilePath::ExtensionPosition(const std::string& path)
{
size_t l = path.length();
size_t r = RootLength(path);
size_t i = l;
#ifdef _WIN32
if (r >= 4 && HasExtendedPrefix(path))
{
while (l > r && path[l-1] != '\\')
{
if (path[--l] == '.')
{
i = l;
break;
}
}
}
else
#endif
{
while (l > r && !IsSeparator(path[l-1]))
{
if (path[--l] == '.')
{
i = l;
break;
}
}
}
return i;
}
//----------------------------------------------------------------------------
bool vtkDICOMFilePath::IsRoot() const
{
size_t l = this->Path.length();
return (l > 0 && l == RootLength(this->Path));
}
//----------------------------------------------------------------------------
bool vtkDICOMFilePath::IsDirectory() const
{
bool result = false;
#ifdef _WIN32
wchar_t *widepath = ConvertToWideChar(this->Path.c_str());
if (widepath)
{
DWORD a = GetFileAttributesW(widepath);
delete [] widepath;
result = ((a & FILE_ATTRIBUTE_DIRECTORY) != 0);
}
#else
struct stat fs;
if (stat(this->Path.c_str(), &fs) == 0 && S_ISDIR(fs.st_mode))
{
result = true;
}
#endif
return result;
}
//----------------------------------------------------------------------------
bool vtkDICOMFilePath::IsSpecial() const
{
bool result = false;
#ifdef _WIN32
wchar_t *widepath = ConvertToWideChar(this->Path.c_str());
if (widepath)
{
DWORD a = GetFileAttributesW(widepath);
delete [] widepath;
result = ((a & FILE_ATTRIBUTE_DEVICE) != 0);
}
#else
struct stat fs;
if (stat(this->Path.c_str(), &fs) == 0 &&
!S_ISDIR(fs.st_mode) && !S_ISREG(fs.st_mode))
{
result = true;
}
#endif
return result;
}
//----------------------------------------------------------------------------
bool vtkDICOMFilePath::IsSymlink() const
{
bool result = false;
#ifdef _WIN32
wchar_t *widepath = ConvertToWideChar(this->Path.c_str());
if (widepath && GetFileAttributesW(widepath) != INVALID_FILE_ATTRIBUTES)
{
WIN32_FIND_DATAW buf;
HANDLE h = FindFirstFileW(widepath, &buf);
if (h != INVALID_HANDLE_VALUE)
{
CloseHandle(h);
if ((buf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0 &&
buf.dwReserved0 == 0xA000000C)
{
result = true;
}
}
}
delete [] widepath;
#else
struct stat fs;
if (lstat(this->Path.c_str(), &fs) == 0 && S_ISLNK(fs.st_mode))
{
result = true;
}
#endif
return result;
}
//----------------------------------------------------------------------------
std::string vtkDICOMFilePath::GetRealPath() const
{
#ifdef _WIN32
std::string result;
wchar_t *widepath = ConvertToWideChar(this->Path.c_str());
#ifndef DICOM_DEPRECATE_WINXP
wchar_t buffer[512];
DWORD m = 512;
wchar_t *tmp = buffer;
DWORD n = GetFullPathNameW(widepath, m, tmp, nullptr);
if (n >= m-1)
{
tmp = new wchar_t[n+3];
n = GetFullPathNameW(widepath, n+3, tmp, nullptr);
}
delete [] widepath;
widepath = tmp;
#else
HANDLE h = CreateFileW(widepath,
GENERIC_READ, FILE_SHARE_READ , nullptr, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, nullptr);
delete [] widepath;
if (h == INVALID_HANDLE_VALUE)
{
return result;
}
wchar_t buffer[512];
widepath = buffer;
DWORD m = 512;
// GetFinalPathNameByHandle() is not supported on Windows XP
DWORD n = GetFinalPathNameByHandleW(h, widepath, m, 0);
if (n >= m)
{
widepath = new wchar_t[n+1];
n = GetFinalPathNameByHandleW(h, widepath, n+1, 0);
}
CloseHandle(h);
#endif
if (n != 0)
{
char *path = ConvertToUTF8(widepath);
if (widepath != buffer)
{
delete [] widepath;
}
if (path == 0)
{
return result;
}
#ifndef DICOM_DEPRECATE_WINXP
// Add extended prefix if not present
if (path[0] == '\0' ||
strncmp("\\\\?\\", path, 4) == 0 ||
strncmp("\\\\.\\", path, 4) == 0)
{
result = path;
}
else
{
size_t l = strlen(path);
if (l >= 2 && path[0] == '\\' && path[1] == '\\')
{
result = "\\\\?\\UNC\\";
result.append(&path[2]);
}
else
{
result = "\\\\?\\";
result.append(path);
}
}
#else
result = path;
#endif
delete [] path;
}
return result;
#else
#if _POSIX_VERSION >= 200809L
char *storage = nullptr;
#else
char *storage = static_cast<char *>(malloc(PATH_MAX));
#endif
std::string result;
char *path = realpath(this->Path.c_str(), storage);
if (path != nullptr)
{
result = path;
storage = path;
}
free(storage);
return result;
#endif
}
//----------------------------------------------------------------------------
void vtkDICOMFilePath::PushBack(const std::string& second)
{
if (this->Path.length() == 0 || RootLength(second) == 0)
{
this->Path = this->Join(second);
}
#ifdef _WIN32
else if (this->Path.length() == 4 && HasExtendedPrefix(this->Path))
{
this->Path = this->Join(second);
}
else if (RootLength(this->Path) == 2 && DriveLetter(this->Path) &&
(DriveLetter(this->Path) == DriveLetter(second) ||
RootLength(second) == 1))
{
this->Path = this->Join(second);
}
// Check if the separator changed, make it sticky.
size_t l = this->Path.length();
for (size_t i = 0; i < l; i++)
{
if (IsSeparator(this->Path[i]))
{
this->Separator = this->Path[i];
break;
}
}
#endif
}
//----------------------------------------------------------------------------
void vtkDICOMFilePath::PopBack()
{
const std::string& path = this->Path;
size_t root = RootLength(path);
size_t l = path.length();
#ifdef _WIN32
if (l >= 4 && HasExtendedPrefix(path))
{
// only allow backslash as a separator with the "\\?\" prefix
while (l > root && path[l-1] != '\\')
{
--l;
}
// strip all trailing slashes that aren't part of the root
while (l > root && path[l-1] == '\\')
{
--l;
}
}
else
#endif
{
// back up to the last separator
while (l > root && !IsSeparator(path[l-1]))
{
--l;
}
// strip all trailing slashes that aren't part of the root
while (l > root && IsSeparator(path[l-1]))
{
--l;
}
}
this->Path.resize(l);
}
//----------------------------------------------------------------------------
void vtkDICOMFilePath::StripTrailingSlash(std::string *path)
{
size_t l = path->length();
size_t r = RootLength(*path);
#ifdef _WIN32
if (r >= 4 && HasExtendedPrefix(*path))
{
// remove the trailing slash, if present and not the root
while (l > r && (*path)[l-1] == '\\')
{
path->resize(--l);
}
}
else
#endif
{
// remove the trailing slash, if present and not the root
while (l > r && IsSeparator((*path)[l-1]))
{
path->resize(--l);
}
}
}
//----------------------------------------------------------------------------
size_t vtkDICOMFilePath::RootLength(const std::string& path)
{
size_t root = 0;
size_t l = path.length();
#ifdef _WIN32
// Several possible base prefixes exist on Windows:
// \ (the root of the current device)
// A: (indicates a relative path on a device)
// A:\ (the root of a device)
// \\server\share\ (a UNC path prefix)
// There's also the special "\\?\" prefix, which allows paths to go beyond
// the usual 260 char limits, but is only allowed for absolute paths and
// does not allows substitutions such as "/" for "\" or "." and "..".
// \\?\A:\ (the root of a device)
// \\?\UNC\server\share\ (a UNC path prefix)
// Finally, the special "\\.\" prefix is used to name system devices:
// \\.\DEVICE (name of a special devices)
if (l >= 4 && HasExtendedPrefix(path))
{
root = 4;
if (path[2] == '\?')
{
if (l >= 7 && path[4] == 'U' && path[5] == 'N' && path[6] == 'C')
{
if (l == 7)
{
root = 7;
}
else if (l > 7 && path[7] == '\\')
{
root = 8;
while (root != l && path[root] != '\\')
{
root++;
}
if (root != l && path[root] == '\\')
{
root++;
}
while (root != l && path[root] != '\\')
{
root++;
}
}
}
else if (l > 5 && path[5] == ':' &&
((path[4] >= 'A' && path[4] <= 'Z') ||
(path[4] >= 'a' && path[4] <= 'z')))
{
root = 6;
if (l > 6 && path[6] == '\\')
{
root = 7;
}
}
}
}
else
{
if (l > 0 && IsSeparator(path[0]))
{
root = 1;
if (l > 1 && IsSeparator(path[1]))
{
// UNC path
root = 2;
while (root != l && !IsSeparator(path[root]))
{
root++;
}
if (root != l && IsSeparator(path[root]))
{
root++;
}
while (root != l && !IsSeparator(path[root]))
{
root++;
}
}
}
else if (l >= 2 && path[1] == ':' &&
((path[0] >= 'A' && path[0] <= 'Z') ||
(path[0] >= 'a' && path[0] <= 'z')))
{
root = 2;
if (l > 2 && IsSeparator(path[2]))
{
root = 3;
}
}
}
#else
// For UNIX, the only possible root is "/"
if (l > 0 && IsSeparator(path[0]))
{
root = 1;
}
#endif
return root;
}
//----------------------------------------------------------------------------
#ifdef _WIN32
const wchar_t *vtkDICOMFilePath::Wide()
{
delete [] this->WidePath;
this->WidePath = ConvertToWideChar(this->Path.c_str());
return this->WidePath;
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
const char *vtkDICOMFilePath::Local()
{
delete [] this->LocalPath;
this->LocalPath = nullptr;
if (this->Wide())
{
this->LocalPath = ConvertToLocal(this->WidePath);
}
return this->LocalPath;
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
bool vtkDICOMFilePath::HasExtendedPrefix(const std::string& path)
{
// extended prefixes are '\\?\' and '\\.\'
return (path.length() >= 4 &&
path[0] == '\\' && path[1] == '\\' && path[3] == '\\' &&
(path[2] == '\?' || path[2] == '.'));
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
char vtkDICOMFilePath::DriveLetter(const std::string& path)
{
char d = '\0';
size_t l = path.length();
size_t pos = 0;
if (l >= 4 && HasExtendedPrefix(path))
{
pos = 4;
}
if (l >= pos+2 && path[pos+1] == ':')
{
char e = path[pos];
if (e >= 'A' && e <= 'Z')
{
d = e;
}
else if (e >= 'a' && e <= 'z')
{
d = e - ('a' - 'A');
}
}
return d;
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
wchar_t *vtkDICOMFilePath::ConvertToWideChar(const char *filename)
{
wchar_t *wideFilename = nullptr;
int n = MultiByteToWideChar(
CP_UTF8, 0, filename, -1, nullptr, 0);
if (n > 0)
{
wideFilename = new wchar_t[n];
n = MultiByteToWideChar(
CP_UTF8, 0, filename, -1, wideFilename, n);
if (n == 0)
{
delete [] wideFilename;
wideFilename = nullptr;
}
}
return wideFilename;
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
char *vtkDICOMFilePath::ConvertToUTF8(const wchar_t *wideFilename)
{
char *filename = nullptr;
int n = WideCharToMultiByte(
CP_UTF8, 0, wideFilename, -1, nullptr, 0, 0, 0);
if (n > 0)
{
filename = new char[n];
n = WideCharToMultiByte(
CP_UTF8, 0, wideFilename, -1, filename, n, 0, 0);
if (n == 0)
{
delete [] filename;
filename = nullptr;
}
}
return filename;
}
#endif
//----------------------------------------------------------------------------
#ifdef _WIN32
char *vtkDICOMFilePath::ConvertToLocal(const wchar_t *wideFilename)
{
char *filename = nullptr;
int n = WideCharToMultiByte(
CP_ACP, 0, wideFilename, -1, nullptr, 0, 0, 0);
if (n > 0)
{
filename = new char[n];
n = WideCharToMultiByte(
CP_ACP, 0, wideFilename, -1, filename, n, 0, 0);
if (n == 0)
{
delete [] filename;
filename = nullptr;
}
}
return filename;
}
#endif
|