# gdcmScanner.h

Go to the documentation of this file.

    00001 /*=========================================================================
    00002
    00003   Program: GDCM (Grassroots DICOM). A DICOM library
    00004   Module:  $URL$
    00005
    00006   Copyright (c) 2006-2010 Mathieu Malaterre
    00007   All rights reserved.
    00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    00009
    00010      This software is distributed WITHOUT ANY WARRANTY; without even
    00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00012      PURPOSE.  See the above copyright notice for more information.
    00013
    00014 =========================================================================*/
    00015 #ifndef GDCMSCANNER_H
    00016 #define GDCMSCANNER_H
    00017
    00018 #include "gdcmDirectory.h"
    00019 #include "gdcmSubject.h"
    00020 #include "gdcmTag.h"
    00021 #include "gdcmPrivateTag.h"
    00022 #include "gdcmSmartPointer.h"
    00023
    00024 #include <map>
    00025 #include <set>
    00026 #include <string>
    00027
    00028 #include <string.h> // strcmp
    00029
    00030 namespace gdcm
    00031 {
    00032 class StringFilter;
    00033
    00056 class GDCM_EXPORT Scanner : public Subject
    00057 {
    00058   friend std::ostream& operator<<(std::ostream &_os, const Scanner &s);
    00059 public:
    00060   Scanner():Values(),Filenames(),Mappings() {}
    00061   ~Scanner();
    00062
    00069   typedef std::map<Tag, const char*> TagToValue;
    00070   //typedef std::map<Tag, ConstCharWrapper> TagToValue; //StringMap;
    00071   //typedef TagToStringMap TagToValue;
    00072   typedef TagToValue::value_type TagToValueValueType;
    00073
    00075   void AddTag( Tag const & t );
    00076   void ClearTags();
    00077
    00078   // Work in progress do not use:
    00079   void AddPrivateTag( PrivateTag const & t );
    00080
    00082   void AddSkipTag( Tag const & t );
    00083   void ClearSkipTags();
    00084
    00086   bool Scan( Directory::FilenamesType const & filenames );
    00087
    00088   Directory::FilenamesType const &GetFilenames() const { return Filenames; }
    00089
    00091   void Print( std::ostream & os ) const;
    00092
    00096   bool IsKey( const char * filename ) const;
    00097
    00100   Directory::FilenamesType GetKeys() const;
    00101
    00102   // struct to store all the values found:
    00103   typedef std::set< std::string > ValuesType;
    00104
    00106   ValuesType const & GetValues() const { return Values; }
    00107
    00109   ValuesType GetValues(Tag const &t) const;
    00110
    00111   /* ltstr is CRITICAL, otherwise pointers value are used to do the key comparison */
    00112   struct ltstr
    00113     {
    00114     bool operator()(const char* s1, const char* s2) const
    00115       {
    00116       assert( s1 && s2 );
    00117       return strcmp(s1, s2) < 0;
    00118       }
    00119     };
    00120   typedef std::map<const char *,TagToValue, ltstr> MappingType;
    00121   typedef MappingType::const_iterator ConstIterator;
    00122   ConstIterator Begin() const { return Mappings.begin(); }
    00123   ConstIterator End() const { return Mappings.end(); }
    00124
    00126   MappingType const & GetMappings() const { return Mappings; }
    00127
    00129   TagToValue const & GetMapping(const char *filename) const;
    00130
    00133   const char *GetFilenameFromTagToValue(Tag const &t, const char *valueref) const;
    00134
    00136   // by a call to GetMapping()
    00137   TagToValue const & GetMappingFromTagToValue(Tag const &t, const char *value) const;
    00138
    00144   const char* GetValue(const char *filename, Tag const &t) const;
    00145
    00147   static SmartPointer<Scanner> New() { return new Scanner; }
    00148
    00149 protected:
    00150   void ProcessPublicTag(StringFilter &sf, const char *filename);
    00151 private:
    00152   // struct to store all uniq tags in ascending order:
    00153   typedef std::set< Tag > TagsType;
    00154   typedef std::set< PrivateTag > PrivateTagsType;
    00155   std::set< Tag > Tags;
    00156   std::set< PrivateTag > PrivateTags;
    00157   std::set< Tag > SkipTags;
    00158   ValuesType Values;
    00159   Directory::FilenamesType Filenames;
    00160
    00161   // Main struct that will hold all mapping:
    00162   MappingType Mappings;
    00163
    00164   double Progress;
    00165 };
    00166 //-----------------------------------------------------------------------------
    00167 inline std::ostream& operator<<(std::ostream &os, const Scanner &s)
    00168 {
    00169   s.Print( os );
    00170   return os;
    00171 }
    00172
    00173 #if defined(SWIGPYTHON) || defined(SWIGCSHARP) || defined(SWIGJAVA)
    00174 /*
    00175  * HACK: I need this temp class to be able to manipulate a std::map from python,
    00176  * swig does not support wrapping of simple class like std::map...
    00177  */
    00178 class SWIGTagToValue
    00179 {
    00180 public:
    00181   SWIGTagToValue(Scanner::TagToValue const &t2v):Internal(t2v),it(t2v.begin()) {}
    00182   const Scanner::TagToValueValueType& GetCurrent() const { return *it; }
    00183   const Tag& GetCurrentTag() const { return it->first; }
    00184   const char *GetCurrentValue() const { return it->second; }
    00185   void Start() { it = Internal.begin(); }
    00186   bool IsAtEnd() const { return it == Internal.end(); }
    00187   void Next() { ++it; }
    00188 private:
    00189   const Scanner::TagToValue& Internal;
    00190   Scanner::TagToValue::const_iterator it;
    00191 };
    00192 #endif /* SWIG */
    00193
    00199 } // end namespace gdcm
    00200
    00201 #endif //GDCMSCANNER_H