c99fc7a65f3825ee8a8955121c1bbf494c381238
[vg.git] / vg_string.h
1 #ifndef VG_STRING_H
2 #define VG_STRING_H
3
4 /* string builder with optional dynamic memory or static buffer. */
5
6 #include "vg_stdint.h"
7
8 typedef struct vg_str vg_str;
9 typedef struct vg_str_dynamic vg_str_dynamic;
10
11 struct vg_str{
12 char *buffer;
13 i32 i, /* -1: error condition. otherwise, current cursor position */
14 len; /* -1: dynamically allocated. otherwise, buffer length */
15 };
16
17 struct vg_str_dynamic {
18 i32 len;
19 };
20
21 /*
22 * Returns the current storage size of the string
23 */
24 static i32 vg_str_storage( vg_str *str ){
25 if( str->len == -1 ){
26 if( str->buffer ){
27 vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer;
28 return (arr-1)->len;
29 }
30 else return 0;
31 }
32 else return str->len;
33 }
34
35 /*
36 * Reset string. If len is -1 (dynamically allocated), buffer must be either
37 * NULL or be acquired from malloc or realloc
38 */
39 static void vg_strnull( vg_str *str, char *buffer, i32 len ){
40 str->buffer = buffer;
41 if( buffer )
42 str->buffer[0] = '\0';
43
44 str->i = 0;
45 str->len = len;
46
47 assert(len);
48 }
49
50 static void vg_strfree( vg_str *str ){
51 if( str->len == -1 ){
52 if( str->buffer ){
53 vg_str_dynamic *arr = (vg_str_dynamic *)str->buffer;
54 free( arr-1 );
55
56 str->buffer = NULL;
57 str->i = 0;
58 }
59 }
60 }
61
62 /*
63 * Double the size of the dynamically allocated string. If unallocated, alloc of
64 * 16 bytes minimum.
65 */
66 static i32 vg_str_dynamic_grow( vg_str *str ){
67 if( str->buffer ){
68 vg_str_dynamic *hdr = ((vg_str_dynamic *)str->buffer) - 1;
69 i32 total = (hdr->len + sizeof(vg_str_dynamic)) * 2;
70 hdr = realloc( hdr, total );
71 hdr->len = total - sizeof(vg_str_dynamic);
72 str->buffer = (char *)(hdr+1);
73 return hdr->len;
74 }
75 else {
76 vg_str_dynamic *hdr = malloc(16);
77 hdr->len = 16-sizeof(vg_str_dynamic);
78 str->buffer = (char *)(hdr+1);
79 str->buffer[0] = '\0';
80 return hdr->len;
81 }
82 }
83
84 /*
85 * Append null terminated string to vg_str
86 */
87 static void vg_strcat( vg_str *str, const char *append ){
88 if( !append || (str->i == -1) ) return;
89
90 i32 max = vg_str_storage( str ),
91 i = 0;
92
93 append:
94 if( str->i == max ){
95 if( str->len == -1 )
96 max = vg_str_dynamic_grow( str );
97 else{
98 str->i = -1;
99 str->buffer[ max-1 ] = '\0';
100 return;
101 }
102 }
103
104 char c = append[ i ++ ];
105 str->buffer[ str->i ] = c;
106
107 if( c == '\0' )
108 return;
109
110 str->i ++;
111 goto append;
112 }
113
114 /*
115 * Append character to vg_str
116 */
117 static void vg_strcatch( vg_str *str, char c ){
118 vg_strcat( str, (char[]){ c, '\0' } );
119 }
120
121 /*
122 * FIXME: Negative numbers
123 */
124 static void vg_strcati32( vg_str *str, i32 value ){
125 if( value ){
126 char temp[32];
127 int i=0;
128 while( value && (i<31) ){
129 temp[ i ++ ] = '0' + (value % 10);
130 value /= 10;
131 }
132
133 char reverse[32];
134 for( int j=0; j<i; j ++ )
135 reverse[j] = temp[ i-1-j ];
136 reverse[i] = '\0';
137
138 vg_strcat( str, reverse );
139 }
140 else
141 vg_strcat( str, "0" );
142 }
143
144 static void vg_strcati32r( vg_str *str, i32 value, i32 n, char alt ){
145 char temp[32];
146 i32 i=0;
147 while( value ){
148 if( i>=n )
149 break;
150
151 temp[ n-1 - (i ++) ] = '0' + (value % 10);
152 value /= 10;
153 }
154
155 for( ;i<n; i ++ )
156 temp[ n-1 - i ] = alt;
157
158 temp[n]='\0';
159 vg_strcat( str, temp );
160 }
161
162 /*
163 * Returns 1 if string did not overflow while building
164 */
165 static int vg_strgood( vg_str *str ){
166 if( str->i == -1 ) return 0;
167 else return 1;
168 }
169
170 /*
171 * Returns pointer to last instance of character
172 */
173 static char *vg_strch( vg_str *str, char c ){
174 char *ptr = NULL;
175 for( i32 i=0; i<str->i; i++ ){
176 if( str->buffer[i] == c )
177 ptr = str->buffer+i;
178 }
179
180 return ptr;
181 }
182
183 #endif /* VG_STRING_H */