2 * Simulator of microcontrollers (chars.cc)
4 * Copyright (C) 1997 Drotos Daniel
6 * To contact author send email to dr.dkdb@gmail.com
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
53 chars::chars(const char *s
)
55 if ((chars_string
= const_cast<char *>(s
)) != NULL
)
56 chars_length
= strlen(s
);
62 chars::chars(const chars
&cs
)
69 chars::chars(const char *, const char *fmt
, ...)
75 vsnprintf(n
, 999, fmt
, ap
);
78 chars_string
= strdup(n
);
79 chars_length
= strlen(n
);
91 chars::allocate_string(const char *s
)
100 n
= (char*)malloc(l
+1);
112 chars::deallocate_string(void)
128 if (idx
>=chars_length
)
130 return chars_string
[idx
];
134 chars::token(const char *delims
) const
136 chars c
= (char*)NULL
;
138 if (!delims
|| !*delims
)
140 if (pars_pos
>= chars_length
)
142 if (chars_length
< 1)
146 // skip initial delims first;
147 l
= strspn(&chars_string
[pars_pos
], delims
);
149 if (pars_pos
>= chars_length
)
151 // skip chars not in delims: search token end
152 l
= strcspn(&chars_string
[pars_pos
], delims
);
157 for (i
= pars_pos
; i
< pars_pos
+l
; i
++)
160 // skip delims at end
161 l
= strspn(&chars_string
[pars_pos
], delims
);
176 for (i
= 0; chars_string
[i
]; i
++)
178 char c
= toupper(chars_string
[i
]);
179 if ((c
>='0') && (c
<='9'))
181 else if ((c
>='A') && (c
<='F'))
191 unsigned long long int
194 unsigned long long int v
= 0;
198 for (i
= 0; chars_string
[i
]; i
++)
200 char c
= toupper(chars_string
[i
]);
201 if ((c
>='0') && (c
<='9'))
203 else if ((c
>='A') && (c
<='F'))
217 char *p
= chars_string
;
220 while (*p
&& isspace(*p
))
234 if (isspace(chars_string
[i
]))
243 chars::lrip(const char *cset
)
250 skip
= strspn(chars_string
, cset
);
252 allocate_string(chars_string
+skip
);
256 chars::rrip(const char *cset
)
262 int i
= chars_length
-1;
265 char c
= chars_string
[i
];
266 if (strchr(cset
, c
) != NULL
)
275 chars::rrip(int nuof_chars
)
278 if (nuof_chars
< 1) return;
279 int i
= chars_length
-1;
280 while ((i
>=0) && nuof_chars
)
289 chars::starts_with(const char *x
) const
295 if (strstr(chars_string
, x
) == chars_string
)
301 chars::first_pos(char c
)
305 char *pos
= strchr(chars_string
, c
);
308 return pos
-chars_string
;
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
);
328 chars::append(const char *s
)
333 char *temp
= (char*)malloc(chars_length
+ strlen(s
) + 1);
336 strcpy(temp
, chars_string
);
344 chars_length
+= strlen(s
);
351 chars::append(char c
)
356 char *temp
= (char*)malloc(chars_length
+ 1 + 1);
359 strcpy(temp
, chars_string
);
377 chars::appendf(const char *format
, ...)
382 va_start(ap
, format
);
383 vsnprintf(n
, 999, format
, ap
);
386 char *temp
= (char*)malloc(chars_length
+ strlen(n
) + 1);
389 strcpy(temp
, chars_string
);
397 chars_length
+= strlen(n
);
404 chars::appendn(const char *src
, int n
)
406 char *temp
= (char*)malloc(chars_length
+ n
+ 1);
409 strcpy(temp
, chars_string
);
416 while (src
[s
] && (s
<n
))
417 temp
[chars_length
++]= src
[s
++];
418 temp
[chars_length
]= '\0';
425 chars::format(const char *format
, ...)
432 va_start(ap
, format
);
433 vsnprintf(n
, 999, format
, ap
);
436 char *temp
= (char*)malloc(chars_length
+ strlen(n
) + 1);
439 strcpy(temp
, chars_string
);
447 chars_length
+= strlen(n
);
454 chars::uppercase(void)
457 allocate_string(chars_string
);
459 for (int i
= 0; i
< chars_length
; i
++)
460 chars_string
[i
]= toupper(chars_string
[i
]);
466 chars::subst(const char *what
, char with
)
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
;
479 chars::substr(int start
, int maxlen
)
484 char *s
= (char*)malloc(maxlen
+1);
486 for (i
= start
, l
= 0; i
<chars_length
&& chars_string
[i
] && l
<maxlen
; i
++, l
++)
487 s
[l
]= chars_string
[i
];
497 // Assignment operators
499 chars::operator=(const char *s
)
506 chars::operator=(const chars
&cs
)
509 allocate_string(cs
.chars_string
);
514 // Arithmetic operators
517 chars::operator+(char c
) const
522 chars
temp(chars_string
);
523 return temp
.append(b
);
527 chars::operator+(const char *s
) const
529 chars
temp(chars_string
);
530 return(temp
.append(s
));
534 operator+(char c
, const chars
&cs
)
540 return(temp
.append(cs
));
546 chars::equal(const char *s
) const
556 return(strcmp(chars_string
, s
) == 0);
560 chars::operator==(const char *s
) const
566 chars::operator!=(const char *s
) const
572 /* End of utils.src/chars.cc */