]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeAttributeDescriptor.cpp
Order by columns is done. For the moment it only compares as if everything were a...
[creaImageIO.git] / src2 / creaImageIOTreeAttributeDescriptor.cpp
1 #include <creaImageIOTreeAttributeDescriptor.h>
2 #include <creaImageIOSystem.h>
3
4 #include <gdcmGlobal.h>
5 #include <gdcmDictSet.h>
6
7 #include <boost/algorithm/string/replace.hpp>
8
9 namespace creaImageIO
10 {
11
12   namespace tree
13   {
14
15     //========================================================================
16     void AttributeDescriptor::CleanName(std::string& str) const
17     {
18       // quote must be doubled for SQL
19       //    crea::Utils::Replace( str, "'", "''" );
20       boost::algorithm::replace_all(str,"'","''");
21       // Found strange strings which contained NULL char INSIDE string 
22       int i,size=str.size();
23       for (i=0;i<size;++i) 
24         {
25           if (str[i]==0) 
26             {
27               str = str.substr(0,i);
28               break;
29             }
30         }
31     }
32     //========================================================================
33
34     //=====================================================================
35     // Ctor with key, name and flags
36      AttributeDescriptor::AttributeDescriptor(const std::string& key,
37                                              const std::string& name,
38                                              unsigned int flags)
39       : mKey(key), mName(name), mGroup(0), mElement(0), mFlags(flags)
40     {
41
42       CleanName(mName);
43       GimmickDebugMessage(3,"AttributeDescriptor : '"<<key
44                           <<"' ["<<flags<<"]"<<std::endl);
45       GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
46           DecodeType();
47     }
48
49     //=====================================================================
50
51      //=====================================================================
52    // Ctor with dicom group, elem and flags
53     // The key is built as 'Dgroup_elem'
54     // The user name is retreived from dicom dictionnary
55     AttributeDescriptor::AttributeDescriptor(unsigned short group,
56                                              unsigned short element,
57                                              unsigned int flags)
58       : mGroup(group), mElement(element), mFlags(flags)
59     {
60       
61       //GDCM_NAME_SPACE::TagKey tag(group,element);
62       char ctag[12];
63       sprintf(ctag,"D%04x_%04x",group,element);
64       mKey = ctag;
65
66       GimmickDebugMessage(3,"AttributeDescriptor : '"<<mKey
67                           <<"' ["<<flags<<"]"<<std::endl);
68
69       // Retrieve the name from gdcm dict
70       GDCM_NAME_SPACE::DictEntry* entry =
71         GDCM_NAME_SPACE::Global::GetDicts()
72         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
73           
74       if (entry)
75         {
76           mName = entry->GetName();
77           CleanName(mName);
78           DecodeType();
79           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
80         }
81       else
82         {
83           GimmickMessage(1,"!! WARNING : tag '"<<mKey
84                          <<"' is not in DICOM dictionnary ! "
85                          <<"Considering it as a user attribute"
86                          << std::endl);
87           mName = "UNKNOWN";
88           mGroup = mElement = mType = 0;
89           DecodeType();
90         }
91       
92     }
93     //=====================================================================
94
95
96     //=====================================================================
97     /// Extracts group and element from a key of the form "Dgroup_elem" 
98     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
99                                                           unsigned short& group,
100                                                           unsigned short& elem)
101     {
102       group = elem = 0;
103       if ( (key.size()==10) &&
104            (key[0] == 'D') &&
105            (key[5] == '_') )
106         {
107           std::string g = key.substr(1,4);
108           sscanf(key.c_str(),"D %04x _ %04x ",&group,&elem);  
109           sscanf(g.c_str(),"%04x",&group);
110           GimmickDebugMessage(3,"GetDicomGroupElementFromKey '"<<g<<"' : "
111                          <<group<<"|"<<elem<<std::endl);
112         }
113       else 
114         { 
115           GimmickMessage(5,"GetDicomGroupElementFromKey '"<<key<<"' : "
116                          <<" not a DICOM key format"<<std::endl);
117         }
118       return;
119     }
120     //=====================================================================
121         ///Decodes the type of attribute into the valid groups
122         void AttributeDescriptor::DecodeType()
123         {
124                 // Retrieve the name from gdcm dict
125                 GDCM_NAME_SPACE::DictEntry* entry =
126                 GDCM_NAME_SPACE::Global::GetDicts()
127                 ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
128
129                 std::string type = entry->GetVR().str();
130                 CleanName(type);
131                 GimmickDebugMessage(3,"VR Value is "<<type<<"!"<<std::endl);
132                 if(type=="AS" ||
133                 type=="DA" ||
134                 type=="FL" ||
135                 type=="FD" ||
136                 type=="IS" ||
137                 type=="SL" ||
138                 type=="SS" ||
139                 type=="UI" ||
140                 type=="US" ||
141                 type=="SH")
142                 {
143                         mType=1;
144                 }
145                 else
146                 {
147                         mType=2;
148                 }
149
150         }
151
152   } // EO namespace tree
153
154 } // EO namespace creaImageIO