18 #include <itkImageFileReader.h>
19 #include <itksys/Directory.hxx>
20 #include <itksys/SystemTools.hxx>
25 typedef std::vector<std::string> StringContainer;
26 typedef std::map<unsigned int, std::string> SortedStringContainer;
30 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"FileName must be non-empty");
40 std::string basename, path;
41 path = itksys::SystemTools::GetFilenamePath(
m_FileName);
42 basename = itksys::SystemTools::GetFilenameName(
m_FileName);
44 unsigned int digitBegin = 0;
45 unsigned int digitEnd = 0;
46 bool digitFound =
false;
47 for (
unsigned int i = basename.length() - 1;; --i)
49 char character = basename[i];
50 if (character >=
'0' && character <=
'9')
77 itkWarningMacro(
"Filename contains no digit!");
84 unsigned int prefixBegin = 0;
85 unsigned int prefixLength = digitBegin;
86 unsigned int extensionBegin = digitEnd + 1;
87 unsigned int extensionLength = (digitEnd == basename.length() - 1 ? 0 : basename.length() - 1 - digitEnd);
88 unsigned int numberLength = digitEnd - digitBegin + 1;
93 std::string prefix =
"";
94 if (prefixLength != 0)
95 prefix = basename.substr(prefixBegin, prefixLength);
96 std::string extension =
"";
97 if (extensionLength != 0)
98 extension = basename.substr(extensionBegin, extensionLength);
114 if ((prefixLength + extensionLength + numberLength) != basename.length())
116 throw itk::ImageFileReaderException(
117 __FILE__, __LINE__,
"prefixLength + extensionLength + numberLength != basenameLength");
123 std::string directory = itksys::SystemTools::GetFilenamePath(
m_FileName);
124 itksys::Directory itkDir;
125 if (!itkDir.Load(directory.c_str()))
127 itkWarningMacro(<<
"Directory " << directory <<
" cannot be read!");
134 StringContainer unmatchedFiles;
136 for (
unsigned long i = 0; i < itkDir.GetNumberOfFiles(); i++)
139 std::string
filename = directory +
"/" + itkDir.GetFile(i);
140 if (itksys::SystemTools::FileIsDirectory(filename.c_str()))
144 unmatchedFiles.push_back(itkDir.GetFile(i));
151 StringContainer matchedFiles;
152 for (
auto it = unmatchedFiles.begin(); it != unmatchedFiles.end(); ++it)
154 bool prefixMatch =
false;
155 bool extensionMatch =
false;
158 if (prefixLength != 0)
159 prefixMatch = (it->find(prefix) == prefixBegin);
161 prefixMatch = (((*it)[0] >=
'0') && ((*it)[0] <=
'9'));
164 if (extensionLength != 0)
165 extensionMatch = (it->find(extension) == it->length() - extensionLength);
168 (((*it)[it->length() - 1] >=
'0') && ((*it)[it->length() - 1] <=
'9'));
170 if (prefixMatch && extensionMatch)
172 matchedFiles.push_back(*it);
175 if (matchedFiles.size() == 0)
177 itkWarningMacro(<<
"Sorry, none of the files matched the prefix!");
186 SortedStringContainer sortedFiles;
187 for (
auto it = matchedFiles.begin(); it != matchedFiles.end(); ++it)
191 std::string number =
"";
192 std::string currentFilename(*it);
193 for (
unsigned int i = digitBegin; i < currentFilename.length(); ++i)
195 char character = currentFilename[i];
197 if (character >=
'0' && character <=
'9')
202 if (number.length() == 0)
206 itkWarningMacro(<<
"The filename " << *it
207 <<
"does not contain a valid digit sequence but matches prefix and extension. Skipping file!");
211 if ((number.length() + prefix.length() + extension.length()) != it->length())
216 <<
" matches prefix and extension, but the string in beteen is not a single digit-sequence. Skipping file!");
222 unsigned int num = atoi(number.c_str());
223 sortedFiles.insert(std::make_pair(num, directory +
"/" + *it));
227 if (sortedFiles.size() == 0)
229 itkWarningMacro(<<
"Sorry, no numbered files found, I can't load anything...");
238 unsigned long index = 0;
239 for (
auto it = sortedFiles.begin(); it != sortedFiles.end(); ++it, ++index)
242 MITK_INFO <<
"Added " << it->second <<
" to the set of matched files!" << std::endl;
249 return m_MatchedFileNames;
virtual mitkClassMacro(FileSeriesReader, FileReader) typedef std MatchedFileNames GetMatchedFileNames()
virtual bool GenerateFileList()
virtual ~FileSeriesReader()
static const std::string filename
MatchedFileNames m_MatchedFileNames