Added missing file.
[tinycc/k1w1.git] / tcccstring.c
blobe4c67e4e8b42511af64cba5a8799d2d9a40adca1
1 #include "tcccstring.h"
3 void cstr_realloc(CString *cstr, int new_size)
5 int size;
6 void *data;
8 size = cstr->size_allocated;
9 if (size == 0)
10 size = 8; /* no need to allocate a too small first string */
11 while (size < new_size)
12 size = size * 2;
13 data = tcc_realloc(cstr->data_allocated, size);
14 if (!data)
15 error("memory full");
16 cstr->data_allocated = data;
17 cstr->size_allocated = size;
18 cstr->data = data;
21 void cstr_cat(CString *cstr, const char *str)
23 int c;
24 for(;;) {
25 c = *str;
26 if (c == '\0')
27 break;
28 cstr_ccat(cstr, c);
29 str++;
33 /* add a wide char */
34 void cstr_wccat(CString *cstr, int ch)
36 int size;
37 size = cstr->size + sizeof(nwchar_t);
38 if (size > cstr->size_allocated)
39 cstr_realloc(cstr, size);
40 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
41 cstr->size = size;
44 void cstr_new(CString *cstr)
46 memset(cstr, 0, sizeof(CString));
49 /* free string and reset it to NULL */
50 void cstr_free(CString *cstr)
52 tcc_free(cstr->data_allocated);
53 cstr_new(cstr);
56 #define cstr_reset(cstr) cstr_free(cstr)
58 /* XXX: unicode ? */
59 void add_char(CString *cstr, int c)
61 if (c == '\'' || c == '\"' || c == '\\') {
62 /* XXX: could be more precise if char or string */
63 cstr_ccat(cstr, '\\');
65 if (c >= 32 && c <= 126) {
66 cstr_ccat(cstr, c);
67 } else {
68 cstr_ccat(cstr, '\\');
69 if (c == '\n') {
70 cstr_ccat(cstr, 'n');
71 } else {
72 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
73 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
74 cstr_ccat(cstr, '0' + (c & 7));