Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

qcschema_json.h

Go to the documentation of this file.
00001 
00002 /* vim: set et ts=3 sw=3 sts=3 ft=c:
00003  *
00004  * Copyright (C) 2012, 2013, 2014 James McLaughlin et al.  All rights reserved.
00005  * https://github.com/udp/json-parser
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *   notice, this list of conditions and the following disclaimer.
00013  *
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *   notice, this list of conditions and the following disclaimer in the
00016  *   documentation and/or other materials provided with the distribution.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  */
00030 
00031 #ifndef _QCSCHEMA_JSON_H
00032 #define _QCSCHEMA_JSON_H
00033 
00034 #ifndef json_char
00035    #define json_char char
00036 #endif
00037 
00038 #ifndef json_int_t
00039    #ifndef _MSC_VER
00040       #include <inttypes.h>
00041       #define json_int_t int64_t
00042    #else
00043       #define json_int_t __int64
00044    #endif
00045 #endif
00046 
00047 #include <stdlib.h>
00048 
00049 #ifdef __cplusplus
00050 
00051    #include <string.h>
00052 
00053    extern "C"
00054    {
00055 
00056 #endif
00057 
00058 typedef struct
00059 {
00060    unsigned long max_memory;
00061    int settings;
00062 
00063    /* Custom allocator support (leave null to use malloc/free)
00064     */
00065 
00066    void * (* mem_alloc) (size_t, int zero, void * user_data);
00067    void (* mem_free) (void *, void * user_data);
00068 
00069    void * user_data;  /* will be passed to mem_alloc and mem_free */
00070 
00071    size_t value_extra;  /* how much extra space to allocate for values? */
00072 
00073 } json_settings;
00074 
00075 #define json_enable_comments  0x01
00076 
00077 typedef enum
00078 {
00079    json_none,
00080    json_object,
00081    json_array,
00082    json_integer,
00083    json_double,
00084    json_string,
00085    json_boolean,
00086    json_null
00087 
00088 } json_type;
00089 
00090 extern const struct _json_value json_value_none;
00091        
00092 typedef struct _json_object_entry
00093 {
00094     json_char * name;
00095     unsigned int name_length;
00096     
00097     struct _json_value * value;
00098     
00099 } json_object_entry;
00100 
00101 typedef struct _json_value
00102 {
00103    struct _json_value * parent;
00104 
00105    json_type type;
00106 
00107    union
00108    {
00109       int boolean;
00110       json_int_t integer;
00111       double dbl;
00112 
00113       struct
00114       {
00115          unsigned int length;
00116          json_char * ptr; /* null terminated */
00117 
00118       } string;
00119 
00120       struct
00121       {
00122          unsigned int length;
00123 
00124          json_object_entry * values;
00125 
00126          #if defined(__cplusplus) && __cplusplus >= 201103L
00127          decltype(values) begin () const
00128          {  return values;
00129          }
00130          decltype(values) end () const
00131          {  return values + length;
00132          }
00133          #endif
00134 
00135       } object;
00136 
00137       struct
00138       {
00139          unsigned int length;
00140          struct _json_value ** values;
00141 
00142          #if defined(__cplusplus) && __cplusplus >= 201103L
00143          decltype(values) begin () const
00144          {  return values;
00145          }
00146          decltype(values) end () const
00147          {  return values + length;
00148          }
00149          #endif
00150 
00151       } array;
00152 
00153    } u;
00154 
00155    union
00156    {
00157       struct _json_value * next_alloc;
00158       void * object_mem;
00159 
00160    } _reserved;
00161 
00162    #ifdef JSON_TRACK_SOURCE
00163 
00164       /* Location of the value in the source JSON
00165        */
00166       unsigned int line, col;
00167 
00168    #endif
00169 
00170 
00171    /* Some C++ operator sugar */
00172 
00173    #ifdef __cplusplus
00174 
00175       public:
00176 
00177          inline _json_value ()
00178          {  memset (this, 0, sizeof (_json_value));
00179          }
00180 
00181          inline const struct _json_value &operator [] (int index) const
00182          {
00183             if (type != json_array || index < 0
00184                      || ((unsigned int) index) >= u.array.length)
00185             {
00186                return json_value_none;
00187             }
00188 
00189             return *u.array.values [index];
00190          }
00191 
00192          inline const struct _json_value &operator [] (const char * index) const
00193          { 
00194             if (type != json_object)
00195                return json_value_none;
00196 
00197             for (unsigned int i = 0; i < u.object.length; ++ i)
00198                if (!strcmp (u.object.values [i].name, index))
00199                   return *u.object.values [i].value;
00200 
00201             return json_value_none;
00202          }
00203 
00204          inline operator const char * () const
00205          {  
00206             switch (type)
00207             {
00208                case json_string:
00209                   return u.string.ptr;
00210 
00211                default:
00212                   return "";
00213             };
00214          }
00215 
00216          inline operator json_int_t () const
00217          {  
00218             switch (type)
00219             {
00220                case json_integer:
00221                   return u.integer;
00222 
00223                case json_double:
00224                   return (json_int_t) u.dbl;
00225 
00226                default:
00227                   return 0;
00228             };
00229          }
00230 
00231          inline operator bool () const
00232          {  
00233             if (type != json_boolean)
00234                return false;
00235 
00236             return u.boolean != 0;
00237          }
00238 
00239          inline operator double () const
00240          {  
00241             switch (type)
00242             {
00243                case json_integer:
00244                   return (double) u.integer;
00245 
00246                case json_double:
00247                   return u.dbl;
00248 
00249                default:
00250                   return 0;
00251             };
00252          }
00253 
00254    #endif
00255 
00256 } json_value;
00257        
00258 json_value * json_parse (const json_char * json,
00259                          size_t length);
00260 
00261 #define json_error_max 128
00262 json_value * json_parse_ex (json_settings * settings,
00263                             const json_char * json,
00264                             size_t length,
00265                             char * error);
00266 
00267 void json_value_free (json_value *);
00268 
00269 
00270 /* Not usually necessary, unless you used a custom mem_alloc and now want to
00271  * use a custom mem_free.
00272  */
00273 void json_value_free_ex (json_settings * settings,
00274                          json_value *);
00275 
00276 
00277 #ifdef __cplusplus
00278    } /* extern "C" */
00279 #endif
00280 
00281 #endif
00282 
00283 

Generated on Fri Mar 29 03:10:17 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002