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