comparison Resources/Orthanc/Plugins/Samples/Common/DicomDatasetReader.cpp @ 32:517c46f527cd

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 19 Dec 2016 11:00:23 +0100
parents 9aace933cb64
children 7207a407bcd8
comparison
equal deleted inserted replaced
31:9aace933cb64 32:517c46f527cd
66 assert(first <= last); 66 assert(first <= last);
67 return source.substr(first, last - first); 67 return source.substr(first, last - first);
68 } 68 }
69 69
70 70
71 DicomDatasetReader::DicomDatasetReader(IDicomDataset* dataset) : // takes ownership 71 DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) :
72 dataset_(dataset) 72 dataset_(dataset)
73 { 73 {
74 if (dataset == NULL) 74 }
75
76
77 std::string DicomDatasetReader::GetStringValue(const DicomPath& path,
78 const std::string& defaultValue) const
79 {
80 std::string s;
81 if (dataset_.GetStringValue(s, path))
75 { 82 {
76 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 83 return s;
84 }
85 else
86 {
87 return defaultValue;
77 } 88 }
78 } 89 }
79 90
80 91
81 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const 92 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const
82 { 93 {
83 std::string s; 94 std::string s;
84 if (dataset_->GetStringValue(s, path)) 95 if (dataset_.GetStringValue(s, path))
85 { 96 {
86 return s; 97 return s;
87 } 98 }
88 else 99 else
89 { 100 {
90 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag); 101 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag);
91 } 102 }
92 } 103 }
93 104
94 105
95 int DicomDatasetReader::GetIntegerValue(const DicomPath& path) 106 template <typename T>
107 static bool GetValueInternal(T& target,
108 const IDicomDataset& dataset,
109 const DicomPath& path)
96 { 110 {
97 try 111 try
98 { 112 {
99 std::string s = StripSpaces(GetMandatoryStringValue(path)); 113 std::string s;
100 return boost::lexical_cast<int>(s); 114
115 if (dataset.GetStringValue(s, path))
116 {
117 target = boost::lexical_cast<T>(StripSpaces(s));
118 return true;
119 }
120 else
121 {
122 return false;
123 }
101 } 124 }
102 catch (boost::bad_lexical_cast&) 125 catch (boost::bad_lexical_cast&)
103 { 126 {
104 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 127 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
105 } 128 }
106 } 129 }
107 130
108 131
109 unsigned int DicomDatasetReader::GetUnsignedIntegerValue(const DicomPath& path) 132 bool DicomDatasetReader::GetIntegerValue(int& target,
133 const DicomPath& path) const
110 { 134 {
111 int value = GetIntegerValue(path); 135 return GetValueInternal<int>(target, dataset_, path);
136 }
112 137
113 if (value >= 0) 138
139 bool DicomDatasetReader::GetUnsignedIntegerValue(unsigned int& target,
140 const DicomPath& path) const
141 {
142 int value;
143
144 if (!GetIntegerValue(value, path))
114 { 145 {
115 return static_cast<unsigned int>(value); 146 return false;
147 }
148 else if (value >= 0)
149 {
150 target = static_cast<unsigned int>(value);
151 return true;
116 } 152 }
117 else 153 else
118 { 154 {
119 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 155 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
120 } 156 }
121 } 157 }
158
159
160 bool DicomDatasetReader::GetFloatValue(float& target,
161 const DicomPath& path) const
162 {
163 return GetValueInternal<float>(target, dataset_, path);
164 }
165
166
167 bool DicomDatasetReader::GetDoubleValue(double& target,
168 const DicomPath& path) const
169 {
170 return GetValueInternal<double>(target, dataset_, path);
171 }
122 } 172 }