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