yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc) { yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t)); memset((void *) b, 0, sizeof(struct yajl_buf_t)); b->alloc = alloc; return b; }
yajl_handle yajl_alloc(const yajl_callbacks * callbacks, yajl_alloc_funcs * afs, void * ctx) { yajl_handle hand = NULL; yajl_alloc_funcs afsBuffer; /* first order of business is to set up memory allocation routines */ if (afs != NULL) { if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL) { return NULL; } } else { yajl_set_default_alloc_funcs(&afsBuffer); afs = &afsBuffer; } hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t)); /* copy in pointers to allocation routines */ memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs)); hand->callbacks = callbacks; hand->ctx = ctx; hand->lexer = NULL; hand->bytesConsumed = 0; hand->decodeBuf = yajl_buf_alloc(&(hand->alloc)); hand->flags = 0; yajl_bs_init(hand->stateStack, &(hand->alloc)); yajl_bs_push(hand->stateStack, yajl_state_start); return hand; }
yajl_gen yajl_gen_alloc(const yajl_gen_config * config, const yajl_alloc_funcs * afs) { yajl_gen g = NULL; yajl_alloc_funcs afsBuffer; /* first order of business is to set up memory allocation routines */ if (afs != NULL) { if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL) { return NULL; } } else { yajl_set_default_alloc_funcs(&afsBuffer); afs = &afsBuffer; } g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t)); memset((void *) g, 0, sizeof(struct yajl_gen_t)); /* copy in pointers to allocation routines */ memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs)); if (config) { g->pretty = config->beautify; g->indentString = config->indentString ? config->indentString : " "; } g->buf = yajl_buf_alloc(&(g->alloc)); return g; }
yajl_gen yajl_gen_alloc2(const yajl_print_t callback, const yajl_gen_config * config, const yajl_alloc_funcs * afs, void * ctx) { yajl_gen g = NULL; yajl_alloc_funcs afsBuffer; /* first order of business is to set up memory allocation routines */ if (afs != NULL) { if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL) { return NULL; } } else { yajl_set_default_alloc_funcs(&afsBuffer); afs = &afsBuffer; } g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t)); if (!g) return NULL; memset((void *) g, 0, sizeof(struct yajl_gen_t)); /* copy in pointers to allocation routines */ memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs)); if (config) { const char *indent = config->indentString; g->pretty = config->beautify; g->indentString = config->indentString; if (indent) { for (; *indent; indent++) { if (*indent != '\n' && *indent != '\v' && *indent != '\f' && *indent != '\t' && *indent != '\r' && *indent != ' ') { g->indentString = NULL; break; } } } if (!g->indentString) { g->indentString = " "; } } if (callback) { g->print = callback; g->ctx = ctx; } else { g->print = (yajl_print_t)&yajl_buf_append; g->ctx = yajl_buf_alloc(&(g->alloc)); } return g; }
bool hkvJsonStreamReader::Parse(const char* pszFileName, unsigned int uiChunkSize) { yajl_status stat; void* pFile; size_t rd; // Allocate read buffer unsigned char* pFileData = (unsigned char*)(YA_MALLOC(&(m_pHandle->alloc), uiChunkSize)); assert(pFileData); // Open file pFile = m_pFileHandler->Open(pszFileName, hkvJsonFileHandler::JFM_READ); if (pFile == NULL) return false; for (;;) { rd = m_pFileHandler->Read((void*)pFileData, uiChunkSize - 1, pFile); if (rd == 0) { if (!m_pFileHandler->IsEOF(pFile)) fprintf(stderr, "Error encountered on file read.\n"); break; } pFileData[rd] = 0; // Pass to parser stat = yajl_parse(m_pHandle, pFileData, rd); if (stat != yajl_status_ok) break; } // Parse any remaining buffered data stat = yajl_complete_parse(m_pHandle); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(m_pHandle, 1, pFileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(m_pHandle, str); assert(false); return false; } // Close the file m_pFileHandler->Close(pFile); // Free read buffer YA_FREE(&(m_pHandle->alloc), pFileData); return true; }
yajl_handle yajl_alloc(const yajl_callbacks * callbacks, const yajl_parser_config * config, const yajl_alloc_funcs * afs, void * ctx) { unsigned int allowComments = 0; unsigned int validateUTF8 = 0; yajl_handle hand = NULL; yajl_alloc_funcs afsBuffer; /* first order of business is to set up memory allocation routines */ if (afs != NULL) { if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL) { return NULL; } } else { yajl_set_default_alloc_funcs(&afsBuffer); afs = &afsBuffer; } hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t)); /* copy in pointers to allocation routines */ memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs)); if (config != NULL) { allowComments = config->allowComments; validateUTF8 = config->checkUTF8; } hand->callbacks = callbacks; hand->ctx = ctx; hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8); hand->bytesConsumed = 0; hand->decodeBuf = yajl_buf_alloc(&(hand->alloc)); yajl_bs_init(hand->stateStack, &(hand->alloc)); yajl_bs_push(hand->stateStack, yajl_state_start); return hand; }
rk_yajl_gen rk_yajl_gen_alloc2(const rk_yajl_print_t callback, const rk_yajl_gen_config * config, const rk_yajl_alloc_funcs * afs, void * ctx) { rk_yajl_gen g = NULL; rk_yajl_alloc_funcs afsBuffer; /* first order of business is to set up memory allocation routines */ if (afs != NULL) { if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL) { return NULL; } } else { rk_yajl_set_default_alloc_funcs(&afsBuffer); afs = &afsBuffer; } g = (rk_yajl_gen) YA_MALLOC(afs, sizeof(struct rk_yajl_gen_t)); memset((void *) g, 0, sizeof(struct rk_yajl_gen_t)); /* copy in pointers to allocation routines */ memcpy((void *) &(g->alloc), (void *) afs, sizeof(rk_yajl_alloc_funcs)); if (config) { g->pretty = config->beautify; g->indentString = config->indentString ? config->indentString : " "; } if (callback) { g->print = callback; g->ctx = ctx; } else { g->print = (rk_yajl_print_t)&rk_yajl_buf_append; g->ctx = rk_yajl_buf_alloc(&(g->alloc)); } return g; }
static void yajl_buf_ensure_available(yajl_buf buf, size_t want) { size_t need; assert(buf != NULL); /* first call */ if (buf->data == NULL) { buf->len = YAJL_BUF_INIT_SIZE; buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len); buf->data[0] = 0; } need = buf->len; while (want >= (need - buf->used)) need <<= 1; if (need != buf->len) { buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need); buf->len = need; } }
unsigned char * yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText, unsigned int jsonTextLen, int verbose) { unsigned int offset = hand->bytesConsumed; unsigned char * str; const char * errorType = NULL; const char * errorText = NULL; char text[72]; const char * arrow = " (right here) ------^\n"; if (yajl_bs_current(hand->stateStack) == yajl_state_parse_error) { errorType = "parse"; errorText = hand->parseError; } else if (yajl_bs_current(hand->stateStack) == yajl_state_lexical_error) { errorType = "lexical"; errorText = yajl_lex_error_to_string(yajl_lex_get_error(hand->lexer)); } else { errorType = "unknown"; } { unsigned int memneeded = 0; memneeded += strlen(errorType); memneeded += strlen(" error"); if (errorText != NULL) { memneeded += strlen(": "); memneeded += strlen(errorText); } str = (unsigned char *) YA_MALLOC(&(hand->alloc), memneeded + 2); str[0] = 0; strcat((char *) str, errorType); strcat((char *) str, " error"); if (errorText != NULL) { strcat((char *) str, ": "); strcat((char *) str, errorText); } strcat((char *) str, "\n"); } /* now we append as many spaces as needed to make sure the error * falls at char 41, if verbose was specified */ if (verbose) { unsigned int start, end, i; unsigned int spacesNeeded; spacesNeeded = (offset < 30 ? 40 - offset : 10); start = (offset >= 30 ? offset - 30 : 0); end = (offset + 30 > jsonTextLen ? jsonTextLen : offset + 30); for (i=0;i<spacesNeeded;i++) text[i] = ' '; for (;start < end;start++, i++) { if (jsonText[start] != '\n' && jsonText[start] != '\r') { text[i] = jsonText[start]; } else { text[i] = ' '; } } assert(i <= 71); text[i++] = '\n'; text[i] = 0; { char * newStr = (char *) YA_MALLOC(&(hand->alloc), (strlen((char *) str) + strlen((char *) text) + strlen(arrow) + 1)); newStr[0] = 0; strcat((char *) newStr, (char *) str); strcat((char *) newStr, text); strcat((char *) newStr, arrow); YA_FREE(&(hand->alloc), str); str = (unsigned char *) newStr; } } return str; }