This is Unofficial EPICS BASE Doxygen Site
yajl_gen.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "epicsMath.h"
#include "yajl_gen.h"
#include "yajl_buf.h"
#include "yajl_encode.h"
+ Include dependency graph for yajl_gen.c:

Go to the source code of this file.

Classes

struct  yajl_gen_t
 

Macros

#define INSERT_SEP
 
#define INSERT_WHITESPACE
 
#define ENSURE_NOT_KEY
 
#define ENSURE_VALID_STATE
 
#define INCREMENT_DEPTH   if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
 
#define DECREMENT_DEPTH   if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
 
#define APPENDED_ATOM
 
#define FINAL_NEWLINE
 

Enumerations

enum  yajl_gen_state {
  yajl_gen_start, yajl_gen_map_start, yajl_gen_map_key, yajl_gen_map_val,
  yajl_gen_array_start, yajl_gen_in_array, yajl_gen_complete, yajl_gen_error
}
 

Functions

int yajl_gen_config (yajl_gen g, yajl_gen_option opt,...)
 
yajl_gen yajl_gen_alloc (const yajl_alloc_funcs *afs)
 
void yajl_gen_free (yajl_gen g)
 
yajl_gen_status yajl_gen_integer (yajl_gen g, long long int number)
 
yajl_gen_status yajl_gen_double (yajl_gen g, double number)
 
yajl_gen_status yajl_gen_number (yajl_gen g, const char *s, size_t l)
 
yajl_gen_status yajl_gen_string (yajl_gen g, const unsigned char *str, size_t len)
 
yajl_gen_status yajl_gen_null (yajl_gen g)
 
yajl_gen_status yajl_gen_bool (yajl_gen g, int boolean)
 
yajl_gen_status yajl_gen_map_open (yajl_gen g)
 
yajl_gen_status yajl_gen_map_close (yajl_gen g)
 
yajl_gen_status yajl_gen_array_open (yajl_gen g)
 
yajl_gen_status yajl_gen_array_close (yajl_gen g)
 
yajl_gen_status yajl_gen_get_buf (yajl_gen g, const unsigned char **buf, size_t *len)
 
void yajl_gen_clear (yajl_gen g)
 

Macro Definition Documentation

#define APPENDED_ATOM
Value:
switch (g->state[g->depth]) { \
g->state[g->depth] = yajl_gen_complete; \
break; \
g->state[g->depth] = yajl_gen_map_val; \
break; \
g->state[g->depth] = yajl_gen_in_array; \
break; \
g->state[g->depth] = yajl_gen_map_key; \
break; \
default: \
break; \
} \

Definition at line 176 of file yajl_gen.c.

#define DECREMENT_DEPTH   if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;

Definition at line 173 of file yajl_gen.c.

#define ENSURE_NOT_KEY
Value:
if (g->state[g->depth] == yajl_gen_map_key || \
g->state[g->depth] == yajl_gen_map_start) { \
} \

Definition at line 155 of file yajl_gen.c.

#define ENSURE_VALID_STATE
Value:
if (g->state[g->depth] == yajl_gen_error) { \
} else if (g->state[g->depth] == yajl_gen_complete) { \
}

Definition at line 163 of file yajl_gen.c.

#define FINAL_NEWLINE
Value:
if ((g->flags & yajl_gen_beautify) && g->state[g->depth] == yajl_gen_complete) \
g->print(g->ctx, "\n", 1);

Definition at line 195 of file yajl_gen.c.

#define INCREMENT_DEPTH   if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;

Definition at line 170 of file yajl_gen.c.

#define INSERT_SEP
Value:
if (g->state[g->depth] == yajl_gen_map_key || \
g->state[g->depth] == yajl_gen_in_array) { \
g->print(g->ctx, ",", 1); \
if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1); \
} else if (g->state[g->depth] == yajl_gen_map_val) { \
g->print(g->ctx, ":", 1); \
if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, " ", 1); \
}
if(yy_init)
Definition: scan.c:972

