annotate Core/DicomFormat/DicomValue.cpp @ 2954:d924f9bb61cc

taking advantage of details in OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Dec 2018 14:35:34 +0100
parents da12ba232119
children 4e43e67f8ecf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1761
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
2447
878b59270859 upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2412
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../PrecompiledHeaders.h"
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "DicomValue.h"
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "../OrthancException.h"
2863
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
38 #include "../SerializationToolbox.h"
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include "../Toolbox.h"
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
2410
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
41 #include <boost/lexical_cast.hpp>
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
42
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 namespace Orthanc
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 DicomValue::DicomValue(const DicomValue& other) :
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 type_(other.type_),
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 content_(other.content_)
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 DicomValue::DicomValue(const std::string& content,
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 bool isBinary) :
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 type_(isBinary ? Type_Binary : Type_String),
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 content_(content)
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 DicomValue::DicomValue(const char* data,
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 size_t size,
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 bool isBinary) :
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 type_(isBinary ? Type_Binary : Type_String)
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 content_.assign(data, size);
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const std::string& DicomValue::GetContent() const
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (type_ == Type_Null)
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 throw OrthancException(ErrorCode_BadParameterType);
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 else
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 return content_;
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 DicomValue* DicomValue::Clone() const
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 return new DicomValue(*this);
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
2139
764c9157301b cleaning
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
88 #if ORTHANC_ENABLE_BASE64 == 1
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 void DicomValue::FormatDataUriScheme(std::string& target,
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 const std::string& mime) const
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 Toolbox::EncodeBase64(target, GetContent());
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 target.insert(0, "data:" + mime + ";base64,");
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 }
1761
f4286d99ee0a fix sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1738
diff changeset
95 #endif
f4286d99ee0a fix sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1738
diff changeset
96
2410
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
97
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
98 template <typename T,
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
99 bool allowSigned>
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
100 static bool ParseValue(T& result,
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
101 const DicomValue& source)
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
102 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
103 if (source.IsBinary() ||
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
104 source.IsNull())
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
105 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
106 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
107 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
108
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
109 try
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
110 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
111 std::string value = Toolbox::StripSpaces(source.GetContent());
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
112 if (value.empty())
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
113 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
114 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
115 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
116
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
117 if (!allowSigned &&
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
118 value[0] == '-')
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
119 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
120 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
121 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
122
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
123 result = boost::lexical_cast<T>(value);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
124 return true;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
125 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
126 catch (boost::bad_lexical_cast&)
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
127 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
128 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
129 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
130 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
131
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
132 bool DicomValue::ParseInteger32(int32_t& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
133 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
134 int64_t tmp;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
135 if (ParseValue<int64_t, true>(tmp, *this))
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
136 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
137 result = static_cast<int32_t>(tmp);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
138 return (tmp == static_cast<int64_t>(result)); // Check no overflow occurs
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
139 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
140 else
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
141 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
142 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
143 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
144 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
145
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
146 bool DicomValue::ParseInteger64(int64_t& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
147 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
148 return ParseValue<int64_t, true>(result, *this);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
149 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
150
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
151 bool DicomValue::ParseUnsignedInteger32(uint32_t& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
152 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
153 uint64_t tmp;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
154 if (ParseValue<uint64_t, false>(tmp, *this))
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
155 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
156 result = static_cast<uint32_t>(tmp);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
157 return (tmp == static_cast<uint64_t>(result)); // Check no overflow occurs
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
158 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
159 else
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
160 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
161 return false;
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
162 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
163 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
164
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
165 bool DicomValue::ParseUnsignedInteger64(uint64_t& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
166 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
167 return ParseValue<uint64_t, false>(result, *this);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
168 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
169
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
170 bool DicomValue::ParseFloat(float& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
171 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
172 return ParseValue<float, true>(result, *this);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
173 }
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
174
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
175 bool DicomValue::ParseDouble(double& result) const
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
176 {
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
177 return ParseValue<double, true>(result, *this);
3590c936e56f parsing numbers in DicomValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
178 }
2412
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
179
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
180 bool DicomValue::CopyToString(std::string& result,
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
181 bool allowBinary) const
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
182 {
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
183 if (IsNull())
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
184 {
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
185 return false;
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
186 }
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
187 else if (IsBinary() && !allowBinary)
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
188 {
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
189 return false;
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
190 }
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
191 else
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
192 {
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
193 result.assign(content_);
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
194 return true;
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
195 }
cad393b41bc3 handy shortcuts in DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2410
diff changeset
196 }
2863
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
197
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
198
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
199 static const char* KEY_TYPE = "Type";
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
200 static const char* KEY_CONTENT = "Content";
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
201
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
202 void DicomValue::Serialize(Json::Value& target) const
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
203 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
204 target = Json::objectValue;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
205
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
206 switch (type_)
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
207 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
208 case Type_Null:
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
209 target[KEY_TYPE] = "Null";
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
210 break;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
211
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
212 case Type_String:
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
213 target[KEY_TYPE] = "String";
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
214 target[KEY_CONTENT] = content_;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
215 break;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
216
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
217 case Type_Binary:
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
218 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
219 target[KEY_TYPE] = "Binary";
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
220
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
221 std::string base64;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
222 Toolbox::EncodeBase64(base64, content_);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
223 target[KEY_CONTENT] = base64;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
224 break;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
225 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
226
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
227 default:
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
228 throw OrthancException(ErrorCode_InternalError);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
229 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
230 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
231
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
232 void DicomValue::Unserialize(const Json::Value& source)
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
233 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
234 std::string type = SerializationToolbox::ReadString(source, KEY_TYPE);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
235
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
236 if (type == "Null")
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
237 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
238 type_ = Type_Null;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
239 content_.clear();
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
240 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
241 else if (type == "String")
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
242 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
243 type_ = Type_String;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
244 content_ = SerializationToolbox::ReadString(source, KEY_CONTENT);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
245 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
246 else if (type == "Binary")
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
247 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
248 type_ = Type_Binary;
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
249
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
250 const std::string base64 =SerializationToolbox::ReadString(source, KEY_CONTENT);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
251 Toolbox::DecodeBase64(content_, base64);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
252 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
253 else
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
254 {
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
255 throw OrthancException(ErrorCode_BadFileFormat);
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
256 }
da12ba232119 serialization of DicomMap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
257 }
1738
15a788a63846 DicomToJsonFlags_IncludeBinary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }