Update README.md
[deliverable/titan.core.git] / xsdconvert / Mstring.cc
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Godar, Marton
11 * Raduly, Csaba
12 * Szabo, Bence Janos
13 *
14 ******************************************************************************/
970ed795
EL
15#include "../common/dbgnew.hh"
16// dbgnew must come before Mstring.hh which includes common/memory.hh
17#include "Mstring.hh"
18
19#include <string.h>
20#include <ctype.h>
21#include <stdio.h>
22#include <stdlib.h> // only for abort
23
24const Mstring empty_string;
25
26Mstring::Mstring()
3abe9331 27: text(memptystr()) {
28}
970ed795
EL
29
30Mstring::Mstring(const Mstring & other)
3abe9331 31: text(mcopystr(other.text)) {
32}
970ed795
EL
33
34Mstring::Mstring(const char * s)
3abe9331 35: text(mcopystr(s)) {
36}
970ed795
EL
37
38Mstring::Mstring(size_t len, const char *s)
3abe9331 39: text((expstring_t) Malloc(len + 1)) {
970ed795
EL
40 memcpy(text, s, len);
41 text[len] = 0;
42}
43
44Mstring::Mstring(char c)
3abe9331 45: text(memptystr()) {
970ed795
EL
46 text = mputc(text, c);
47}
48
3abe9331 49Mstring::~Mstring() {
970ed795
EL
50 Free(text);
51}
52
3abe9331 53bool Mstring::empty() const {
970ed795
EL
54 return mstrlen(text) == 0;
55}
56
3abe9331 57size_t Mstring::size() const {
970ed795
EL
58 return mstrlen(text);
59}
60
3abe9331 61void Mstring::clear() {
970ed795
EL
62 Free(text);
63 text = memptystr();
64}
65
3abe9331 66const char * Mstring::c_str() const {
970ed795
EL
67 return text;
68}
69
3abe9331 70void Mstring::eraseChar(size_t pos) {
970ed795
EL
71 Mstring temp;
72 for (size_t i = 0; i != size(); ++i)
73 if (i != pos) temp += text[i];
74 Free(text);
75 text = mcopystr(temp.text);
76}
77
3abe9331 78void Mstring::insertChar(size_t pos, char c) {
970ed795
EL
79 Mstring temp;
80 for (size_t i = 0; i != size(); ++i)
81 if (i == pos) {
82 temp += c;
83 temp += text[i];
3abe9331 84 } else temp += text[i];
970ed795
EL
85 Free(text);
86 text = mcopystr(temp.text);
87}
88
3abe9331 89bool Mstring::isFound(const Mstring & s) {
970ed795
EL
90 return strstr(text, s.text);
91}
92
3abe9331 93bool Mstring::isFound(const char * s) {
970ed795
EL
94 return strstr(text, s);
95}
96
3abe9331 97bool Mstring::isFound(char c) {
970ed795
EL
98 return strchr(text, c);
99}
100
3abe9331 101char * Mstring::foundAt(const char * s) {
102 return strstr(text, s);
103}
104
105void Mstring::setCapitalized() {
970ed795
EL
106 text[0] = toupper(text[0]);
107}
108
3abe9331 109void Mstring::setUncapitalized() {
970ed795
EL
110 text[0] = tolower(text[0]);
111}
112
3abe9331 113Mstring Mstring::getPrefix(const char delimiter) const {
970ed795
EL
114 Mstring result;
115 char * pos = strchr(text, delimiter);
116 if (pos != NULL) for (int i = 0; text + i != pos; ++i) result += text[i];
117 return result;
118}
119
3abe9331 120Mstring Mstring::getValueWithoutPrefix(const char delimiter) const {
970ed795 121 char * pos = strrchr(text, delimiter);
3abe9331 122 if (pos != NULL) return Mstring(pos + 1);
970ed795
EL
123 else return *this;
124}
125
3abe9331 126void Mstring::removeWSfromBegin() {
970ed795
EL
127 size_t i = 0;
128 size_t s = mstrlen(text);
3abe9331 129 for (; i < s; ++i)
130 if (!isspace((const unsigned char) text[i])) break;
131 memmove(text, text + i, s - i);
132 text = mtruncstr(text, s - i);
970ed795
EL
133}
134
3abe9331 135void Mstring::removeWSfromEnd() {
970ed795 136 int i = mstrlen(text);
3abe9331 137 for (; i > 0; --i)
138 if (!isspace((const unsigned char) text[i - 1])) break;
970ed795
EL
139 text = mtruncstr(text, i);
140}
141
3abe9331 142char & Mstring::operator[](size_t pos) {
970ed795
EL
143 size_t s = mstrlen(text);
144 if (pos < s)
145 return text[pos];
146 else {
147 fputs("String index overflow\n", stderr);
148 abort();
149 }
150}
151
3abe9331 152const char & Mstring::operator[](size_t pos) const {
970ed795
EL
153 size_t s = mstrlen(text);
154 if (pos < s)
155 return text[pos];
156 else {
157 fputs("String index overflow\n", stderr);
158 abort();
159 }
160}
161
3abe9331 162Mstring & Mstring::operator=(const Mstring & other) {
970ed795
EL
163 if (&other != this) {
164 Free(text);
165 text = mcopystr(other.text);
166 }
167 return *this;
168}
169
3abe9331 170Mstring & Mstring::operator=(const char * s) {
970ed795
EL
171 Free(text);
172 text = mcopystr(s);
173 return *this;
174}
175
3abe9331 176Mstring & Mstring::operator=(char c) {
970ed795
EL
177 Free(text);
178 text = memptystr();
179 text = mputc(text, c);
180 return *this;
181}
182
3abe9331 183Mstring & Mstring::operator+=(const Mstring & other) {
970ed795
EL
184 text = mputstr(text, other.text);
185 return *this;
186}
187
3abe9331 188Mstring & Mstring::operator+=(const char * s) {
970ed795
EL
189 text = mputstr(text, s);
190 return *this;
191}
192
3abe9331 193Mstring & Mstring::operator+=(char c) {
970ed795
EL
194 text = mputc(text, c);
195 return *this;
196}
197
3abe9331 198const Mstring operator+(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
199 return Mstring(lhs) += rhs;
200}
201
3abe9331 202const Mstring operator+(const char * lhs, const Mstring & rhs) {
970ed795
EL
203 return Mstring(lhs) += rhs;
204}
205
3abe9331 206const Mstring operator+(char lhs, const Mstring & rhs) {
970ed795
EL
207 return Mstring(lhs) += rhs;
208}
209
3abe9331 210const Mstring operator+(const Mstring & lhs, const char * rhs) {
970ed795
EL
211 return Mstring(lhs) += rhs;
212}
213
3abe9331 214const Mstring operator+(const Mstring & lhs, char rhs) {
970ed795
EL
215 return Mstring(lhs) += rhs;
216}
217
3abe9331 218bool operator==(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
219 if (strcmp(lhs.text, rhs.text) == 0) // they are equal
220 return true;
221 else
222 return false;
223}
224
3abe9331 225bool operator==(const char * lhs, const Mstring & rhs) {
970ed795
EL
226 if (strcmp(lhs, rhs.text) == 0) // they are equal
227 return true;
228 else
229 return false;
230}
231
3abe9331 232bool operator==(const Mstring & lhs, const char * rhs) {
970ed795
EL
233 if (strcmp(lhs.text, rhs) == 0) // they are equal
234 return true;
235 else
236 return false;
237}
238
3abe9331 239bool operator!=(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
240 if (strcmp(lhs.text, rhs.text) != 0) // they are NOT equal
241 return true;
242 else
243 return false;
244}
245
3abe9331 246bool operator!=(const char * lhs, const Mstring & rhs) {
970ed795
EL
247 if (strcmp(lhs, rhs.text) != 0) // they are NOT equal
248 return true;
249 else
250 return false;
251}
252
3abe9331 253bool operator!=(const Mstring & lhs, const char * rhs) {
970ed795
EL
254 if (strcmp(lhs.text, rhs) != 0) // they are NOT equal
255 return true;
256 else
257 return false;
258}
259
3abe9331 260bool operator<(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
261 if (strcmp(lhs.text, rhs.text) < 0)
262 return true;
263 else
264 return false;
265}
266
3abe9331 267bool operator<(const char * lhs, const Mstring & rhs) {
970ed795
EL
268 if (strcmp(lhs, rhs.text) < 0)
269 return true;
270 else
271 return false;
272}
273
3abe9331 274bool operator<(const Mstring & lhs, const char * rhs) {
970ed795
EL
275 if (strcmp(lhs.text, rhs) < 0)
276 return true;
277 else
278 return false;
279}
280
3abe9331 281bool operator>(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
282 if (strcmp(lhs.text, rhs.text) > 0)
283 return true;
284 else
285 return false;
286}
287
3abe9331 288bool operator>(const char * lhs, const Mstring & rhs) {
970ed795
EL
289 if (strcmp(lhs, rhs.text) > 0)
290 return true;
291 else
292 return false;
293}
294
3abe9331 295bool operator>(const Mstring & lhs, const char * rhs) {
970ed795
EL
296 if (strcmp(lhs.text, rhs) > 0)
297 return true;
298 else
299 return false;
300}
301
3abe9331 302bool operator<=(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
303 if (strcmp(lhs.text, rhs.text) <= 0)
304 return true;
305 else
306 return false;
307}
308
3abe9331 309bool operator<=(const char * lhs, const Mstring & rhs) {
970ed795
EL
310 if (strcmp(lhs, rhs.text) <= 0)
311 return true;
312 else
313 return false;
314}
315
3abe9331 316bool operator<=(const Mstring & lhs, const char * rhs) {
970ed795
EL
317 if (strcmp(lhs.text, rhs) <= 0)
318 return true;
319 else
320 return false;
321}
322
3abe9331 323bool operator>=(const Mstring & lhs, const Mstring & rhs) {
970ed795
EL
324 if (strcmp(lhs.text, rhs.text) >= 0)
325 return true;
326 else
327 return false;
328}
329
3abe9331 330bool operator>=(const char * lhs, const Mstring & rhs) {
970ed795
EL
331 if (strcmp(lhs, rhs.text) >= 0)
332 return true;
333 else
334 return false;
335}
336
3abe9331 337bool operator>=(const Mstring & lhs, const char * rhs) {
970ed795
EL
338 if (strcmp(lhs.text, rhs) >= 0)
339 return true;
340 else
341 return false;
342}
This page took 0.063598 seconds and 5 git commands to generate.