Definition at line 134 of file yajl_gen.c.

#define INSERT_WHITESPACE
Value:
if ((g->flags & yajl_gen_beautify)) { \
if (g->state[g->depth] != yajl_gen_map_val) { \
unsigned int _i; \
for (_i=0;_i<g->depth;_i++) \
g->print(g->ctx, \
g->indentString, \
(unsigned int)strlen(g->indentString)); \
} \
}
if(yy_init)
Definition: scan.c:972

Definition at line 144 of file yajl_gen.c.

Enumeration Type Documentation

Enumerator
yajl_gen_start 
yajl_gen_map_start 
yajl_gen_map_key 
yajl_gen_map_val 
yajl_gen_array_start 
yajl_gen_in_array 
yajl_gen_complete 
yajl_gen_error 

Definition at line 27 of file yajl_gen.c.

Function Documentation

yajl_gen yajl_gen_alloc ( const yajl_alloc_funcs allocFuncs)

Allocate a generator handle

Parameters
allocFuncsAn optional pointer to a structure which allows the client to overide the memory allocation used by yajl. May be NULL, in which case malloc(), free() and realloc() will be used.
Returns
an allocated handle on success, NULL on failure (bad params)

Definition at line 97 of file yajl_gen.c.

98 {
99  yajl_gen g = NULL;
100  yajl_alloc_funcs afsBuffer;
101 
102  /* first order of business is to set up memory allocation routines */
103  if (afs != NULL) {
104  if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
105  {
106  return NULL;
107  }
108  } else {
109  yajl_set_default_alloc_funcs(&afsBuffer);
110  afs = &afsBuffer;
111  }
112 
113  g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
114  if (!g) return NULL;
115 
116  memset((void *) g, 0, sizeof(struct yajl_gen_t));
117  /* copy in pointers to allocation routines */
118  memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
119 
121  g->ctx = yajl_buf_alloc(&(g->alloc));
122  g->indentString = " ";
123 
124  return g;
125 }
struct yajl_gen_t * yajl_gen
Definition: yajl_gen.h:59
yajl_free_func free
Definition: yajl_common.h:75
#define YA_MALLOC(afs, sz)
Definition: yajl_alloc.h:32
yajl_buf yajl_buf_alloc(yajl_alloc_funcs *alloc)
Definition: yajl_buf.c:56
#define NULL
Definition: catime.c:38
void yajl_set_default_alloc_funcs(yajl_alloc_funcs *yaf)
Definition: yajl_alloc.c:43
yajl_realloc_func realloc
Definition: yajl_common.h:72
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
yajl_alloc_funcs alloc
Definition: yajl_gen.c:47
yajl_print_t print
Definition: yajl_gen.c:44
const char * indentString
Definition: yajl_gen.c:42
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
yajl_malloc_func malloc
Definition: yajl_common.h:70
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_array_close ( yajl_gen  hand)

Finish generating a JSON array.

Definition at line 318 of file yajl_gen.c.

