00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00110 #ifndef DISABLE_DATASTRUCTURE
00111
00112 #include <stdio.h>
00113 #include <stdlib.h>
00114 #include <stdbool.h>
00115 #include <stdarg.h>
00116 #include <string.h>
00117 #include "qDecoder.h"
00118
00130 Q_OBSTACK *qObstackInit(void) {
00131 Q_OBSTACK *obstack;
00132
00133 obstack = (Q_OBSTACK *)malloc(sizeof(Q_OBSTACK));
00134 if(obstack == NULL) return NULL;
00135
00136 memset((void *)obstack, 0, sizeof(Q_OBSTACK));
00137 obstack->stack = qEntryInit();
00138 if(obstack->stack == NULL) {
00139 free(obstack);
00140 return NULL;
00141 }
00142
00143 return obstack;
00144 }
00145
00155 bool qObstackGrow(Q_OBSTACK *obstack, const void *object, size_t size) {
00156 if(obstack == NULL || object == NULL || size <= 0) return false;
00157 return qEntryPut(obstack->stack, "", object, size, false);
00158 }
00159
00168 bool qObstackGrowStr(Q_OBSTACK *obstack, const char *str) {
00169 return qObstackGrow(obstack, (void *)str, strlen(str));
00170 }
00171
00180 bool qObstackGrowStrf(Q_OBSTACK *obstack, const char *format, ...) {
00181 if(obstack == NULL) return false;
00182
00183 char str[1024];
00184 va_list arglist;
00185
00186 va_start(arglist, format);
00187 vsnprintf(str, sizeof(str), format, arglist);
00188 va_end(arglist);
00189
00190 return qObstackGrow(obstack, (void *)str, strlen(str));
00191 }
00192
00200 void *qObstackFinish(Q_OBSTACK *obstack) {
00201 if(obstack == NULL) return NULL;
00202
00203 void *dp;
00204 if(obstack->final != NULL) free(obstack->final);
00205 obstack->final = dp = (void *)malloc(obstack->stack->size + 1);
00206 if(obstack->final == NULL) return NULL;
00207
00208 const Q_NLOBJ *obj;
00209 for(obj = qEntryFirst(obstack->stack); obj; obj = qEntryNext(obstack->stack)) {
00210 memcpy(dp, obj->object, obj->size);
00211 dp += obj->size;
00212 }
00213 *((char *)dp) = '\0';
00214
00215 return obstack->final;
00216 }
00217
00225 void *qObstackGetFinal(Q_OBSTACK *obstack) {
00226 if(obstack == NULL) return NULL;
00227 if(obstack->final == NULL) qObstackFinish(obstack);
00228 return obstack->final;
00229 }
00230
00238 size_t qObstackGetSize(Q_OBSTACK *obstack) {
00239 if(obstack == NULL) return 0;
00240 return obstack->stack->size;
00241 }
00242
00250 int qObstackGetNum(Q_OBSTACK *obstack) {
00251 if(obstack == NULL) return 0;
00252 return obstack->stack->num;
00253 }
00254
00262 bool qObstackFree(Q_OBSTACK *obstack) {
00263 if(obstack == NULL) return false;
00264 qEntryFree(obstack->stack);
00265 if(obstack->final != NULL) free(obstack->final);
00266 free(obstack);
00267 return true;
00268 }
00269
00270 #endif