]> Creatis software - gdcm.git/blob - src/gdcmContentEntry.cxx
84794b41d6988a311452bcea9e3e04e496d161fb
[gdcm.git] / src / gdcmContentEntry.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmContentEntry.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/26 10:29:16 $
7   Version:   $Revision: 1.4 $
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 #include "gdcmContentEntry.h"
20 #include "gdcmVR.h"
21 #include "gdcmTS.h"
22 #include "gdcmGlobal.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <fstream>
27
28 namespace gdcm 
29 {
30
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
33 /**
34  * \brief   Constructor for a given DictEntry
35  * @param   e Pointer to existing dictionary entry
36  */
37 ContentEntry::ContentEntry(DictEntry *e) : DocEntry(e)
38 {
39    Value = GDCM_UNFOUND;
40 }
41
42 /**
43  * \brief   Constructor for a given DocEntry
44  * @param   e Pointer to existing Doc entry
45  */
46 ContentEntry::ContentEntry(DocEntry *e)
47              : DocEntry(e->GetDictEntry())
48 {
49    Copy(e);
50 }
51
52 /**
53  * \brief   Canonical destructor.
54  */
55 ContentEntry::~ContentEntry ()
56 {
57 }
58
59 //-----------------------------------------------------------------------------
60 // Public
61
62 /**
63  * \brief   Writes the 'std::string representable' value of a ContentEntry
64  * @param fp already open ofstream pointer
65  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
66  */
67 void ContentEntry::WriteContent(std::ofstream *fp, FileType filetype)
68 {
69    DocEntry::WriteContent(fp, filetype);
70
71    if ( GetGroup() == 0xfffe )
72    {
73       return; //delimitors have NO value
74    }
75
76    const VRKey &vr = GetVR();
77    unsigned int lgr = GetLength();
78    if (vr == "US" || vr == "SS")
79    {
80       // some 'Short integer' fields may be multivaluated
81       // each single value is separated from the next one by '\'
82       // we split the string and write each value as a short int
83       std::vector<std::string> tokens;
84       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
85       Util::Tokenize (GetValue(), tokens, "\\");
86       for (unsigned int i=0; i<tokens.size();i++)
87       {
88          uint16_t val_uint16 = atoi(tokens[i].c_str());
89          binary_write( *fp, val_uint16);
90       }
91       tokens.clear();
92       return;
93    }
94    if (vr == "UL" || vr == "SL")
95    {
96       // Some 'Integer' fields may be multivaluated (multiple instances 
97       // of integer). But each single integer value is separated from the
98       // next one by '\' (backslash character). Hence we split the string
99       // along the '\' and write each value as an int:
100       std::vector<std::string> tokens;
101       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
102       Util::Tokenize (GetValue(), tokens, "\\");
103       for (unsigned int i=0; i<tokens.size();i++)
104       {
105          uint32_t val_uint32 = atoi(tokens[i].c_str());
106          binary_write( *fp, val_uint32);
107       }
108       tokens.clear();
109       return;
110    } 
111
112    gdcmAssertMacro( lgr == GetValue().length() );
113    binary_write(*fp, GetValue());
114
115
116 //-----------------------------------------------------------------------------
117 // Protected
118
119 //-----------------------------------------------------------------------------
120 // Private
121
122 //-----------------------------------------------------------------------------
123 } // end namespace gdcm
124