Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmValEntry.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmValEntry.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/06 14:43:28 $
00007   Version:   $Revision: 1.57 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 
00019 #include "gdcmValEntry.h"
00020 #include "gdcmVR.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmGlobal.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025 
00026 #include <fstream>
00027 
00028 namespace gdcm 
00029 {
00030 //-----------------------------------------------------------------------------
00031 #define MAX_SIZE_PRINT_ELEMENT_VALUE 128
00032 
00033 //-----------------------------------------------------------------------------
00034 // Constructor / Destructor
00039 ValEntry::ValEntry(DictEntry *e) 
00040         : ContentEntry(e)
00041 {
00042 }
00043 
00048 ValEntry::ValEntry(DocEntry *e)
00049         : ContentEntry(e->GetDictEntry())
00050 {
00051    Copy(e);
00052 }
00053 
00057 ValEntry::~ValEntry ()
00058 {
00059 }
00060 
00061 //-----------------------------------------------------------------------------
00062 // Public
00068 void ValEntry::WriteContent(std::ofstream *fp, FileType filetype)
00069 {
00070    DocEntry::WriteContent(fp, filetype);
00071 
00072    if ( GetGroup() == 0xfffe )
00073    {
00074       return; //delimitors have NO value
00075    }
00076 
00077    const VRKey &vr = GetVR();
00078    unsigned int lgth = GetLength();
00079    if (vr == "US" || vr == "SS")
00080    {
00081       // some 'Short integer' fields may be multivaluated
00082       // each single value is separated from the next one by '\'
00083       // we split the string and write each value as a short int
00084       std::vector<std::string> tokens;
00085       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
00086       Util::Tokenize (GetValue(), tokens, "\\");
00087       for (unsigned int i=0; i<tokens.size();i++)
00088       {
00089          uint16_t val_uint16 = atoi(tokens[i].c_str());
00090          binary_write( *fp, val_uint16);
00091       }
00092       tokens.clear();
00093       return;
00094    }
00095    if (vr == "UL" || vr == "SL")
00096    {
00097       // Some 'Integer' fields may be multivaluated (multiple instances 
00098       // of integer). But each single integer value is separated from the
00099       // next one by '\' (backslash character). Hence we split the string
00100       // along the '\' and write each value as an int:
00101       std::vector<std::string> tokens;
00102       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
00103       Util::Tokenize (GetValue(), tokens, "\\");
00104       for (unsigned int i=0; i<tokens.size();i++)
00105       {
00106          uint32_t val_uint32 = atoi(tokens[i].c_str());
00107          binary_write( *fp, val_uint32);
00108       }
00109       tokens.clear();
00110       return;
00111    } 
00112 
00113    gdcmAssertMacro( lgth == GetValue().length() );
00114    binary_write(*fp, GetValue());
00115 } 
00116 
00121 void ValEntry::SetValue(std::string const &val)
00122 {
00123    // Integers have a special treatement for their length:
00124    int l = val.length();
00125    if ( l != 0) // To avoid to be cheated by 'zero length' integers
00126    {   
00127       const VRKey &vr = GetVR();
00128       if( vr == "US" || vr == "SS" )
00129       {
00130          // for multivaluated items
00131          l = (Util::CountSubstring(val, "\\") + 1) * 2;
00132          ContentEntry::SetValue(val);
00133       }
00134       else if( vr == "UL" || vr == "SL" )
00135       {
00136          // for multivaluated items
00137          l = (Util::CountSubstring(val, "\\") + 1) * 4;;
00138          ContentEntry::SetValue(val);
00139       }
00140       else
00141       {
00142          std::string finalVal = Util::DicomString( val.c_str() );
00143          gdcmAssertMacro( !(finalVal.size() % 2) );
00144 
00145          l = finalVal.length();
00146          ContentEntry::SetValue(finalVal);
00147       }
00148    }
00149    else
00150    {
00151       std::string finalVal = Util::DicomString( val.c_str() );
00152       gdcmAssertMacro( !(finalVal.size() % 2) );
00153 
00154       l = finalVal.length();
00155       ContentEntry::SetValue(finalVal);
00156    }
00157 
00158    SetLength(l);
00159 }
00160 
00161 //-----------------------------------------------------------------------------
00162 // Protected
00163 
00164 //-----------------------------------------------------------------------------
00165 // Private
00166 
00167 //-----------------------------------------------------------------------------
00168 // Print
00174 void ValEntry::Print(std::ostream &os, std::string const &)
00175 {
00176    uint16_t g = GetGroup();
00177    uint16_t e = GetElement();
00178    VRKey vr   = GetVR();
00179    std::ostringstream s; 
00180    std::string st;
00181    std::string d2;
00182      
00183    os << "V ";
00184    DocEntry::Print(os); 
00185 
00186    if (g == 0xfffe) // delimiters have NO value
00187    {
00188       // just to avoid identing all the remaining code     
00189       return;
00190    }
00191    
00192    TS *ts = Global::GetTS();
00193     
00194    TSAtr v  = GetValue();  // not applicable for SQ ...     
00195    d2 = Util::CreateCleanString(v);  // replace non printable characters by '.'            
00196    if( GetLength() <= MAX_SIZE_PRINT_ELEMENT_VALUE
00197     || PrintLevel >= 3
00198     || d2.find(GDCM_NOTLOADED) < d2.length() )
00199    {
00200       s << " [" << d2 << "]";
00201    }
00202    else
00203    {
00204       s << " [gdcm::too long for print (" << GetLength() << ") ]";
00205    }
00206    
00207    // Display the UID value (instead of displaying only the rough code)
00208    // First 'clean' trailing character (space or zero) 
00209    if (g == 0x0002)
00210    {
00211       // Any more to be displayed ?
00212       if ( e == 0x0010 || e == 0x0002 )
00213       {
00214          if ( v.length() != 0 )  // for brain damaged headers
00215          {
00216             if ( ! isdigit((unsigned char)v[v.length()-1]) )
00217             {
00218                v.erase(v.length()-1, 1);
00219             }
00220          }
00221          s << "  ==>\t[" << ts->GetValue(v) << "]";
00222       }
00223    }
00224    else
00225    {
00226       if (g == 0x0008)
00227       {
00228          if ( e == 0x0016 || e == 0x1150 )
00229          {
00230             if ( v.length() != 0 )  // for brain damaged headers
00231             {
00232                if ( ! isdigit((unsigned char)v[v.length()-1]) )
00233                {
00234                   v.erase(v.length()-1, 1);
00235                }
00236             }
00237             s << "  ==>\t[" << ts->GetValue(v) << "]";
00238          }
00239       }
00240       else
00241       {
00242          if (g == 0x0004)
00243          {
00244             if ( e == 0x1510 || e == 0x1512  )
00245             {
00246                if ( v.length() != 0 )  // for brain damaged headers  
00247                {
00248                   if ( ! isdigit((unsigned char)v[v.length()-1]) )
00249                   {
00250                      v.erase(v.length()-1, 1);  
00251                   }
00252                }
00253               s << "  ==>\t[" << ts->GetValue(v) << "]";
00254             }
00255          }     
00256       }
00257    }
00258    //if (e == 0x0000) {        // elem 0x0000 --> group length 
00259    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
00260    {
00261       if (v == "4294967295") // to avoid troubles in convertion 
00262       {
00263          st = Util::Format(" x(ffffffff)");
00264       }
00265       else
00266       {
00267          if ( GetLength() != 0 )
00268          {
00269             st = Util::Format(" x(%x)", atoi(v.c_str()));//FIXME
00270          }
00271          else
00272          {
00273             st = Util::Format(" ");
00274          }
00275       }
00276       s << st;
00277    }
00278    os << s.str();
00279 }
00280 
00281 //-----------------------------------------------------------------------------
00282 } // end namespace gdcm
00283 

Generated on Thu Feb 10 22:18:00 2005 for gdcm by doxygen 1.3.6