[ucsim] Update email and file info, fix stm8 flash controller
[sdcc.git] / sdcc / sim / ucsim / src / core / utils.src / chars.cc
blob5f528246be7787b8b166e6cf8921bc5aece0218f
1 /*
2 * Simulator of microcontrollers (chars.cc)
4 * Copyright (C) 1997 Drotos Daniel
5 *
6 * To contact author send email to dr.dkdb@gmail.com
8 */
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26 /*@1@*/
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <ctype.h>
35 #include "charscl.h"
38 chars::chars(void)
40 chars_string= 0;
41 chars_length= 0;
42 dynamic= false;
43 pars_pos= 0;
46 chars::chars(char *s)
48 chars_string= 0;
49 chars_length= 0;
50 allocate_string(s);
53 chars::chars(const char *s)
55 if ((chars_string= const_cast<char *>(s)) != NULL)
56 chars_length= strlen(s);
57 else
58 chars_length= 0;
59 dynamic= false;
60 pars_pos= 0;
62 chars::chars(const chars &cs)
64 chars_string= 0;
65 chars_length= 0;
66 allocate_string(cs);
69 chars::chars(const char *, const char *fmt, ...)
71 va_list ap;
72 char n[1000];
74 va_start(ap, fmt);
75 vsnprintf(n, 999, fmt, ap);
76 va_end(ap);
78 chars_string= strdup(n);
79 chars_length= strlen(n);
80 dynamic= true;
81 pars_pos= 0;
84 chars::~chars(void)
86 deallocate_string();
90 void
91 chars::allocate_string(const char *s)
93 char *n= NULL;
94 int l= 0;
95 bool d= false;
97 if (s)
99 l= strlen(s);
100 n= (char*)malloc(l+1);
101 strcpy(n, s);
102 d= true;
104 deallocate_string();
105 chars_length= l;
106 chars_string= n;
107 dynamic= d;
108 pars_pos= 0;
111 void
112 chars::deallocate_string(void)
114 if (chars_string)
115 if (dynamic)
116 free(chars_string);
117 chars_string= 0;
118 chars_length= 0;
119 dynamic= 0;
123 char
124 chars::c(int idx)
126 if (!chars_string)
127 return 0;
128 if (idx>=chars_length)
129 return 0;
130 return chars_string[idx];
133 chars
134 chars::token(const char *delims) const
136 chars c= (char*)NULL;
138 if (!delims || !*delims)
139 return c;
140 if (pars_pos >= chars_length)
141 return c;
142 if (chars_length < 1)
143 return c;
145 int l;
146 // skip initial delims first;
147 l= strspn(&chars_string[pars_pos], delims);
148 pars_pos+= l;
149 if (pars_pos >= chars_length)
150 return c;
151 // skip chars not in delims: search token end
152 l= strcspn(&chars_string[pars_pos], delims);
153 if (l > 0)
155 // found
156 int i;
157 for (i= pars_pos; i < pars_pos+l; i++)
158 c+= chars_string[i];
159 pars_pos= i;
160 // skip delims at end
161 l= strspn(&chars_string[pars_pos], delims);
162 pars_pos+= l;
163 return c;
165 // not found more
166 return c;
169 unsigned int
170 chars::htoi(void)
172 unsigned int v= 0;
173 int i, x;
174 if (!chars_string)
175 return 0;
176 for (i= 0; chars_string[i]; i++)
178 char c= toupper(chars_string[i]);
179 if ((c>='0') && (c<='9'))
180 x= c-'0';
181 else if ((c>='A') && (c<='F'))
182 x= 10+c-'A';
183 else
184 x= 0;
185 v<<= 4;
186 v|= x;
188 return v;
191 unsigned long long int
192 chars::htoll(void)
194 unsigned long long int v= 0;
195 int i, x;
196 if (!chars_string)
197 return 0;
198 for (i= 0; chars_string[i]; i++)
200 char c= toupper(chars_string[i]);
201 if ((c>='0') && (c<='9'))
202 x= c-'0';
203 else if ((c>='A') && (c<='F'))
204 x= 10+c-'A';
205 else
206 return v;//x= 0;
207 v<<= 4;
208 v|= x;
210 return v;
214 void
215 chars::ltrim(void)
217 char *p= chars_string;
218 if (empty())
219 return;
220 while (*p && isspace(*p))
221 p++;
222 allocate_string(p);
225 void
226 chars::rtrim(void)
228 int i;
229 if (empty())
230 return;
231 i= chars_length-1;
232 while (i>=0)
234 if (isspace(chars_string[i]))
235 chars_string[i]= 0;
236 else
237 break;
238 i--;
242 void
243 chars::lrip(const char *cset)
245 int skip;
246 if (empty())
247 return;
248 if (!cset || !*cset)
249 return;
250 skip= strspn(chars_string, cset);
251 if (skip > 0)
252 allocate_string(chars_string+skip);
255 void
256 chars::rrip(const char *cset)
258 if (empty())
259 return;
260 if (!cset || !*cset)
261 return;
262 int i= chars_length-1;
263 while (i>=0)
265 char c= chars_string[i];
266 if (strchr(cset, c) != NULL)
267 chars_string[i]= 0;
268 else
269 break;
270 i--;
274 void
275 chars::rrip(int nuof_chars)
277 if (empty()) return;
278 if (nuof_chars < 1) return;
279 int i= chars_length-1;
280 while ((i>=0) && nuof_chars)
282 chars_string[i]= 0;
283 i--;
284 nuof_chars--;
288 bool
289 chars::starts_with(const char *x) const
291 if (empty() ||
292 !x ||
293 !*x)
294 return false;
295 if (strstr(chars_string, x) == chars_string)
296 return true;
297 return false;
301 chars::first_pos(char c)
303 if (empty())
304 return -1;
305 char *pos= strchr(chars_string, c);
306 if (pos == NULL)
307 return -1;
308 return pos-chars_string;
311 long int
312 chars::lint(void)
314 return lint(10);
317 long int
318 chars::lint(int base)
320 if (base < 2) base= 0;
321 if (base > 36) base= 36;
322 if (empty()) return 0;
323 long int l= strtol(chars_string, 0, base);
324 return l;
327 chars &
328 chars::append(const char *s)
330 if (!s)
331 return(*this);
333 char *temp= (char*)malloc(chars_length + strlen(s) + 1);
334 if (chars_string)
336 strcpy(temp, chars_string);
337 if (dynamic)
338 free(chars_string);
340 else
341 temp[0]= '\0';
342 strcat(temp, s);
343 chars_string= temp;
344 chars_length+= strlen(s);
345 dynamic= true;
347 return *this;
350 chars &
351 chars::append(char c)
353 if (!c)
354 return(*this);
356 char *temp= (char*)malloc(chars_length + 1 + 1);
357 if (chars_string)
359 strcpy(temp, chars_string);
360 if (dynamic)
361 free(chars_string);
363 else
364 temp[0]= '\0';
365 char b[2];
366 b[0]= c;
367 b[1]= 0;
368 strcat(temp, b);
369 chars_string= temp;
370 chars_length+= 1;
371 dynamic= true;
373 return *this;
376 chars &
377 chars::appendf(const char *format, ...)
379 va_list ap;
380 char n[1000];
382 va_start(ap, format);
383 vsnprintf(n, 999, format, ap);
384 va_end(ap);
386 char *temp= (char*)malloc(chars_length + strlen(n) + 1);
387 if (chars_string)
389 strcpy(temp, chars_string);
390 if (dynamic)
391 free(chars_string);
393 else
394 temp[0]= '\0';
395 strcat(temp, n);
396 chars_string= temp;
397 chars_length+= strlen(n);
398 dynamic= true;
400 return *this;
403 chars &
404 chars::appendn(const char *src, int n)
406 char *temp= (char*)malloc(chars_length + n + 1);
407 if (chars_string)
409 strcpy(temp, chars_string);
410 if (dynamic)
411 free(chars_string);
413 else
414 temp[0]= 0;
415 int s= 0;
416 while (src[s] && (s<n))
417 temp[chars_length++]= src[s++];
418 temp[chars_length]= '\0';
419 chars_string= temp;
420 dynamic= true;
421 return *this;
424 chars &
425 chars::format(const char *format, ...)
427 deallocate_string();
429 va_list ap;
430 char n[1000];
432 va_start(ap, format);
433 vsnprintf(n, 999, format, ap);
434 va_end(ap);
436 char *temp= (char*)malloc(chars_length + strlen(n) + 1);
437 if (chars_string)
439 strcpy(temp, chars_string);
440 if (dynamic)
441 free(chars_string);
443 else
444 temp[0]= '\0';
445 strcat(temp, n);
446 chars_string= temp;
447 chars_length+= strlen(n);
448 dynamic= true;
450 return *this;
453 chars &
454 chars::uppercase(void)
456 if (!dynamic)
457 allocate_string(chars_string);
459 for (int i= 0; i < chars_length; i++)
460 chars_string[i]= toupper(chars_string[i]);
462 return *this;
465 chars &
466 chars::subst(const char *what, char with)
468 if (!dynamic)
469 allocate_string(chars_string);
471 for (int i= 0; i < chars_length; i++)
472 if (strchr(what, chars_string[i]))
473 chars_string[i] = with;
475 return *this;
478 chars &
479 chars::substr(int start, int maxlen)
481 if (!chars_string)
482 return *this;
484 char *s= (char*)malloc(maxlen+1);
485 int i, l;
486 for (i= start, l= 0; i<chars_length && chars_string[i] && l<maxlen; i++, l++)
487 s[l]= chars_string[i];
488 s[l]= 0;
489 deallocate_string();
490 chars_string= s;
491 chars_length= l;
492 dynamic= true;
493 return *this;
497 // Assignment operators
498 chars &
499 chars::operator=(const char *s)
501 allocate_string(s);
502 return(*this);
505 chars &
506 chars::operator=(const chars &cs)
508 if (this != &cs)
509 allocate_string(cs.chars_string);
510 return(*this);
514 // Arithmetic operators
516 chars
517 chars::operator+(char c) const
519 char b[2];
520 b[0]= c;
521 b[1]= 0;
522 chars temp(chars_string);
523 return temp.append(b);
526 chars
527 chars::operator+(const char *s) const
529 chars temp(chars_string);
530 return(temp.append(s));
533 chars
534 operator+(char c, const chars &cs)
536 char b[2];
537 b[0]= c;
538 b[1]= 0;
539 chars temp(b);
540 return(temp.append(cs));
544 // Boolean operators
545 bool
546 chars::equal(const char *s) const
548 if ((chars_string &&
549 !s) ||
550 (!chars_string &&
552 return(0);
553 if (!chars_string &&
555 return(1);
556 return(strcmp(chars_string, s) == 0);
559 bool
560 chars::operator==(const char *s) const
562 return(equal(s));
565 bool
566 chars::operator!=(const char *s) const
568 return(!equal(s));
572 /* End of utils.src/chars.cc */