Logo Search packages:      
Sourcecode: yajl version File versions  Download package

YAJL_API yajl_gen yajl_gen_alloc2 ( const yajl_print_t  callback,
const yajl_gen_config config,
const yajl_alloc_funcs allocFuncs,
void *  ctx 
)

allocate a generator handle that will print to the specified callback rather than storing the results in an internal buffer.

Parameters:
callbacka pointer to a printer function. May be NULL in which case, the results will be store in an internal buffer.
configa pointer to a structure containing parameters which configure the behavior of the json generator.
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/realloc will be used.
ctxa context pointer that will be passed to the printer callback.
Returns:
an allocated handle on success, NULL on failure (bad params)

Definition at line 73 of file yajl_gen.c.

References yajl_gen_config::beautify, yajl_alloc_funcs::free, yajl_gen_config::indentString, yajl_alloc_funcs::malloc, yajl_alloc_funcs::realloc, and yajl_gen_alloc2().

Referenced by yajl_gen_alloc(), and yajl_gen_alloc2().

{
    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;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index