]> Creatis software - gdcm.git/blob - src/gdcmValidator.cxx
Avoid poluting the output, right now
[gdcm.git] / src / gdcmValidator.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmValidator.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/07 11:42:25 $
7   Version:   $Revision: 1.10 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmValidator.h"
20 #include "gdcmElementSet.h"
21 #include "gdcmDataEntry.h"
22 #include "gdcmUtil.h"
23 #include <map>
24
25 #include <sstream>
26
27 namespace gdcm 
28 {
29 //-----------------------------------------------------------------------------
30 typedef std::map<uint16_t, int> GroupHT;    //  Hash Table
31 //-----------------------------------------------------------------------------
32
33 Validator::Validator()
34 {
35 }
36
37 Validator::~Validator()
38 {
39 }
40
41 // Function to compare the VM found while parsing d->GetString()
42 // compare to the one from the dictionary
43 bool CheckVM(DataEntry *entry)
44 {
45   // Don't waste time checking tags where VM is OB and OW, since we know
46   // it's allways 1, whatever the actual length (found on disc)
47   
48   if ( entry->GetVR() == "OB" ||  entry->GetVR() == "OW" )
49      return true;
50      
51   const std::string &s = entry->GetString();
52
53   unsigned int n = Util::CountSubstring( s , "\\");
54   
55   n++; // number of '\' + 1 == Value Multiplicity
56
57   std::string vmFromDict = entry->GetVM();
58   if ( vmFromDict == "1-n" || vmFromDict == "2-n" || vmFromDict == "3-n" )
59      return true;
60      
61   unsigned int m;
62   std::istringstream is;
63   is.str( vmFromDict );
64   is >> m;
65
66   return n == m;
67 }
68
69 void Validator::SetInput(ElementSet *input)
70 {
71 /*
72 // First stage to check group length
73   GroupHT grHT;
74   DocEntry *d=input->GetFirstEntry();
75   while(d)
76   {
77     grHT[d->GetGroup()] = 0;
78     d=input->GetNextEntry();
79   }
80   for (GroupHT::iterator it = grHT.begin(); it != grHT.end(); ++it)  
81   {
82       std::cout << std::hex << it->first << std::endl; 
83   } 
84 */
85
86   // berk for now SetInput do two things at the same time
87   d=input->GetFirstEntry();
88   if (!d)
89   {
90      std::cout << "No Entry found" << std::endl;
91      return;
92   }
93   while(d)
94   { 
95     if ( DataEntry *v = dynamic_cast<DataEntry *>(d) )
96     { 
97       if ( v->GetVM() != gdcm::GDCM_UNKNOWN )
98          if ( !CheckVM(v) )
99          {
100            std::cout << "Tag (" <<  v->GetKey() 
101                      << ")-> [" << v->GetName() << "] contains an illegal VM. "
102                      << "value [" << v->GetString() << "] VR :"
103                      << v->GetVR() << ", Expected VM :" << v->GetVM() << " " 
104                      << std::endl;
105          }
106       
107       if ( v->GetReadLength() % 2 )
108       {
109         std::cout << "Tag (" <<  v->GetKey() 
110                   << ")-> [" << v->GetName() << "] has an uneven length :"
111                   << v->GetReadLength()
112                   << " [" << v->GetString() << "] " 
113                   << std::endl;         
114       }
115     }
116     else
117     {
118       // We skip pb of SQ recursive exploration
119     }
120     d=input->GetNextEntry();
121   }
122 }
123
124 } // end namespace gdcm