"Fossies" - the Fresh Open Source Software Archive

Member "remind-03.03.09/src/json.h" (15 Oct 2021, 5617 Bytes) of package /linux/misc/remind-03.03.09.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "json.h" see the Fossies "Dox" file reference documentation.

    1 /* vim: set et ts=3 sw=3 sts=3 ft=c:
    2  *
    3  * Copyright (C) 2012, 2013, 2014 James McLaughlin et al.  All rights reserved.
    4  * https://github.com/udp/json-parser
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  *
   10  * 1. Redistributions of source code must retain the above copyright
   11  *   notice, this list of conditions and the following disclaimer.
   12  *
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *   notice, this list of conditions and the following disclaimer in the
   15  *   documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #ifndef _JSON_H
   31 #define _JSON_H
   32 
   33 #ifndef json_char
   34    #define json_char char
   35 #endif
   36 
   37 #ifndef json_int_t
   38    #ifndef _MSC_VER
   39       #include <inttypes.h>
   40       #define json_int_t int64_t
   41    #else
   42       #define json_int_t __int64
   43    #endif
   44 #endif
   45 
   46 #include <stdlib.h>
   47 
   48 #ifdef __cplusplus
   49 
   50    #include <string.h>
   51 
   52    extern "C"
   53    {
   54 
   55 #endif
   56 
   57 typedef struct
   58 {
   59    unsigned long max_memory;
   60    int settings;
   61 
   62    /* Custom allocator support (leave null to use malloc/free)
   63     */
   64 
   65    void * (* mem_alloc) (size_t, int zero, void * user_data);
   66    void (* mem_free) (void *, void * user_data);
   67 
   68    void * user_data;  /* will be passed to mem_alloc and mem_free */
   69 
   70    size_t value_extra;  /* how much extra space to allocate for values? */
   71 
   72 } json_settings;
   73 
   74 #define json_enable_comments  0x01
   75 
   76 typedef enum
   77 {
   78    json_none,
   79    json_object,
   80    json_array,
   81    json_integer,
   82    json_double,
   83    json_string,
   84    json_boolean,
   85    json_null
   86 
   87 } json_type;
   88 
   89 extern const struct _json_value json_value_none;
   90 
   91 typedef struct _json_object_entry
   92 {
   93     json_char * name;
   94     unsigned int name_length;
   95 
   96     struct _json_value * value;
   97 
   98 } json_object_entry;
   99 
  100 typedef struct _json_value
  101 {
  102    struct _json_value * parent;
  103 
  104    json_type type;
  105 
  106    union
  107    {
  108       int boolean;
  109       json_int_t integer;
  110       double dbl;
  111 
  112       struct
  113       {
  114      unsigned int length;
  115      json_char * ptr; /* null terminated */
  116 
  117       } string;
  118 
  119       struct
  120       {
  121      unsigned int length;
  122 
  123      json_object_entry * values;
  124 
  125      #if defined(__cplusplus) && __cplusplus >= 201103L
  126      decltype(values) begin () const
  127      {  return values;
  128      }
  129      decltype(values) end () const
  130      {  return values + length;
  131      }
  132      #endif
  133 
  134       } object;
  135 
  136       struct
  137       {
  138      unsigned int length;
  139      struct _json_value ** values;
  140 
  141      #if defined(__cplusplus) && __cplusplus >= 201103L
  142      decltype(values) begin () const
  143      {  return values;
  144      }
  145      decltype(values) end () const
  146      {  return values + length;
  147      }
  148      #endif
  149 
  150       } array;
  151 
  152    } u;
  153 
  154    union
  155    {
  156       struct _json_value * next_alloc;
  157       void * object_mem;
  158 
  159    } _reserved;
  160 
  161    #ifdef JSON_TRACK_SOURCE
  162 
  163       /* Location of the value in the source JSON
  164        */
  165       unsigned int line, col;
  166 
  167    #endif
  168 
  169 
  170    /* Some C++ operator sugar */
  171 
  172    #ifdef __cplusplus
  173 
  174       public:
  175 
  176      inline _json_value ()
  177      {  memset (this, 0, sizeof (_json_value));
  178      }
  179 
  180      inline const struct _json_value &operator [] (int index) const
  181      {
  182         if (type != json_array || index < 0
  183              || ((unsigned int) index) >= u.array.length)
  184         {
  185            return json_value_none;
  186         }
  187 
  188         return *u.array.values [index];
  189      }
  190 
  191      inline const struct _json_value &operator [] (const char * index) const
  192      {
  193         if (type != json_object)
  194            return json_value_none;
  195 
  196         for (unsigned int i = 0; i < u.object.length; ++ i)
  197            if (!strcmp (u.object.values [i].name, index))
  198           return *u.object.values [i].value;
  199 
  200         return json_value_none;
  201      }
  202 
  203      inline operator const char * () const
  204      {
  205         switch (type)
  206         {
  207            case json_string:
  208           return u.string.ptr;
  209 
  210            default:
  211           return "";
  212         };
  213      }
  214 
  215      inline operator json_int_t () const
  216      {
  217         switch (type)
  218         {
  219            case json_integer:
  220           return u.integer;
  221 
  222            case json_double:
  223           return (json_int_t) u.dbl;
  224 
  225            default:
  226           return 0;
  227         };
  228      }
  229 
  230      inline operator bool () const
  231      {
  232         if (type != json_boolean)
  233            return false;
  234 
  235         return u.boolean != 0;
  236      }
  237 
  238      inline operator double () const
  239      {
  240         switch (type)
  241         {
  242            case json_integer:
  243           return (double) u.integer;
  244 
  245            case json_double:
  246           return u.dbl;
  247 
  248            default:
  249           return 0;
  250         };
  251      }
  252 
  253    #endif
  254 
  255 } json_value;
  256 
  257 json_value * json_parse (const json_char * json,
  258              size_t length);
  259 
  260 #define json_error_max 128
  261 json_value * json_parse_ex (json_settings * settings,
  262                 const json_char * json,
  263                 size_t length,
  264                 char * error);
  265 
  266 void json_value_free (json_value *);
  267 
  268 
  269 /* Not usually necessary, unless you used a custom mem_alloc and now want to
  270  * use a custom mem_free.
  271  */
  272 void json_value_free_ex (json_settings * settings,
  273              json_value *);
  274 
  275 
  276 #ifdef __cplusplus
  277    } /* extern "C" */
  278 #endif
  279 
  280 #endif