| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkDirectory.cxx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:35 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
DEF |
=========================================================================*/ |
| 17 |
|
#include "itkDirectory.h" |
| 18 |
|
|
| 19 |
|
namespace itk |
| 20 |
|
{ |
| 21 |
|
|
| 22 |
|
/** |
| 23 |
|
* |
| 24 |
|
*/ |
| 25 |
|
Directory |
| 26 |
|
::Directory() |
| 27 |
|
{ |
| 28 |
|
} |
| 29 |
|
|
| 30 |
|
/** |
| 31 |
|
* |
| 32 |
|
*/ |
| 33 |
|
Directory |
| 34 |
|
::~Directory() |
| 35 |
|
{ |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
/* |
| 39 |
|
ostream& operator << (ostream& os, string& s) |
| 40 |
|
{ |
| 41 |
|
os << s.c_str(); |
| 42 |
|
return os; |
| 43 |
|
} |
| 44 |
|
*/ |
| 45 |
|
|
| 46 |
|
/** |
| 47 |
|
* |
| 48 |
|
*/ |
| 49 |
|
void |
| 50 |
|
Directory |
| 51 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 52 |
|
{ |
| 53 |
|
Superclass::PrintSelf(os, indent); |
| 54 |
|
os << indent << "Directory for: " << m_Path << "\n"; |
| 55 |
|
os << indent << "Contains the following files:\n"; |
| 56 |
|
indent = indent.GetNextIndent(); |
| 57 |
|
for(std::vector<std::string>::const_iterator i = m_Files.begin(); |
| 58 |
IND |
******i != m_Files.end(); ++i) |
| 59 |
|
{ |
| 60 |
|
os << indent << (*i) << "\n"; |
| 61 |
|
} |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
} // end namespace itk |
| 65 |
|
|
| 66 |
|
// First microsoft compilers |
| 67 |
|
|
| 68 |
|
#ifdef _MSC_VER |
| 69 |
|
#include "itkWindows.h" |
| 70 |
|
#include <io.h> |
| 71 |
|
#include <ctype.h> |
| 72 |
|
#include <fcntl.h> |
| 73 |
|
#include <stdio.h> |
| 74 |
|
#include <stdlib.h> |
| 75 |
|
#include <string.h> |
| 76 |
|
#include <sys/stat.h> |
| 77 |
|
#include <sys/types.h> |
| 78 |
|
|
| 79 |
|
namespace itk |
| 80 |
|
{ |
| 81 |
|
|
| 82 |
|
/** |
| 83 |
|
* |
| 84 |
|
*/ |
| 85 |
|
bool |
| 86 |
|
Directory |
| 87 |
|
::Load(const char* name) |
| 88 |
|
{ |
| 89 |
|
char* buf; |
| 90 |
|
int n = static_cast<int>( strlen(name) ); |
| 91 |
|
if ( name[n - 1] == '/' ) |
| 92 |
|
{ |
| 93 |
|
buf = new char[n + 1 + 1]; |
| 94 |
|
sprintf(buf, "%s*", name); |
| 95 |
|
} |
| 96 |
|
else |
| 97 |
|
{ |
| 98 |
|
buf = new char[n + 2 + 1]; |
| 99 |
|
sprintf(buf, "%s/*", name); |
| 100 |
|
} |
| 101 |
|
struct _finddata_t data; // data of current file |
| 102 |
|
|
| 103 |
|
// Now put them into the file array |
| 104 |
|
long srchHandle = _findfirst(buf, &data); |
| 105 |
|
delete [] buf; |
| 106 |
|
|
| 107 |
|
if ( srchHandle == -1 ) |
| 108 |
|
{ |
| 109 |
|
return 0; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
// Loop through names |
| 113 |
|
do |
| 114 |
|
{ |
| 115 |
|
m_Files.push_back(data.name); |
| 116 |
|
} |
| 117 |
|
while ( _findnext(srchHandle, &data) != -1 ); |
| 118 |
|
m_Path = name; |
| 119 |
|
return _findclose(srchHandle) != -1; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
} // end namespace itk |
| 123 |
|
|
| 124 |
|
#else |
| 125 |
|
|
| 126 |
|
// Now the POSIX style directory access |
| 127 |
|
|
| 128 |
|
#include <sys/types.h> |
| 129 |
|
#include <dirent.h> |
| 130 |
|
|
| 131 |
|
namespace itk |
| 132 |
IND |
{ |
| 133 |
|
|
| 134 |
IND |
/** |
| 135 |
IND |
** |
| 136 |
IND |
**/ |
| 137 |
IND |
bool |
| 138 |
IND |
Directory |
| 139 |
IND |
::Load(const char* name) |
| 140 |
IND |
{ |
| 141 |
IND |
**DIR* dir = opendir(name); |
| 142 |
IND |
**if ( !dir ) |
| 143 |
IND |
****{ |
| 144 |
IND |
****return 0; |
| 145 |
IND |
****} |
| 146 |
IND |
**for (dirent* d = readdir(dir); d; d = readdir(dir) ) |
| 147 |
IND |
****{ |
| 148 |
IND |
****m_Files.push_back(d->d_name); |
| 149 |
IND |
****} |
| 150 |
IND |
**m_Path = name; |
| 151 |
IND |
**closedir(dir); |
| 152 |
IND |
**return 1; |
| 153 |
IND |
} |
| 154 |
|
|
| 155 |
IND |
} // end namespace itk |
| 156 |
|
|
| 157 |
|
#endif |
| 158 |
|
|
| 159 |
|
namespace itk |
| 160 |
|
{ |
| 161 |
|
|
| 162 |
|
/** |
| 163 |
|
* |
| 164 |
|
*/ |
| 165 |
|
const char* |
| 166 |
|
Directory |
| 167 |
|
::GetFile(unsigned int index) |
| 168 |
|
{ |
| 169 |
|
if ( index >= m_Files.size() ) |
| 170 |
|
{ |
| 171 |
|
itkGenericOutputMacro( << "Bad index for GetFile on itk::Directory\n"); |
| 172 |
|
return 0; |
| 173 |
|
} |
| 174 |
|
|
| 175 |
|
return m_Files[index].c_str(); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
} // end namespace itk |
| 179 |
|
|
| 180 |
EOF |
|