-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvlarr.c
267 lines (243 loc) · 6.94 KB
/
cvlarr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "cvlarr.h"
obj_cvlarr*
OBF(cvlarr,finalize)(obj_cvlarr* this){
NULL_P_CHECK(this);
this->mem=calloc(CVLARR_DEF_ALLOC_SIZE,sizeof(char));
this->memSize=CVLARR_DEF_ALLOC_SIZE*sizeof(char);
this->endOfLastString=0;
this->indexArr=calloc(CVLARR_DEF_SIZE,sizeof(size_t));
this->indexArrSize=CVLARR_DEF_SIZE*sizeof(size_t);
this->noStrings=0;
this->iterPos=0;
#ifdef DEBUG
this->_debug_resizeCount=0;
#endif
return(this);}
void
OBF(cvlarr,clean)(obj_cvlarr* this){
NULL_P_CHECK(this);
#ifdef DEBUG
fprintf(stderr,"INFO %s freeing obj_cvlarr at %p\n",\
__func__,(void*)this);
#endif
free(this->mem);
free(this->indexArr);}
char*
obj_cvlarr_getStr(obj_cvlarr* this, unsigned int index){
NULL_P_CHECK(this);
char* retPtr=NULL;
if(this->noStrings<1 || index>=this->noStrings)
return(NULL);
#ifdef DEBUG
if(index>this->indexArrSize){
fprintf(stderr,"ERR %s inder over indexArrSize limit in obj %p\n",\
__func__,(void*)this);
return(NULL);}
#endif
retPtr=this->mem+*(this->indexArr+index);
#ifdef DEBUG
if(retPtr>=this->mem+this->memSize){
fprintf(stderr,"ERR %s retPtr over memSize limit in obj %p\n",\
__func__,(void*)this);
return(NULL);}
#endif
return(retPtr);}
int
obj_cvlarr__resize_mem(obj_cvlarr* this, size_t by){
if((this->mem=realloc(this->mem,this->memSize+by))==NULL)
return(1);
memset(this->mem+this->memSize/sizeof(char),0,by);
this->memSize+=by;
#ifdef DEBUG
this->_debug_resizeCount++;
#endif
return(0);}
int
obj_cvlarr__resize_indexArr(obj_cvlarr* this, size_t by){
size_t* reallocPtr=NULL;
if((reallocPtr=realloc(this->indexArr,this->indexArrSize+by))==NULL)
return(1);
this->indexArr=reallocPtr;
/* TODO memset is broken, NOT sure why */
//memset(this->indexArr+this->noStrings,0,by);
this->indexArrSize+=by;
#ifdef DEBUG
this->_debug_resizeCount++;
#endif
return(0);}
int
obj_cvlarr__autoExtend(obj_cvlarr* this, size_t nextString){
if((this->noStrings+1)*sizeof(size_t)>=this->indexArrSize)
if(obj_cvlarr__resize_indexArr(this,this->indexArrSize+CVLARR_DEF_SIZE*sizeof(size_t))){
#ifdef DEBUG
fprintf(stderr,"ERR %s failed to reallocate this->\n",__func__);
#endif
return(1);}
if(nextString<1||nextString>CVLARR_STRL_MAX)
return(1);
/* memsize - (freespace-strlen) */
if(this->memSize-(this->endOfLastString+nextString*sizeof(char))<=0){
if(obj_cvlarr__resize_mem(this,this->memSize+(nextString+1)*sizeof(char))){
#ifdef DEBUG
fprintf(stderr,"ERR %s failed to reallocate this->mem\n",__func__);
#endif
return(1);}}
return(0);}
int
obj_cvlarr_resize(obj_cvlarr* this, unsigned int charArrSize, unsigned int indexArrSize){
NULL_P_CHECK(this);
int ret=0;
size_t newSize;
if((newSize=charArrSize*sizeof(char)-this->memSize)>0)
obj_cvlarr__resize_mem(this,newSize);
else
ret=1;
if((newSize=indexArrSize*sizeof(size_t)-(this->indexArrSize))>0)
obj_cvlarr__resize_indexArr(this, newSize);
else
ret=1;
return(ret);
}
void
OBF(cvlarr,copy)(obj_cvlarr* this, obj_cvlarr* dest){
NULL_P_CHECK(this);
NULL_P_CHECK(dest);
//resize
OBF(cvlarr,_resize_mem)(dest, this->memSize-dest->memSize);
memcpy(dest->mem,this->mem, dest->memSize);
OBF(cvlarr,_resize_indexArr)(dest, this->indexArrSize-dest->indexArrSize);
memcpy(dest->indexArr, this->indexArr, dest->indexArrSize);
STRUCTCOPPIER(dest,this,memSize);
STRUCTCOPPIER(dest,this,indexArrSize);
STRUCTCOPPIER(dest,this,noStrings);
STRUCTCOPPIER(dest,this,iterPos);
#ifdef DEBUG
STRUCTCOPPIER(dest,this,_debug_resizeCount);
#endif
}
char*
obj_cvlarr_insert(obj_cvlarr* this, char* string){
NULL_P_CHECK(this);
char* retptr=NULL;
size_t strl=strnlen(string,CVLARR_STRL_MAX);
if(obj_cvlarr__autoExtend(this,strl))
return(NULL);
retptr=(this->mem+this->endOfLastString+1*sizeof(char));
*(this->indexArr+this->noStrings)=retptr-this->mem;
fprintf(stderr,"retptr-this->mem=%lu\n",retptr-this->mem);
this->noStrings++;
this->endOfLastString+=strl;
memcpy(retptr,string,strl);
/* just in case... */
*(this->mem+this->endOfLastString)='\0';
return(retptr);}
void
OBF(cvlarr,iterReset)(obj_cvlarr* this){
NULL_P_CHECK(this);
this->iterPos=0;}
char*
OBF(cvlarr,iterNext)(obj_cvlarr* this){
NULL_P_CHECK(this);
char* retPtr=NULL;
if(this->iterPos+1>this->noStrings){
#ifdef DEBUG
fprintf(stderr,"WARN %s iterNext over range\n",__func__);
#endif
return(NULL);}
retPtr=OBF(cvlarr,getStr)(this,this->iterPos);
this->iterPos++;
return(retPtr);}
void
OBF(cvlarr,print)(obj_cvlarr* this){
NULL_P_CHECK(this);
fprintf(stderr,"\ndumping obj_cvlarr %p\n",(void*)this);
DUMP_STRUCT_voidPtr(this,mem);
DUMP_STRUCT_size_t(this,memSize);
DUMP_STRUCT_size_t(this,endOfLastString);
DUMP_STRUCT_voidPtr(this,indexArr);
DUMP_STRUCT_size_t(this,indexArrSize);
DUMP_STRUCT_uint(this,noStrings);
DUMP_STRUCT_uint(this,iterPos);
#ifdef DEBUG
DUMP_STRUCT_uint(this,_debug_resizeCount);
#endif
char* p_str=NULL;
OBF(cvlarr,iterReset)(this);
while( (p_str=OBF(cvlarr,iterNext)(this)) !=NULL ){
fprintf(stderr,"\t string no %u \"%s\" at %p %p\n",
this->iterPos-1,p_str,p_str,this->mem+*(this->indexArr+(this->iterPos-1)));}
fprintf(stderr,"\nEND of obj_cvlarr %p\n",(void*)this);}
// TODO UNFINISHED UNTESTED
int
cvlarrParse(obj_cvlarr* this, json_stream* js){
NULL_P_CHECK(this);
NULL_P_CHECK(js);
enum json_type type;
const char* str=json_get_string(js,NULL);
bool var=false;
bool arrLoop=true;
while(true){
type=json_next(js);
switch(type){
case JSON_ERROR:
PARSE_EMSG(js,json_typename[type]);
fprintf(stderr,"JSON ERR %s\n",\
json_get_error(js));
break;
case JSON_NULL:
case JSON_TRUE:
case JSON_FALSE:
case JSON_NUMBER:
break;
case JSON_STRING:
var=true;
break;
case JSON_ARRAY:
case JSON_OBJECT:
case JSON_DONE:
PARSE_EMSG(js,json_typename[type]);
break;
case JSON_ARRAY_END:
PARSE_EMSG(js,json_typename[type]);
return(1);
case JSON_OBJECT_END:
fprintf(stderr,"SHIP END l=%ld\n",json_get_lineno(js));
return(0);}
if(var){
var=false;
if(strcmp("_clvarr_size",str)==0){
if(json_next(js)==JSON_STRING){
OBF(cvlarr,resize)(this,(size_t)json_get_number(js),0);
continue;}}
if(strcmp("arr",str)==0){
while(arrLoop){
type=json_next(js);
switch(type){
case JSON_ERROR:
PARSE_EMSG(js,json_typename[type]);
fprintf(stderr,"JSON ERR %s\n",\
json_get_error(js));
break;
case JSON_NULL:
case JSON_TRUE:
case JSON_FALSE:
case JSON_NUMBER:
break;
case JSON_STRING:
/* cast cause arg is cons char* */
OBF(cvlarr,insert)(this,(char*)str);
break;
case JSON_ARRAY:
case JSON_OBJECT_END:
case JSON_OBJECT:
case JSON_DONE:
PARSE_EMSG(js,json_typename[type]);
break;
case JSON_ARRAY_END:
arrLoop=false;
break;}}}
//fprintf(stderr,"json %s found invalid key %s ",__func__,str);
//type=json_next(js);
//fprintf(stderr,"with value %s\n",str);
}}
return(1);}