DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
StringMemoryArea.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3// Fork baseline commit: system-00-00-20 (2020-09-25).
4// Renamed since fork: yes (from src/StringMemoryArea.cxx to src/StringMemoryArea.cpp).
5
6/*
7 * StringMemoryArea.cxx
8 * OksSystem
9 *
10 * Created by Matthias Wiesmann on 17.02.05.
11 * Copyright 2005 CERN. All rights reserved.
12 *
13 */
14
15#include "ers/ers.hpp"
16#include <sstream>
17
19
22
27
30{
32 const size_t l = strlen(str);
33 const offset_t offset = last_string();
34 const offset_t start = offset + 1;
35 const offset_t end = start + l;
36
38 char* data = string_area_write();
39 char* current = &data[start];
40 strcpy(current, str);
41 last_string(end);
42 return start;
43} // add
44
49
50size_t
52{
53 char* data = string_area_write();
54 size_t count = 0;
55 while (data[count + offset]) {
56 data[count + offset] = '\0';
57 count++;
58 } // while
59 return count;
60} // clear_string
61
67
68const char*
70{
71 if (offset == 0)
72 return 0;
74 const char* str = string_area_read();
75 if (str == NULL) {
76 return 0;
77 }
78 return &(str[offset]);
79} // get_string
80
89
92{
94 if (offset == 0)
95 return add(str);
97 size_t available = clear(offset);
98 if (available >= strlen(str)) {
99 char* data = string_area_write();
100 char* dest = &data[offset];
101 strcpy(dest, str);
102 return offset;
103 } // if
104 return add(str);
105} // insert_string
106
112
113void
115{
117 const offset_t n_offset = insert(*offset, str);
118 *offset = n_offset;
119} // insert
120
125
128{
129
131 const char* str = get_string(offset);
132
133 if (str != 0) {
134
135 const std::string text(str);
136
137 std::string::size_type start_p = 0;
138 std::string::size_type end_p = 0;
139
140 while (start_p < text.size()) {
141 end_p = text.find(STRING_SEPARATOR, start_p);
142 if (end_p == std::string::npos) {
143 end_p = text.length();
144 }
145 std::string extract = text.substr(start_p, end_p - start_p);
146 if (extract.size() > 0) {
147 vector.push_back(extract);
148 }
149 start_p = end_p + 1;
150 } // while
151
152 } // if
153
154 return vector;
155
156} // get_vector
157
163
166{
167 // ERS_RANGE_CHECK(0, offset, string_area_size());
168 str_vector::const_iterator pos;
169 std::ostringstream stream;
170 for (pos = vect.begin(); pos != vect.end(); pos++) {
171 if (vect.begin() != pos) {
172 stream << STRING_SEPARATOR;
173 } // if first
174 stream << (*pos);
175 } // for
176 return insert(offset, stream.str().c_str());
177} // insert
178
185
186void
188{
190 const offset_t n_offset = insert(*offset, vect);
191 *offset = n_offset;
192} // insert
193
198
201{
202
205 const char* str = get_string(offset);
206
207 if (str != 0) {
208
209 const std::string text(str);
210 std::string::size_type start_p = 0;
211 std::string::size_type end_p = 0;
212
213 while (start_p < text.size()) {
214 end_p = text.find(MAP_ENTRY_SEPARATOR, start_p);
215 if (end_p == std::string::npos) {
216 end_p = text.length();
217 }
218 const std::string entry(text.substr(start_p, end_p - start_p));
219 const std::string::size_type entrySize = entry.size();
220 if (entrySize > 0) {
221 const std::string::size_type separator_p = entry.find(STRING_SEPARATOR);
222 if (separator_p != std::string::npos) {
223 if ((separator_p + 1) < entrySize) {
224 map[entry.substr(0, separator_p)] = entry.substr(separator_p + 1);
225 } else {
226 map[entry.substr(0, separator_p)] = "";
227 }
228 }
229 }
230 start_p = end_p + 1;
231 } // while
232
233 } // if
234
235 return map;
236
237} // get_map
238
245
248{
249 // ERS_RANGE_CHECK(0, offset, string_area_size());
250 str_map::const_iterator pos;
251 std::ostringstream stream;
252 for (pos = map.begin(); pos != map.end(); pos++) {
253 if (map.begin() != pos) {
254 stream << MAP_ENTRY_SEPARATOR;
255 } // if first
256 stream << (pos->first);
257 stream << STRING_SEPARATOR;
258 stream << (pos->second);
259 } // for
260 return insert(offset, stream.str().c_str());
261} // insert
262
269
270void
272{
274 const offset_t n_offset = insert(*offset, map);
275 *offset = n_offset;
276} // insert
#define ERS_RANGE_CHECK(min, val, max)
#define ERS_PRECONDITION(expression)
virtual char * string_area_write()=0
write pointer for the string area
std::map< std::string, std::string > str_map
virtual size_t string_area_size() const =0
size of the string area
size_t clear(offset_t offset)
const char * get_string(offset_t offset) const
virtual offset_t last_string() const =0
offset of the end of the last string
static const char STRING_SEPARATOR
character used to separate strings (i.e., to tokenize strings in vectors)
static const char MAP_ENTRY_SEPARATOR
character used to separate map entries
str_vector get_vector(offset_t offset) const
offset_t insert(offset_t offset, const char *str)
str_map get_map(offset_t offset) const
offset_t add(const char *str)
std::vector< std::string > str_vector
virtual const char * string_area_read() const =0
read pointer for the string area
double offset