1 /*========================================================================= 2 3 Program: GDCM (Grassroots DICOM). A DICOM library 4 5 Copyright (c) 2006-2011 Mathieu Malaterre 6 All rights reserved. 7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details. 8 9 This software is distributed WITHOUT ANY WARRANTY; without even 10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 11 PURPOSE. See the above copyright notice for more information. 12 13 =========================================================================*/ 14 #ifndef GDCMCSAELEMENT_H 15 #define GDCMCSAELEMENT_H 16 17 #include "gdcmTag.h" 18 #include "gdcmVM.h" 19 #include "gdcmVR.h" 20 #include "gdcmByteValue.h" 21 #include "gdcmSmartPointer.h" 22 23 namespace gdcm 24 { 25 /** 26 * \brief Class to represent a CSA Element 27 * \see CSAHeader 28 */ 29 class GDCM_EXPORT CSAElement 30 { 31 public: KeyField(kf)32 CSAElement(unsigned int kf = 0):KeyField(kf) {} 33 34 friend std::ostream& operator<<(std::ostream &os, const CSAElement &val); 35 36 /// Set/Get Key GetKey()37 unsigned int GetKey() const { return KeyField; } SetKey(unsigned int key)38 void SetKey(unsigned int key) { KeyField = key; } 39 40 /// Set/Get Name GetName()41 const char *GetName() const { return NameField.c_str(); } SetName(const char * name)42 void SetName(const char *name) { NameField = name; } 43 44 /// Set/Get VM GetVM()45 const VM& GetVM() const { return ValueMultiplicityField; } SetVM(const VM & vm)46 void SetVM(const VM &vm) { ValueMultiplicityField = vm; } 47 48 /// Set/Get VR GetVR()49 VR const &GetVR() const { return VRField; } SetVR(VR const & vr)50 void SetVR(VR const &vr) { VRField = vr; } 51 52 /// Set/Get SyngoDT GetSyngoDT()53 unsigned int GetSyngoDT() const { return SyngoDTField; } SetSyngoDT(unsigned int syngodt)54 void SetSyngoDT(unsigned int syngodt) { SyngoDTField = syngodt; } 55 56 /// Set/Get NoOfItems GetNoOfItems()57 unsigned int GetNoOfItems() const { return NoOfItemsField; } SetNoOfItems(unsigned int items)58 void SetNoOfItems(unsigned int items) { NoOfItemsField = items; } 59 60 /// Set/Get Value (bytes array, SQ of items, SQ of fragments): GetValue()61 Value const &GetValue() const { return *DataField; } GetValue()62 Value &GetValue() { return *DataField; } SetValue(Value const & vl)63 void SetValue(Value const & vl) { 64 //assert( DataField == 0 ); 65 DataField = vl; 66 } 67 /// Check if CSA Element is empty IsEmpty()68 bool IsEmpty() const { return DataField == nullptr; } 69 70 /// Set SetByteValue(const char * array,VL length)71 void SetByteValue(const char *array, VL length) 72 { 73 ByteValue *bv = new ByteValue(array,length); 74 SetValue( *bv ); 75 } 76 /// Return the Value of CSAElement as a ByteValue (if possible) 77 /// \warning: You need to check for NULL return value GetByteValue()78 const ByteValue* GetByteValue() const { 79 // Get the raw pointer from the gdcm::SmartPointer 80 const ByteValue *bv = dynamic_cast<const ByteValue*>(DataField.GetPointer()); 81 return bv; // Will return NULL if not ByteValue 82 } 83 CSAElement(const CSAElement & _val)84 CSAElement(const CSAElement &_val) 85 { 86 if( this != &_val) 87 { 88 *this = _val; 89 } 90 } 91 92 bool operator<(const CSAElement &de) const 93 { 94 return GetKey() < de.GetKey(); 95 } 96 CSAElement &operator=(const CSAElement &de) 97 = default; 98 99 bool operator==(const CSAElement &de) const 100 { 101 return KeyField == de.KeyField 102 && NameField == de.NameField 103 && ValueMultiplicityField == de.ValueMultiplicityField 104 && VRField == de.VRField 105 && SyngoDTField == de.SyngoDTField 106 //&& ValueField == de.ValueField; 107 ; 108 } 109 110 protected: 111 unsigned int KeyField; 112 std::string NameField; 113 VM ValueMultiplicityField; 114 VR VRField; 115 unsigned int SyngoDTField; 116 unsigned int NoOfItemsField; 117 typedef SmartPointer<Value> DataPtr; 118 DataPtr DataField; 119 }; 120 //----------------------------------------------------------------------------- 121 inline std::ostream& operator<<(std::ostream &os, const CSAElement &val) 122 { 123 os << val.KeyField; 124 os << " - '" << val.NameField; 125 os << "' VM " << val.ValueMultiplicityField; 126 os << ", VR " << val.VRField; 127 os << ", SyngoDT " << val.SyngoDTField; 128 os << ", NoOfItems " << val.NoOfItemsField; 129 os << ", Data "; 130 if( val.DataField ) 131 { 132 //val.DataField->Print( os << "'" ); 133 const ByteValue * bv = dynamic_cast<ByteValue*>(&*val.DataField); 134 assert( bv ); 135 const char * p = bv->GetPointer(); 136 std::string str(p, p + bv->GetLength() ); 137 if( val.ValueMultiplicityField == VM::VM1 ) 138 { 139 os << "'" << str.c_str() << "'"; 140 } 141 else 142 { 143 std::istringstream is( str ); 144 std::string s; 145 bool sep = false; 146 while( std::getline(is, s, '\\' ) ) 147 { 148 if( sep ) 149 { 150 os << '\\'; 151 } 152 sep = true; 153 os << "'" << s.c_str() << "'"; 154 } 155 //bv->Print( os << "'" ); 156 //os << "'"; 157 } 158 } 159 return os; 160 } 161 162 } // end namespace gdcm 163 164 #endif //GDCMCSAELEMENT_H 165