fw4spl
fwDcmtkIO/src/fwDcmtkIO/helper/DicomSearch.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDcmtkIO/helper/DicomSearch.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <boost/algorithm/string/classification.hpp>
12 #include <boost/algorithm/string/predicate.hpp>
13 #include <boost/algorithm/string/split.hpp>
14 #include <boost/foreach.hpp>
15 
16 #include <dcmtk/dcmdata/dcfilefo.h>
17 #include <dcmtk/ofstd/ofcond.h>
18 
19 namespace fwDcmtkIO
20 {
21 namespace helper
22 {
23 
24 //------------------------------------------------------------------------------
25 
26 void DicomSearch::searchRecursively(const ::boost::filesystem::path& dirPath, std::vector<std::string>& dicomFiles)
27 {
28  std::vector<std::string> extensions;
29  std::string strIgnoreFile = ".zip|.txt|.htm|.html|.xml|.exe|.gz|.dir|.gif|.jpeg|.jpg|dicomdir|.DS_Store";
30  ::boost::algorithm::split( extensions, strIgnoreFile, ::boost::algorithm::is_any_of("|"),
31  ::boost::algorithm::token_compress_on);
32 
33  std::string filePath;
34  std::string filename;
35  for( ::boost::filesystem::recursive_directory_iterator it(dirPath);
36  it != ::boost::filesystem::recursive_directory_iterator(); ++it)
37  {
38  if(!::boost::filesystem::is_directory(*it))
39  {
40  filePath = it->path().string();
41  filename = it->path().filename().string();
42 
43  if(!DicomSearch::checkFilenameExtension( filePath, &extensions) )
44  {
45  DcmFileFormat fileFormat;
46  OFCondition ofCondition = fileFormat.loadFile(filePath.c_str());
47  if(ofCondition.good())
48  {
49  dicomFiles.push_back( filePath.c_str() );
50  }
51  else
52  {
53  throw std::runtime_error("failed to read " + filename + "\n"
54  + "dcmtk error: " + ofCondition.text());
55  }
56  }
57  }
58  }
59 }
60 
61 //------------------------------------------------------------------------------
62 
63 bool DicomSearch::checkFilenameExtension(const std::string& filename, std::vector<std::string>* extensions)
64 {
65  bool result = false;
66  for (size_t i = 0; i < extensions->size() && !result; ++i)
67  {
68  result = ::boost::ends_with(filename, extensions->at(i));
69  }
70  return result;
71 }
72 
73 //------------------------------------------------------------------------------
74 
75 } //namespace helper
76 } //namespace fwDcmtkIO
static FWDCMTKIO_API bool checkFilenameExtension(const std::string &filename, std::vector< std::string > *extensions)
Check if the file extension matches one of the forbidden extension.
static FWDCMTKIO_API void searchRecursively(const ::boost::filesystem::path &dirPath, std::vector< std::string > &dicomFiles)
Search Dicom files recursively.
fwDcmtkIO contains classes used to pull Dicom images from a pacs using dcmtk library.
Definition: Codec.hpp:12