]> Dogcows Code - chaz/yoink/blob - src/Moof/yajl/src/yajl.c
minor cleanups
[chaz/yoink] / src / Moof / yajl / src / yajl.c
1 /*
2 * Copyright 2007-2009, Lloyd Hilaiel.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "yajl/yajl_parse.h"
34 #include "yajl_lex.h"
35 #include "yajl_parser.h"
36 #include "yajl_alloc.h"
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41
42 const char *
43 yajl_status_to_string(yajl_status stat)
44 {
45 const char * statStr = "unknown";
46 switch (stat) {
47 case yajl_status_ok:
48 statStr = "ok, no error";
49 break;
50 case yajl_status_client_canceled:
51 statStr = "client canceled parse";
52 break;
53 case yajl_status_insufficient_data:
54 statStr = "eof was met before the parse could complete";
55 break;
56 case yajl_status_error:
57 statStr = "parse error";
58 break;
59 }
60 return statStr;
61 }
62
63 yajl_handle
64 yajl_alloc(const yajl_callbacks * callbacks,
65 const yajl_parser_config * config,
66 const yajl_alloc_funcs * afs,
67 void * ctx)
68 {
69 unsigned int allowComments = 0;
70 unsigned int validateUTF8 = 0;
71 yajl_handle hand = NULL;
72 yajl_alloc_funcs afsBuffer;
73
74 /* first order of business is to set up memory allocation routines */
75 if (afs != NULL) {
76 if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
77 {
78 return NULL;
79 }
80 } else {
81 yajl_set_default_alloc_funcs(&afsBuffer);
82 afs = &afsBuffer;
83 }
84
85 hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
86
87 /* copy in pointers to allocation routines */
88 memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
89
90 if (config != NULL) {
91 allowComments = config->allowComments;
92 validateUTF8 = config->checkUTF8;
93 }
94
95 hand->callbacks = callbacks;
96 hand->ctx = ctx;
97 hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
98 hand->errorOffset = 0;
99 hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
100 yajl_bs_init(hand->stateStack, &(hand->alloc));
101
102 yajl_bs_push(hand->stateStack, yajl_state_start);
103
104 return hand;
105 }
106
107 void
108 yajl_free(yajl_handle handle)
109 {
110 yajl_bs_free(handle->stateStack);
111 yajl_buf_free(handle->decodeBuf);
112 yajl_lex_free(handle->lexer);
113 YA_FREE(&(handle->alloc), handle);
114 }
115
116 yajl_status
117 yajl_parse(yajl_handle hand, const unsigned char * jsonText,
118 unsigned int jsonTextLen)
119 {
120 unsigned int offset = 0;
121 yajl_status status;
122 status = yajl_do_parse(hand, &offset, jsonText, jsonTextLen);
123 return status;
124 }
125
126 yajl_status
127 yajl_parse_complete(yajl_handle hand)
128 {
129 /* The particular case we want to handle is a trailing number.
130 * Further input consisting of digits could cause our interpretation
131 * of the number to change (buffered "1" but "2" comes in).
132 * A very simple approach to this is to inject whitespace to terminate
133 * any number in the lex buffer.
134 */
135 return yajl_parse(hand, (const unsigned char *)" ", 1);
136 }
137
138 unsigned char *
139 yajl_get_error(yajl_handle hand, int verbose,
140 const unsigned char * jsonText, unsigned int jsonTextLen)
141 {
142 return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
143 }
144
145 void
146 yajl_free_error(yajl_handle hand, unsigned char * str)
147 {
148 /* use memory allocation functions if set */
149 YA_FREE(&(hand->alloc), str);
150 }
151
152 /* XXX: add utility routines to parse from file */
This page took 0.034072 seconds and 4 git commands to generate.