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
|
/*
*
* Copyright (C) 2021-2024, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Jan Schlamelcher
*
* Purpose: Implementing the currently used subset of C++17' std::filesystem
*
*/
#include "dcmtk/config/osconfig.h"
#include <cstring>
#include <cassert>
#include <climits>
#include "dcmtk/ofstd/offilsys.h"
#ifdef _WIN32
#include <io.h>
#else // _WIN32
BEGIN_EXTERN_C
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h> // for struct DIR, opendir()
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h> // for struct DIR, opendir()
#endif
END_EXTERN_C
/* Check if we are using glibc in a version where readdir() is known to be
* thread-safe and where readdir_r() is deprecated.
*/
#if defined(__GLIBC__) && (((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 24)) || (__GLIBC__ >= 3))
#define READDIR_IS_THREADSAFE
#endif
#endif // _WIN32
OFpath::OFpath()
: m_NativeString()
{
}
OFpath::OFpath( const OFpath& rhs )
: m_NativeString( rhs.native() )
{
}
OFpath::OFpath( OFrvalue_ref(OFpath) rhs )
: m_NativeString()
{
m_NativeString.swap( OFrvalue_access( rhs ).m_NativeString );
}
OFpath::OFpath( const char* const cstr, format fmt )
: m_NativeString( cstr )
{
#ifdef _WIN32
convertSeparator(fmt);
#endif
}
OFpath::OFpath( const OFString& string, format fmt )
: m_NativeString( string )
{
#ifdef _WIN32
convertSeparator(fmt);
#endif
}
#ifdef _WIN32
void OFpath::convertSeparator( format fmt )
{
size_t pos = OFString_npos;
switch( fmt )
{
case native_format:
return;
case auto_format:
pos = m_NativeString.find_first_of( "\\/" );
if( pos == OFString_npos || m_NativeString[pos] == preferred_separator )
return;
break;
default:
case generic_format:
pos = m_NativeString.find( '/' );
if( pos == OFString_npos )
return;
break;
}
m_NativeString[pos] = preferred_separator;
for( pos = m_NativeString.find( '/', pos + 1 ); pos != OFString_npos; pos = m_NativeString.find( '/', pos + 1 ) )
m_NativeString[pos] = preferred_separator;
}
#endif
OFpath& OFpath::operator=( const OFpath& rhs )
{
m_NativeString = rhs.native();
return *this;
}
OFpath& OFpath::operator=( OFrvalue_ref(OFpath) rhs )
{
if( this != &OFrvalue_access( rhs ) )
{
m_NativeString.clear();
m_NativeString.swap( OFrvalue_access( rhs ).m_NativeString );
}
return *this;
}
OFBool OFpath::empty() const
{
return m_NativeString.empty();
}
OFBool OFpath::is_absolute() const
{
#if _WIN32
const std::size_t pos = findRootName();
return OFString_npos != pos &&
(
( pos + 1 ) == m_NativeString.size() ||
preferred_separator == m_NativeString[pos+1]
);
#else
return has_root_directory();
#endif
}
OFBool OFpath::is_relative() const
{
return !is_absolute();
}
OFBool OFpath::has_root_name() const
{
return OFString_npos != findRootName();
}
OFBool OFpath::has_root_directory() const
{
#if _WIN32
size_t pos = findRootName();
pos = ( OFString_npos != pos ? pos + 1 : 0 );
return pos < m_NativeString.size() && preferred_separator == m_NativeString[pos];
#else
return !empty() && preferred_separator == *m_NativeString.begin();
#endif
}
OFBool OFpath::has_filename() const
{
#if _WIN32
OFString::const_iterator it = m_NativeString.end();
if( it == m_NativeString.begin() )
return OFFalse;
--it;
if( *it == preferred_separator )
return OFFalse;
if( *it != ':' )
return OFTrue;
while( it != m_NativeString.begin() )
if( *--it == preferred_separator )
return OFTrue;
return OFFalse;
#else
return !empty() && preferred_separator != *(m_NativeString.end() - 1);
#endif
}
OFBool OFpath::has_extension() const
{
return OFString_npos != findExtension();
}
const OFString& OFpath::native() const
{
return m_NativeString;
}
const char* OFpath::c_str() const
{
return m_NativeString.c_str();
}
OFrvalue<OFpath> OFpath::root_name() const
{
#if _WIN32
const size_t pos = findRootName();
if( OFString_npos != pos )
return OFpath( m_NativeString.substr( 0, pos + 1 ) );
#endif
return OFpath();
}
OFrvalue<OFpath> OFpath::filename() const
{
const size_t pos = findFilename();
if( OFString_npos != pos )
return OFpath( m_NativeString.substr( pos ) );
return OFpath();
}
OFrvalue<OFpath> OFpath::extension() const
{
const size_t pos = findExtension();
if( OFString_npos != pos )
return OFpath( m_NativeString.substr( pos ) );
return OFpath();
}
OFpath& OFpath::operator/=( const OFpath& rhs )
{
// self append
if( this == &rhs )
return *this /= OFpath( rhs );
#if _WIN32
// Comments are the descriptions from en.cppreference.com, put to whatever code segment handles it:
// If p.is_absolute() || (p.has_root_name() && p.root_name() != root_name()),
// then replaces the current path with p as if by operator=(p) and finishes.
std::size_t pos = rhs.findRootName();
if( OFString_npos != pos ) // .. p.has_root_name()
{
++pos;
if
(
// .. p.is_absolute()
( pos < rhs.m_NativeString.size() && rhs.m_NativeString[pos] == preferred_separator ) ||
// .. p.root_name() != root_name()
( pos > m_NativeString.size() || 0 != m_NativeString.compare( 0, pos - 1, rhs.m_NativeString, 0, pos - 1 ) )
)
{
// then replaces the current path with p as if by operator=(p) and finishes.
m_NativeString = rhs.m_NativeString;
return *this;
}
}
else pos = 0;
// Otherwise, if p.has_root_directory(), then removes any root directory and the
// entire relative path from the generic format pathname of *this, then appends the native format
// pathname of p, omitting any root-name from its generic format, to the native format of *this.
if( pos < rhs.m_NativeString.size() && rhs.m_NativeString[pos] == '\\' ) // .. p.has_root_directory()
{
// we shall remove ONLY the root directory and relative path, not the root name
// so find it and, if it exists, keep it
const size_t root = findRootName();
if( OFString_npos == root )
{
// no root name, so replace the entire string
m_NativeString = rhs.m_NativeString;
}
else
{
// removes any root directory and the entire relative path from the generic format pathname of *this
// appends the native format pathname of p, omitting any root-name from its generic format
m_NativeString.replace( root + 1, OFString_npos, rhs.m_NativeString, pos, OFString_npos );
}
}
else
{
// If has_filename() || (!has_root_directory() && is_absolute())
if( !empty() && preferred_separator != *(m_NativeString.end() - 1) )
{
// then appends path::preferred_separator to the generic format of *this
m_NativeString.reserve( m_NativeString.size() + rhs.m_NativeString.size() - pos + 1 );
m_NativeString += preferred_separator;
}
// appends the native format pathname of p, omitting any root-name from its generic format
m_NativeString += rhs.m_NativeString.substr( pos );
}
#else
// version for filesystems without root names, pretty straight forward
if( !rhs.is_absolute() )
{
if( has_filename() )
{
m_NativeString.reserve( m_NativeString.size() + rhs.m_NativeString.size() + 1 );
m_NativeString += preferred_separator;
}
m_NativeString += rhs.m_NativeString;
}
else m_NativeString = rhs.m_NativeString;
#endif
return *this;
}
size_t OFpath::findRootName() const
{
#if _WIN32
const size_t pos = m_NativeString.find_first_of( ":\\" );
if( OFString_npos != pos && m_NativeString[pos] == ':' )
return pos;
#endif
return OFString_npos;
}
size_t OFpath::findFilename() const
{
#if _WIN32
const size_t pos = m_NativeString.find_last_of( ":\\" );
#else
const size_t pos = m_NativeString.find_last_of( "/" );
#endif
if( pos == OFString_npos )
return empty() ? OFString_npos : 0;
else
return pos < m_NativeString.size() ? pos + 1 : OFString_npos;
}
size_t OFpath::findExtension() const
{
#if _WIN32
const size_t pos = m_NativeString.find_last_of( ".:\\" );
#else
const size_t pos = m_NativeString.find_last_of( "./" );
#endif
if
(
pos && OFString_npos != pos && m_NativeString[pos] == '.'
)
{
switch( m_NativeString[pos-1] )
{
#if _WIN32
case '\\':
case ':':
#else
case '/':
#endif
return OFString_npos;
case '.':
if( pos < m_NativeString.size() - 1 )
return pos;
if( pos == 1 )
return OFString_npos;
switch( m_NativeString[pos-2] )
{
#if _WIN32
case '\\':
case ':':
#else
case '/':
#endif
return OFString_npos;
default:
break;
}
default:
break;
}
return pos;
}
return OFString_npos;
}
const OFpath& OFdirectory_entry::path() const
{
return m_Path;
}
OFdirectory_entry::operator const OFpath&() const
{
return OFdirectory_entry::path();
}
OFdirectory_iterator_proxy::OFdirectory_iterator_proxy( const OFdirectory_entry& rhs )
: m_Entry( rhs )
{
}
class OFdirectory_iterator::NativeDirectoryEntry : public OFdirectory_entry
{
public:
NativeDirectoryEntry( const OFpath& path );
~NativeDirectoryEntry();
OFBool skipInvalidFiles();
OFBool next();
private:
OFBool good() const;
const char* filename() const;
const OFpath m_Parent;
#ifdef HAVE__FINDFIRST // HAVE__FINDFIRST
::_finddata_t m_FileData;
OFintptr_t m_hFile;
#else // HAVE__FINDFIRST
::DIR* m_pDIR;
::dirent* m_pDirent;
#if defined(HAVE_READDIR_R) && !defined(READDIR_IS_THREADSAFE)
Uint8 m_Buffer[sizeof(::dirent) + _POSIX_PATH_MAX + 1];
#endif
#endif // HAVE__FINDFIRST
};
OFdirectory_iterator::NativeDirectoryEntry::NativeDirectoryEntry( const OFpath& path )
: OFdirectory_entry()
, m_Parent( path )
#ifdef HAVE__FINDFIRST
, m_FileData()
, m_hFile( _findfirst( OFconst_cast( char*, (path / "*").c_str() ), &m_FileData ) )
#else // HAVE__FINDFIRST
, m_pDIR( ::opendir( path.c_str() ) )
, m_pDirent()
#if defined(HAVE_READDIR_R) && !defined(READDIR_IS_THREADSAFE)
, m_Buffer()
#endif
#endif // HAVE__FINDFIRST
{
#ifndef HAVE__FINDFIRST
if( m_pDIR && !next() )
{
::closedir( m_pDIR );
m_pDIR = OFnullptr;
}
#endif
}
OFdirectory_iterator::NativeDirectoryEntry::~NativeDirectoryEntry()
{
#ifdef HAVE__FINDFIRST
::_findclose( m_hFile );
#else
if( m_pDIR ) ::closedir( m_pDIR );
#endif
}
OFBool OFdirectory_iterator::NativeDirectoryEntry::good() const
{
#ifdef HAVE__FINDFIRST
return -1 != m_hFile;
#else
return m_pDIR;
#endif
}
const char* OFdirectory_iterator::NativeDirectoryEntry::filename() const
{
#ifdef HAVE__FINDFIRST
return m_FileData.name;
#else
return m_pDirent->d_name;
#endif
}
OFBool OFdirectory_iterator::NativeDirectoryEntry::next()
{
#ifdef HAVE__FINDFIRST // HAVE__FINDFIRST
return !::_findnext( m_hFile, &m_FileData );
#else // HAVE__FINDFIRST
#if defined(HAVE_READDIR_R) && !defined(READDIR_IS_THREADSAFE)
return !::readdir_r( m_pDIR, OFreinterpret_cast(dirent*, m_Buffer ), &m_pDirent ) && m_pDirent;
#else // HAVE_READDIR_R && !READDIR_IS_THREADSAFE
return (m_pDirent = ::readdir( m_pDIR ) );
#endif // HAVE_READDIR_R && !READDIR_IS_THREADSAFE
#endif // HAVE__FINDFIRST
}
OFBool OFdirectory_iterator::NativeDirectoryEntry::skipInvalidFiles()
{
while( good() && ( !::strcmp( filename(), "." ) || !::strcmp( filename(), ".." ) ) )
{
if( !next() )
return OFFalse;
}
if( good() )
{
OFdirectory_entry::m_Path = m_Parent / filename();
return OFTrue;
}
return OFFalse;
}
OFdirectory_iterator::OFdirectory_iterator()
: m_pEntry()
{
}
OFdirectory_iterator::OFdirectory_iterator( const OFpath& path )
: m_pEntry( new NativeDirectoryEntry( path ) )
{
assert( m_pEntry );
if( !m_pEntry->skipInvalidFiles() )
m_pEntry.reset();
}
OFdirectory_iterator::OFdirectory_iterator( const OFdirectory_iterator& rhs )
: m_pEntry( rhs.m_pEntry )
{
}
OFdirectory_iterator& OFdirectory_iterator::operator=( const OFdirectory_iterator& rhs )
{
m_pEntry = rhs.m_pEntry;
return *this;
}
OFdirectory_iterator::~OFdirectory_iterator()
{
}
OFBool OFdirectory_iterator::operator==( const OFdirectory_iterator& rhs ) const
{
return m_pEntry == rhs.m_pEntry;
}
OFBool OFdirectory_iterator::operator!=( const OFdirectory_iterator& rhs ) const
{
return m_pEntry != rhs.m_pEntry;
}
OFdirectory_iterator& OFdirectory_iterator::operator++()
{
assert( m_pEntry );
if( !m_pEntry->next() || !m_pEntry->skipInvalidFiles() )
m_pEntry.reset();
return *this;
}
OFrvalue<OFdirectory_iterator_proxy> OFdirectory_iterator::operator++(int)
{
assert( m_pEntry );
OFdirectory_iterator_proxy proxy( *m_pEntry );
++(*this);
return proxy;
}
OFdirectory_iterator::pointer OFdirectory_iterator::operator->() const
{
assert( m_pEntry );
return m_pEntry.get();
}
OFdirectory_iterator::reference OFdirectory_iterator::operator*() const
{
assert( m_pEntry );
return *m_pEntry;
}
|