vega
padded_string_manipulator_impl.h
Go to the documentation of this file.
3 
4 #include <sstream>
5 
6 namespace vega {
7  namespace manipulators {
8  template <typename T>
10  : PaddedStringManipulator::base_vector()
11  {}
12 
13  template <typename T>
14  PaddedStringManipulator<T>::PaddedStringManipulator(std::shared_ptr<dicom::RawValue> raw_value)
15  : PaddedStringManipulator(raw_value->str())
16  {}
17 
18  template <typename T>
20  : PaddedStringManipulator::base_vector()
21  {
22  this->parse_from_string(s);
23  }
24 
25  template <typename T>
26  std::shared_ptr<dicom::RawValue> PaddedStringManipulator<T>::raw_value() {
27  return std::make_shared<dicom::RawValue>(this->str());
28  }
29 
30  template <typename T>
31  std::string PaddedStringManipulator<T>::str() const {
32  std::stringstream ss;
33  size_t bytes = 0;
34 
35  for (size_t i = 0; i < this->size(); ++i) {
36  auto s = vega::to_string<T>((*this)[i]);
37  ss << s;
38 
39  bytes += s.size();
40  if (i < this->size()-1) {
41  ss << '\\';
42  bytes += 1;
43  }
44  }
45 
46  if (bytes & 1) {
47  ss << ' ';
48  bytes += 1;
49  }
50 
51  return ss.str();
52  }
53 
54  template <typename T>
55  bool PaddedStringManipulator<T>::read_from(dicom::RawReader* reader, size_t num_bytes) {
56  if (num_bytes == 0) {
57  this->clear();
58  return true;
59  }
60 
61  std::stringstream ss;
62  char c;
63 
64  size_t delimiters = 0;
65 
66  for (size_t i = 0; i < num_bytes; ++i) {
67  if (!reader->read_into(&c)) return false;
68  if (c == '\\') ++delimiters;
69  ss << c;
70  }
71 
72  this->clear();
73  this->reserve(delimiters+1);
74 
75  this->parse_from_string(ss.str());
76  return true;
77  }
78 
79  template <typename T>
80  size_t PaddedStringManipulator<T>::write_to(dicom::RawWriter* writer) const {
81  std::string s = this->str();
82  return writer->write_from(s.begin(), s.end());
83  }
84 
85  template <typename T>
87  const PaddedStringManipulator<T>* other_ptr = dynamic_cast<const PaddedStringManipulator<T>*>(&other);
88  if (!other_ptr) return false;
89 
90  return this->str() == other_ptr->str();
91  }
92 
93  template <typename T>
95  return !(*this == other);
96  }
97 
98  template <typename T>
99  void PaddedStringManipulator<T>::parse_from_string(const std::string& s) {
100  std::istringstream ss(s);
101  std::string element_string;
102 
103  this->clear();
104  while(std::getline(ss, element_string, '\\')) {
105  // Trim padding if present
106  if (element_string.back() == ' ') {
107  element_string = element_string.substr(0, element_string.size()-1);
108  }
109  this->push_back(vega::from_string<T>(element_string));
110  }
111  }
112  }
113 }
PaddedStringManipulator()
Definition: padded_string_manipulator_impl.h:9
Definition: value_manipulator.h:29
Definition: age.h:6
A manipulator class for VR that represent elements which are encoded in DICOM using strings...
Definition: padded_string_manipulator.h:89
std::string str() const override
Definition: padded_string_manipulator_impl.h:31