|
| 1 | +/* |
| 2 | + * Copyright 2010, R. Tyler Ballance <[email protected]> |
| 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 R. Tyler Ballance 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 | +#include <yajl_encode.h> |
| 33 | + |
| 34 | + |
| 35 | +/* |
| 36 | + * This code was yanked largely from yajl_gen.c |
| 37 | + * it is merely a set of hacks |
| 38 | + */ |
| 39 | + |
| 40 | +typedef enum { |
| 41 | + yajl_gen_start, |
| 42 | + yajl_gen_map_start, |
| 43 | + yajl_gen_map_key, |
| 44 | + yajl_gen_map_val, |
| 45 | + yajl_gen_array_start, |
| 46 | + yajl_gen_in_array, |
| 47 | + yajl_gen_complete, |
| 48 | + yajl_gen_error |
| 49 | +} yajl_gen_state; |
| 50 | + |
| 51 | +struct yajl_gen_t |
| 52 | +{ |
| 53 | + unsigned int depth; |
| 54 | + unsigned int pretty; |
| 55 | + const char * indentString; |
| 56 | + yajl_gen_state state[YAJL_MAX_DEPTH]; |
| 57 | + yajl_print_t print; |
| 58 | + void * ctx; /* yajl_buf */ |
| 59 | + /* memory allocation routines */ |
| 60 | + yajl_alloc_funcs alloc; |
| 61 | +}; |
| 62 | + |
| 63 | +#define INSERT_SEP \ |
| 64 | + if (g->state[g->depth] == yajl_gen_map_key || \ |
| 65 | + g->state[g->depth] == yajl_gen_in_array) { \ |
| 66 | + g->print(g->ctx, ",", 1); \ |
| 67 | + if (g->pretty) g->print(g->ctx, "\n", 1); \ |
| 68 | + } else if (g->state[g->depth] == yajl_gen_map_val) { \ |
| 69 | + g->print(g->ctx, ":", 1); \ |
| 70 | + if (g->pretty) g->print(g->ctx, " ", 1); \ |
| 71 | + } |
| 72 | + |
| 73 | +#define INSERT_WHITESPACE \ |
| 74 | + if (g->pretty) { \ |
| 75 | + if (g->state[g->depth] != yajl_gen_map_val) { \ |
| 76 | + unsigned int _i; \ |
| 77 | + for (_i=0;_i<g->depth;_i++) \ |
| 78 | + g->print(g->ctx, g->indentString, \ |
| 79 | + strlen(g->indentString)); \ |
| 80 | + } \ |
| 81 | + } |
| 82 | +/* check that we're not complete, or in error state. in a valid state |
| 83 | + * to be generating */ |
| 84 | +#define ENSURE_VALID_STATE \ |
| 85 | + if (g->state[g->depth] == yajl_gen_error) { \ |
| 86 | + return yajl_gen_in_error_state;\ |
| 87 | + } else if (g->state[g->depth] == yajl_gen_complete) { \ |
| 88 | + return yajl_gen_generation_complete; \ |
| 89 | + } |
| 90 | + |
| 91 | +#define APPENDED_ATOM \ |
| 92 | + switch (g->state[g->depth]) { \ |
| 93 | + case yajl_gen_start: \ |
| 94 | + g->state[g->depth] = yajl_gen_complete; \ |
| 95 | + break; \ |
| 96 | + case yajl_gen_map_start: \ |
| 97 | + case yajl_gen_map_key: \ |
| 98 | + g->state[g->depth] = yajl_gen_map_val; \ |
| 99 | + break; \ |
| 100 | + case yajl_gen_array_start: \ |
| 101 | + g->state[g->depth] = yajl_gen_in_array; \ |
| 102 | + break; \ |
| 103 | + case yajl_gen_map_val: \ |
| 104 | + g->state[g->depth] = yajl_gen_map_key; \ |
| 105 | + break; \ |
| 106 | + default: \ |
| 107 | + break; \ |
| 108 | + } \ |
| 109 | + |
| 110 | +#define FINAL_NEWLINE \ |
| 111 | + if (g->pretty && g->state[g->depth] == yajl_gen_complete) \ |
| 112 | + g->print(g->ctx, "\n", 1); |
| 113 | + |
| 114 | +yajl_gen_status yajl_gen_raw_string(yajl_gen g, const unsigned char * str, unsigned int len) |
| 115 | +{ |
| 116 | + ENSURE_VALID_STATE; INSERT_SEP; INSERT_WHITESPACE; |
| 117 | + g->print(g->ctx, "\"", 1); |
| 118 | + g->print(g->ctx, str, len); |
| 119 | + g->print(g->ctx, "\"", 1); |
| 120 | + APPENDED_ATOM; |
| 121 | + FINAL_NEWLINE; |
| 122 | + return yajl_gen_status_ok; |
| 123 | +} |
0 commit comments