DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
StringMemoryArea.cpp
Go to the documentation of this file.
1/*
2 * StringMemoryArea.cxx
3 * OksSystem
4 *
5 * Created by Matthias Wiesmann on 17.02.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#include <sstream>
11#include "ers/ers.hpp"
12
14
17
24 ERS_PRECONDITION(str);
25 const size_t l = strlen(str);
26 const offset_t offset = last_string();
27 const offset_t start = offset+1;
28 const offset_t end = start + l ;
29
31 char *data = string_area_write();
32 char *current = &data[start];
33 strcpy(current,str);
34 last_string(end);
35 return start;
36} // add
37
44 char *data = string_area_write();
45 size_t count = 0;
46 while(data[count+offset]) {
47 data[count+offset] = '\0';
48 count++;
49 } // while
50 return count;
51} // clear_string
52
60 if (offset==0) return 0;
61 ERS_RANGE_CHECK(1, offset, string_area_size());
62 const char* str = string_area_read();
63 if (str == NULL){
64 return 0;
65 }
66 return &(str[offset]);
67} // get_string
68
80 if (offset==0) return add(str);
81 ERS_RANGE_CHECK(1, offset, string_area_size());
82 size_t available = clear(offset);
83 if (available>=strlen(str)) {
84 char *data = string_area_write();
85 char *dest = &data[offset];
86 strcpy(dest,str);
87 return offset;
88 } // if
89 return add(str);
90} // insert_string
91
100 const offset_t n_offset = insert(*offset,str);
101 *offset = n_offset;
102} // insert
103
110
112 const char *str = get_string(offset);
113
114 if(str != 0) {
115
116 const std::string text(str);
117
118 std::string::size_type start_p = 0;
119 std::string::size_type end_p = 0;
120
121 while(start_p < text.size()) {
122 end_p = text.find(STRING_SEPARATOR,start_p);
123 if (end_p == std::string::npos) {
124 end_p = text.length();
125 }
126 std::string extract = text.substr(start_p, end_p - start_p);
127 if(extract.size() > 0) {
128 vector.push_back(extract);
129 }
130 start_p = end_p + 1;
131 } // while
132
133 } // if
134
135 return vector;
136
137} // get_vector
138
146 // ERS_RANGE_CHECK(0, offset, string_area_size());
147 str_vector::const_iterator pos;
148 std::ostringstream stream;
149 for(pos=vect.begin();pos!=vect.end();pos++) {
150 if (vect.begin()!=pos) {
151 stream << STRING_SEPARATOR;
152 } // if first
153 stream << (*pos);
154 } // for
155 return insert(offset,stream.str().c_str());
156} // insert
157
167 const offset_t n_offset = insert(*offset,vect);
168 *offset = n_offset;
169} // insert
170
177
178 ERS_RANGE_CHECK(1, offset, string_area_size());
180 const char* str = get_string(offset);
181
182 if(str != 0) {
183
184 const std::string text(str);
185 std::string::size_type start_p = 0;
186 std::string::size_type end_p = 0;
187
188 while(start_p < text.size()) {
189 end_p = text.find(MAP_ENTRY_SEPARATOR,start_p);
190 if (end_p == std::string::npos) {
191 end_p = text.length();
192 }
193 const std::string entry(text.substr(start_p, end_p - start_p));
194 const std::string::size_type entrySize = entry.size();
195 if(entrySize > 0) {
196 const std::string::size_type separator_p = entry.find(STRING_SEPARATOR);
197 if(separator_p != std::string::npos) {
198 if((separator_p + 1) < entrySize) {
199 map[entry.substr(0, separator_p)] = entry.substr(separator_p + 1);
200 } else {
201 map[entry.substr(0, separator_p)] = "";
202 }
203 }
204 }
205 start_p = end_p + 1;
206 } // while
207
208 } // if
209
210 return map;
211
212} // get_map
213
222 // ERS_RANGE_CHECK(0, offset, string_area_size());
223 str_map::const_iterator pos;
224 std::ostringstream stream;
225 for(pos=map.begin();pos!=map.end();pos++) {
226 if (map.begin()!=pos) {
227 stream << MAP_ENTRY_SEPARATOR;
228 } // if first
229 stream << (pos->first);
230 stream << STRING_SEPARATOR;
231 stream << (pos->second);
232 } // for
233 return insert(offset,stream.str().c_str());
234} // insert
235
245 const offset_t n_offset = insert(*offset,map);
246 *offset = n_offset;
247} // insert
248
249
250
251
252
253
254
255
#define ERS_RANGE_CHECK(min, val, max)
#define ERS_PRECONDITION(expression)
virtual char * string_area_write()=0
write pointer for the string area
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
std::vector< std::string > str_vector
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
std::map< std::string, std::string > str_map
offset_t insert(offset_t offset, const char *str)
str_map get_map(offset_t offset) const
offset_t add(const char *str)
double offset