]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeAttributeDescriptor.cpp
5a3e5b47f6559dd09c95709b3a611c89ce320a40
[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     }
47
48     //=====================================================================
49
50      //=====================================================================
51    // Ctor with dicom group, elem and flags
52     // The key is built as 'Dgroup_elem'
53     // The user name is retreived from dicom dictionnary
54     AttributeDescriptor::AttributeDescriptor(unsigned short group,
55                                              unsigned short element,
56                                              unsigned int flags)
57       : mGroup(group), mElement(element), mFlags(flags)
58     {
59       
60       //GDCM_NAME_SPACE::TagKey tag(group,element);
61       char ctag[12];
62       sprintf(ctag,"D%04x_%04x",group,element);
63       std::string tag(ctag);
64
65       GimmickDebugMessage(3,"AttributeDescriptor : '"<<tag
66                           <<"' ["<<flags<<"]"<<std::endl);
67
68       mKey = "D" + tag;
69
70       // Retrieve the name from gdcm dict
71       GDCM_NAME_SPACE::DictEntry* entry =
72         GDCM_NAME_SPACE::Global::GetDicts()
73         ->GetDefaultPubDict()->GetEntry(mGroup,mElement);
74
75       if (entry)
76         {
77           mName = entry->GetName();
78           CleanName(mName);
79           GimmickDebugMessage(3,"='"<<mName<<"'"<<std::endl);
80         }
81       else
82         {
83           GimmickMessage(1,"!! WARNING : tag '"<<tag
84                          <<"' is not in DICOM dictionnary ! "
85                          <<"Considering it as a user attribute"
86                          << std::endl);
87           mName = "UNKNOWN";
88           mGroup = mElement = 0;
89         }
90       
91     }
92     //=====================================================================
93
94
95     //=====================================================================
96     /// Extracts group and element from a key of the form "Dgroup_elem" 
97     void AttributeDescriptor::GetDicomGroupElementFromKey(const std::string& key,
98                                                           unsigned short& group,
99                                                           unsigned short& elem)
100     {
101       group = elem = 0;
102       if ( (key.size()==10) &&
103            (key[0] == 'D') &&
104            (key[5] == '_') )
105         {
106           sscanf(key.c_str(),"D%04x_%04x",&group,&elem);
107         }
108       return;
109     }
110     //=====================================================================
111
112   } // EO namespace tree
113
114 } // EO namespace creaImageIO