319 {
322  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
325  g->print(g->ctx, "]", 1);
327  return yajl_gen_status_ok;
328 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define DECREMENT_DEPTH
Definition: yajl_gen.c:173
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_array_open ( yajl_gen  hand)

Start generating a JSON array.

Definition at line 306 of file yajl_gen.c.

307 {
311  g->print(g->ctx, "[", 1);
312  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
314  return yajl_gen_status_ok;
315 }
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
yajl_gen_state state[YAJL_MAX_DEPTH]
Definition: yajl_gen.c:43
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define INCREMENT_DEPTH
Definition: yajl_gen.c:170
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
unsigned int depth
Definition: yajl_gen.c:41
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_bool ( yajl_gen  hand,
int  boolean 
)

Generate a true or false value from boolean.

Definition at line 267 of file yajl_gen.c.

268 {
269  const char * val = boolean ? "true" : "false";
270 
272  g->print(g->ctx, val, (unsigned int)strlen(val));
275  return yajl_gen_status_ok;
276 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void * ctx
Definition: yajl_gen.c:45
void yajl_gen_clear ( yajl_gen  hand)

Clear yajl's output buffer, but maintain all internal generation state. This function will not "reset" the generator state, and is intended to enable incremental JSON outputing.

Definition at line 341 of file yajl_gen.c.

342 {
344 }
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
void yajl_buf_clear(yajl_buf buf)
Definition: yajl_buf.c:86
yajl_print_t print
Definition: yajl_gen.c:44
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
void * ctx
Definition: yajl_gen.c:45
int yajl_gen_config ( yajl_gen  g,
yajl_gen_option  opt,
  ... 
)

Allow the modification of generator options subsequent to handle allocation (via yajl_alloc())

Returns
zero in case of errors, non-zero otherwise

Definition at line 51 of file yajl_gen.c.

52 {
53  int rv = 1;
54  va_list ap;
55  va_start(ap, opt);
56 
57  switch(opt) {
58  case yajl_gen_beautify:
60  if (va_arg(ap, int)) g->flags |= opt;
61  else g->flags &= ~opt;
62  break;
64  const char *indent = va_arg(ap, const char *);
65  g->indentString = indent;
66  for (; *indent; indent++) {
67  if (*indent != '\n'
68  && *indent != '\v'
69  && *indent != '\f'
70  && *indent != '\t'
71  && *indent != '\r'
72  && *indent != ' ')
73  {
74  g->indentString = NULL;
75  rv = 0;
76  }
77  }
78  break;
79  }
81  yajl_buf_free(g->ctx);
82  g->print = va_arg(ap, const yajl_print_t);
83  g->ctx = va_arg(ap, void *);
84  break;
85  default:
86  rv = 0;
87  }
88 
89  va_end(ap);
90 
91  return rv;
92 }
#define NULL
Definition: catime.c:38
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
void yajl_buf_free(yajl_buf buf)
Definition: yajl_buf.c:68
const char * indentString
Definition: yajl_gen.c:42
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_double ( yajl_gen  hand,
double  number 
)

Generate a floating point number. number may not be infinity or NaN, as these have no representation in JSON. In these cases the generator will return yajl_gen_invalid_number

Definition at line 212 of file yajl_gen.c.

213 {
214  char i[32];
216  if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
218  sprintf(i, "%.20g", number);
219  g->print(g->ctx, i, (unsigned int)strlen(i));
222  return yajl_gen_status_ok;
223 }
int i
Definition: scan.c:967
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define isinf(x)
Definition: epicsMath.h:16
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define isnan(x)
Definition: epicsMath.h:21
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void * ctx
Definition: yajl_gen.c:45
void yajl_gen_free ( yajl_gen  handle)

Free a generator handle

Definition at line 128 of file yajl_gen.c.

129 {
131  YA_FREE(&(g->alloc), g);
132 }
#define YA_FREE(afs, ptr)
Definition: yajl_alloc.h:33
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
yajl_alloc_funcs alloc
Definition: yajl_gen.c:47
yajl_print_t print
Definition: yajl_gen.c:44
void yajl_buf_free(yajl_buf buf)
Definition: yajl_buf.c:68
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_get_buf ( yajl_gen  hand,
const unsigned char **  buf,
size_t *  len 
)

Access the null terminated generator buffer. If incrementally outputing JSON, one should call yajl_gen_clear() to clear the buffer. This allows stream generation.

Definition at line 331 of file yajl_gen.c.

333 {
335  *buf = yajl_buf_data((yajl_buf)g->ctx);
336  *len = yajl_buf_len((yajl_buf)g->ctx);
337  return yajl_gen_status_ok;
338 }
size_t yajl_buf_len(yajl_buf buf)
Definition: yajl_buf.c:97
void yajl_buf_append(yajl_buf buf, const void *data, size_t len)
Definition: yajl_buf.c:75
yajl_print_t print
Definition: yajl_gen.c:44
void(* yajl_print_t)(void *ctx, const char *str, size_t len)
Definition: yajl_gen.h:62
const unsigned char * yajl_buf_data(yajl_buf buf)
Definition: yajl_buf.c:92
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_integer ( yajl_gen  hand,
long long int  number 
)

Generate an integer number.

Definition at line 200 of file yajl_gen.c.

201 {
202  char i[32];
204  sprintf(i, "%lld", number);
205  g->print(g->ctx, i, (unsigned int)strlen(i));
208  return yajl_gen_status_ok;
209 }
int i
Definition: scan.c:967
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_map_close ( yajl_gen  hand)

Finish generating a JSON map.

Definition at line 292 of file yajl_gen.c.

293 {
296 
297  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
300  g->print(g->ctx, "}", 1);
302  return yajl_gen_status_ok;
303 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
#define DECREMENT_DEPTH
Definition: yajl_gen.c:173
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_map_open ( yajl_gen  hand)

Start generating a JSON map. This should be followed by calls to yajl_gen_string() to provide a key and another yajl_gen routine to provide the value associated with that key.

Definition at line 279 of file yajl_gen.c.

280 {
283 
284  g->state[g->depth] = yajl_gen_map_start;
285  g->print(g->ctx, "{", 1);
286  if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
288  return yajl_gen_status_ok;
289 }
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
yajl_gen_state state[YAJL_MAX_DEPTH]
Definition: yajl_gen.c:43
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define INCREMENT_DEPTH
Definition: yajl_gen.c:170
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
unsigned int depth
Definition: yajl_gen.c:41
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_null ( yajl_gen  hand)

Generate a null value.

Definition at line 257 of file yajl_gen.c.

258 {
260  g->print(g->ctx, "null", strlen("null"));
263  return yajl_gen_status_ok;
264 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_number ( yajl_gen  hand,
const char *  num,
size_t  len 
)

Generate a number from the string given in num.

Definition at line 226 of file yajl_gen.c.

227 {
229  g->print(g->ctx, s, l);
232  return yajl_gen_status_ok;
233 }
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define ENSURE_NOT_KEY
Definition: yajl_gen.c:155
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void * ctx
Definition: yajl_gen.c:45
yajl_gen_status yajl_gen_string ( yajl_gen  hand,
const unsigned char *  str,
size_t  len 
)

Generate a string value or map key from str.

Definition at line 236 of file yajl_gen.c.

238 {
239  // if validation is enabled, check that the string is valid utf8
240  // XXX: This checking could be done a little faster, in the same pass as
241  // the string encoding
242  if (g->flags & yajl_gen_validate_utf8) {
243  if (!yajl_string_validate_utf8(str, len)) {
245  }
246  }
248  g->print(g->ctx, "\"", 1);
250  g->print(g->ctx, "\"", 1);
253  return yajl_gen_status_ok;
254 }
int yajl_string_validate_utf8(const unsigned char *s, size_t len)
Definition: yajl_encode.c:182
#define APPENDED_ATOM
Definition: yajl_gen.c:176
#define FINAL_NEWLINE
Definition: yajl_gen.c:195
#define INSERT_WHITESPACE
Definition: yajl_gen.c:144
#define str(v)
#define ENSURE_VALID_STATE
Definition: yajl_gen.c:163
unsigned int flags
Definition: yajl_gen.c:40
yajl_print_t print
Definition: yajl_gen.c:44
#define INSERT_SEP
Definition: yajl_gen.c:134
void yajl_string_encode(const yajl_print_t print, void *ctx, const unsigned char *str, size_t len, int escape_solidus)
Definition: yajl_encode.c:32
void * ctx
Definition: yajl_gen.c:45