void yajl_free(yajl_handle handle) { yajl_bs_free(handle->stateStack); yajl_buf_free(handle->decodeBuf); yajl_lex_free(handle->lexer); YA_FREE(&(handle->alloc), handle); }
yajl_val yajl_tree_parse_options (const char *input, char *error_buffer, size_t error_buffer_size, yajl_tree_option options) { static const yajl_callbacks callbacks = { /* null = */ handle_null, /* boolean = */ handle_boolean, /* integer = */ NULL, /* double = */ NULL, /* number = */ handle_number, /* string = */ handle_string, /* start map = */ handle_start_map, /* map key = */ handle_string, /* end map = */ handle_end_map, /* start array = */ handle_start_array, /* end array = */ handle_end_array }; yajl_handle handle; yajl_status status; char * internal_err_str; context_t ctx = { NULL, NULL, NULL, 0 }; ctx.errbuf = error_buffer; ctx.errbuf_size = error_buffer_size; if (error_buffer != NULL) memset (error_buffer, 0, error_buffer_size); handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, (options & yajl_tree_option_dont_allow_comments) ? 0 : 1); yajl_config(handle, yajl_allow_trailing_separator, (options & yajl_tree_option_allow_trailing_separator) ? 1 : 0); status = yajl_parse(handle, (unsigned char *) input, strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { internal_err_str = (char *) yajl_get_error(handle, 1, (const unsigned char *) input, strlen(input)); snprintf(error_buffer, error_buffer_size, "%s", internal_err_str); YA_FREE(&(handle->alloc), internal_err_str); } while (ctx.stack) { yajl_tree_free(context_pop(&ctx)); } yajl_free (handle); return NULL; } yajl_free (handle); return (ctx.root); }
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; }
void yajl_gen_free(yajl_gen g) { if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_free((yajl_buf)g->ctx); YA_FREE(&(g->alloc), g); }
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; }
void yajl_free_error(yajl_handle hand, unsigned char * str) { /* use memory allocation functions if set */ YA_FREE(&(hand->alloc), str); }
void yajl_buf_free(yajl_buf buf) { assert(buf != NULL); if (buf->data) YA_FREE(buf->alloc, buf->data); YA_FREE(buf->alloc, buf); }
void rk_yajl_gen_free(rk_yajl_gen g) { if (g->print == (rk_yajl_print_t)&rk_yajl_buf_append) rk_yajl_buf_free((rk_yajl_buf)g->ctx); YA_FREE(&(g->alloc), g); }
void yajl_gen_free(yajl_gen g) { yajl_buf_free(g->buf); YA_FREE(&(g->alloc), g); }