]> Creatis software - gdcm.git/blob - src/gdcmVRKey.h
9ba7940313e7af883ae3fed843c68a0255877438
[gdcm.git] / src / gdcmVRKey.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmVRKey.h,v $
5   Language:  C++
6   Date:      $Date: 2008/04/10 12:15:36 $
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 #ifndef _GDCMVRKEY_H_
20 #define _GDCMVRKEY_H_
21
22 #include "gdcmCommon.h"
23
24 #include <assert.h>
25 #include <iomanip> // important
26 #include <iostream> // important
27 #include <string>
28
29 namespace GDCM_NAME_SPACE 
30 {
31 //-----------------------------------------------------------------------------
32 class VRKey
33 {
34 public :
35    inline VRKey() { key[0] = key[1] = ' ';}
36    inline VRKey(const char *_key) { key[0] = _key[0]; key[1] = _key[1];}
37    inline VRKey(const std::string &_key) { key[0] = _key[0]; key[1] = _key[1];}
38
39    inline std::string str() const { return std::string(key,2); }
40
41    friend std::ostream &operator<<(std::ostream &_os, const VRKey &_val);
42    friend std::istream &operator>>(std::istream &_is, VRKey &_val);
43
44    inline VRKey &operator=(const VRKey &_val)
45    {
46       key[0] = _val.key[0];
47       key[1] = _val.key[1];
48       return *this;
49    }
50    
51    inline VRKey &operator=(const std::string &_val)
52    {
53       key[0] = _val[0];
54       key[1] = _val[1];
55       return *this;
56    }
57    
58    inline VRKey &operator=(const char *_val)
59    {
60       key[0] = _val[0];
61       key[1] = _val[1];
62       return *this;
63    }
64
65    inline const char &operator[](const unsigned int &_id) const
66    {
67       assert(_id<2);
68       return key[_id];
69    }
70    
71    inline char &operator[](const unsigned int &_id)
72    {
73       assert(_id<2);
74       return key[_id];
75    }
76
77    inline bool operator==(const VRKey &_val) const
78    {
79       return key[0] == _val.key[0] && key[1] == _val.key[1];
80    }
81    
82    inline bool operator==(const std::string &_val) const
83    {
84       return key[0] == _val[0] && key[1] == _val[1];
85    }
86    
87    inline bool operator==(const char *_val) const
88    {
89       return key[0] == _val[0] && key[1] == _val[1];
90    }
91
92    inline bool operator!=(const VRKey &_val) const
93    {
94       return key[0] != _val.key[0] || key[1] != _val.key[1];
95    }
96    
97    inline bool operator!=(const std::string &_val) const
98    {
99       return key[0] != _val[0] || key[1] != _val[1];
100    }
101    inline bool operator!=(const char *_val) const
102    {
103       return key[0] != _val[0] || key[1] != _val[1];
104    }
105
106    inline bool operator<(const VRKey &_val) const
107    {
108       return key[0] < _val[0] || (key[0] == _val[0] && key[1] < _val[1]);
109    }
110
111    inline std::string GetHexaRepresentation()
112    {
113      // We could probabelly write something much more complicated using C++ features !
114      // (I really want HexaRepresentation as xx|xx, not ffffffxx|ffffffxx !)
115       char vr_char[6];
116       char buf[5];
117       sprintf(buf, "%04x",( unsigned short int)key[0]);
118       vr_char[0] = buf[2];
119       vr_char[1] = buf[3];      
120       sprintf(buf, "%04x",( unsigned short int)key[1]);
121       vr_char[2] = '|';
122       vr_char[3] = buf[2];            
123       vr_char[4] = buf[3];
124       vr_char[5] = '\0';
125       return(vr_char);
126    }
127    
128 private :
129    char key[2];
130 };
131
132 //-----------------------------------------------------------------------------
133 inline std::ostream &operator<<(std::ostream &_os, const VRKey &_val)
134 {
135    _os << _val.key[0] << _val.key[1];
136    return _os;
137 }
138
139 inline std::istream &operator>>(std::istream &_is, VRKey &_val)
140 {
141    _is >> _val.key[0] >> _val.key[1];
142    return _is;
143 }
144
145 //-----------------------------------------------------------------------------
146
147 } // end namespace gdcm
148
149 //-----------------------------------------------------------------------------
150 #endif