Line data Source code
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 <sstream>
16 : #include "ers/ers.hpp"
17 :
18 : #include "okssystem/StringMemoryArea.hpp"
19 :
20 : const char OksSystem::StringMemoryArea::STRING_SEPARATOR = (char) 3;
21 : const char OksSystem::StringMemoryArea::MAP_ENTRY_SEPARATOR = (char) 4;
22 :
23 : /** Inserts a string at the end of the string region
24 : * \param str the string to insert
25 : * \return the offset in the string region of the string
26 : */
27 :
28 0 : OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::add(const char *str) {
29 0 : ERS_PRECONDITION(str);
30 0 : const size_t l = strlen(str);
31 0 : const offset_t offset = last_string();
32 0 : const offset_t start = offset+1;
33 0 : const offset_t end = start + l ;
34 :
35 0 : ERS_RANGE_CHECK(1, end, string_area_size());
36 0 : char *data = string_area_write();
37 0 : char *current = &data[start];
38 0 : strcpy(current,str);
39 0 : last_string(end);
40 0 : return start;
41 : } // add
42 :
43 : /** Clears a string in the string region
44 : * \param offset the offset of the start of the string
45 : * \return the lenght of the cleared string
46 : */
47 :
48 0 : size_t OksSystem::StringMemoryArea::clear(offset_t offset) {
49 0 : char *data = string_area_write();
50 0 : size_t count = 0;
51 0 : while(data[count+offset]) {
52 0 : data[count+offset] = '\0';
53 0 : count++;
54 : } // while
55 0 : return count;
56 : } // clear_string
57 :
58 : /** Finds a string in the string area
59 : * \param offset the starting offset of the string
60 : * \return pointer to string
61 : * \note the actual characters are in the shared area
62 : */
63 :
64 0 : const char* OksSystem::StringMemoryArea::get_string(offset_t offset) const {
65 0 : if (offset==0) return 0;
66 0 : ERS_RANGE_CHECK(1, offset, string_area_size());
67 0 : const char* str = string_area_read();
68 0 : if (str == NULL){
69 : return 0;
70 : }
71 0 : return &(str[offset]);
72 : } // get_string
73 :
74 : /** Inserts a string in the string area.
75 : * \param offset offset of the existing string to replace - 0 means no offset defined
76 : * \param str the string to insert
77 : * \return the actual offset where the string was inserted
78 : * \note An existing string is replaced only if the next string can fit in the space.
79 : * if not, the old string is zeroed and the new string inserted at the end of the area.
80 : * This can lead to fragmentation, so you should avoid replacing strings.
81 : */
82 :
83 0 : OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert(offset_t offset, const char* str) {
84 0 : ERS_PRECONDITION(str);
85 0 : if (offset==0) return add(str);
86 0 : ERS_RANGE_CHECK(1, offset, string_area_size());
87 0 : size_t available = clear(offset);
88 0 : if (available>=strlen(str)) {
89 0 : char *data = string_area_write();
90 0 : char *dest = &data[offset];
91 0 : strcpy(dest,str);
92 0 : return offset;
93 : } // if
94 0 : return add(str);
95 : } // insert_string
96 :
97 : /** Inserts a string into memory
98 : * This version updates the memory reference
99 : * \param address of the offset of the string
100 : * \param str the string to insert
101 : */
102 :
103 0 : void OksSystem::StringMemoryArea::insert(offset_t *offset, const char* str) {
104 0 : ERS_PRECONDITION(offset);
105 0 : const offset_t n_offset = insert(*offset,str);
106 0 : *offset = n_offset;
107 0 : } // insert
108 :
109 : /** Extracts a vector of string from memory
110 : * \param offset offset of the vector
111 : * \return a vector of strings
112 : */
113 :
114 0 : OksSystem::StringMemoryArea::str_vector OksSystem::StringMemoryArea::get_vector(offset_t offset) const {
115 :
116 0 : OksSystem::StringMemoryArea::str_vector vector;
117 0 : const char *str = get_string(offset);
118 :
119 0 : if(str != 0) {
120 :
121 0 : const std::string text(str);
122 :
123 0 : std::string::size_type start_p = 0;
124 0 : std::string::size_type end_p = 0;
125 :
126 0 : while(start_p < text.size()) {
127 0 : end_p = text.find(STRING_SEPARATOR,start_p);
128 0 : if (end_p == std::string::npos) {
129 0 : end_p = text.length();
130 : }
131 0 : std::string extract = text.substr(start_p, end_p - start_p);
132 0 : if(extract.size() > 0) {
133 0 : vector.push_back(extract);
134 : }
135 0 : start_p = end_p + 1;
136 0 : } // while
137 :
138 0 : } // if
139 :
140 0 : return vector;
141 :
142 0 : } // get_vector
143 :
144 : /** Inserts a vector in the string area
145 : * \param offset the offset of the string
146 : * \param vect the vector to insert
147 : * \note the strings should not contain the caracter STRING_SEPARATOR (03)
148 : */
149 :
150 0 : OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert(offset_t offset, const str_vector &vect) {
151 : // ERS_RANGE_CHECK(0, offset, string_area_size());
152 0 : str_vector::const_iterator pos;
153 0 : std::ostringstream stream;
154 0 : for(pos=vect.begin();pos!=vect.end();pos++) {
155 0 : if (vect.begin()!=pos) {
156 0 : stream << STRING_SEPARATOR;
157 : } // if first
158 0 : stream << (*pos);
159 : } // for
160 0 : return insert(offset,stream.str().c_str());
161 0 : } // insert
162 :
163 : /** Inserts a vector into memory
164 : * This version updates the memory reference
165 : * \param address of the offset of the string
166 : * \param str the string to insert
167 : * \note the strings should not contain the caracter STRING_SEPARATOR (03)
168 : */
169 :
170 0 : void OksSystem::StringMemoryArea::insert(offset_t *offset, const str_vector &vect) {
171 0 : ERS_PRECONDITION(offset);
172 0 : const offset_t n_offset = insert(*offset,vect);
173 0 : *offset = n_offset;
174 0 : } // insert
175 :
176 : /** Reads a string map from memory
177 : * \param offset offset of the map in memory
178 : * \return a map of strings
179 : */
180 :
181 0 : OksSystem::StringMemoryArea::str_map OksSystem::StringMemoryArea::get_map(offset_t offset) const {
182 :
183 0 : ERS_RANGE_CHECK(1, offset, string_area_size());
184 0 : OksSystem::StringMemoryArea::str_map map;
185 0 : const char* str = get_string(offset);
186 :
187 0 : if(str != 0) {
188 :
189 0 : const std::string text(str);
190 0 : std::string::size_type start_p = 0;
191 0 : std::string::size_type end_p = 0;
192 :
193 0 : while(start_p < text.size()) {
194 0 : end_p = text.find(MAP_ENTRY_SEPARATOR,start_p);
195 0 : if (end_p == std::string::npos) {
196 0 : end_p = text.length();
197 : }
198 0 : const std::string entry(text.substr(start_p, end_p - start_p));
199 0 : const std::string::size_type entrySize = entry.size();
200 0 : if(entrySize > 0) {
201 0 : const std::string::size_type separator_p = entry.find(STRING_SEPARATOR);
202 0 : if(separator_p != std::string::npos) {
203 0 : if((separator_p + 1) < entrySize) {
204 0 : map[entry.substr(0, separator_p)] = entry.substr(separator_p + 1);
205 : } else {
206 0 : map[entry.substr(0, separator_p)] = "";
207 : }
208 : }
209 : }
210 0 : start_p = end_p + 1;
211 0 : } // while
212 :
213 0 : } // if
214 :
215 0 : return map;
216 :
217 0 : } // get_map
218 :
219 : /** Inserts a map into memory
220 : * \param offset current offset of the map
221 : * \param map the map to insert
222 : * \return the new offset of the map
223 : * \note the strings should not contain the caracter STRING_SEPARATOR (03)
224 : */
225 :
226 0 : OksSystem::StringMemoryArea::offset_t OksSystem::StringMemoryArea::insert(offset_t offset, const str_map &map) {
227 : // ERS_RANGE_CHECK(0, offset, string_area_size());
228 0 : str_map::const_iterator pos;
229 0 : std::ostringstream stream;
230 0 : for(pos=map.begin();pos!=map.end();pos++) {
231 0 : if (map.begin()!=pos) {
232 0 : stream << MAP_ENTRY_SEPARATOR;
233 : } // if first
234 0 : stream << (pos->first);
235 0 : stream << STRING_SEPARATOR;
236 0 : stream << (pos->second);
237 : } // for
238 0 : return insert(offset,stream.str().c_str());
239 0 : } // insert
240 :
241 : /** Inserts a vector into memory
242 : * This version updates the memory reference
243 : * \param address of the offset of the string
244 : * \param str the string to insert
245 : * \note the strings should not contain the caracter STRING_SEPARATOR (03)
246 : */
247 :
248 0 : void OksSystem::StringMemoryArea::insert(offset_t *offset, const str_map &map) {
249 0 : ERS_PRECONDITION(offset);
250 0 : const offset_t n_offset = insert(*offset,map);
251 0 : *offset = n_offset;
252 0 : } // insert
253 :
254 :
255 :
256 :
257 :
258 :
259 :
260 :
|