oculus1

diff libovr/Src/OVR_JSON.h @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/OVR_JSON.h	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,143 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +PublicHeader:   None
     1.7 +Filename    :   OVR_JSON.h
     1.8 +Content     :   JSON format reader and writer
     1.9 +Created     :   April 9, 2013
    1.10 +Author      :   Brant Lewis
    1.11 +Notes       :
    1.12 +
    1.13 +Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
    1.14 +
    1.15 +Use of this software is subject to the terms of the Oculus license
    1.16 +agreement provided at the time of installation or download, or which
    1.17 +otherwise accompanies this software in either electronic or hard copy form.
    1.18 +
    1.19 +************************************************************************************/
    1.20 +
    1.21 +#ifndef OVR_JSON_H
    1.22 +#define OVR_JSON_H
    1.23 +
    1.24 +#include "Kernel/OVR_RefCount.h"
    1.25 +#include "Kernel/OVR_String.h"
    1.26 +#include "Kernel/OVR_List.h"
    1.27 +
    1.28 +namespace OVR {  
    1.29 +
    1.30 +// JSONItemType describes the type of JSON item, specifying the type of
    1.31 +// data that can be obtained from it.
    1.32 +enum JSONItemType
    1.33 +{
    1.34 +    JSON_None      = 0,
    1.35 +    JSON_Null      = 1,
    1.36 +    JSON_Bool      = 2,
    1.37 +    JSON_Number    = 3,
    1.38 +    JSON_String    = 4,
    1.39 +    JSON_Array     = 5,
    1.40 +    JSON_Object    = 6
    1.41 +};
    1.42 +
    1.43 +
    1.44 +//-----------------------------------------------------------------------------
    1.45 +// ***** JSON
    1.46 +
    1.47 +// JSON object represents a JSON node that can be either a root of the JSON tree
    1.48 +// or a child item. Every node has a type that describes what is is.
    1.49 +// New JSON trees are typically loaded JSON::Load or created with JSON::Parse.
    1.50 +
    1.51 +class JSON : public RefCountBase<JSON>, public ListNode<JSON>
    1.52 +{
    1.53 +protected:
    1.54 +    List<JSON>      Children;
    1.55 +
    1.56 +public:
    1.57 +    JSONItemType    Type;       // Type of this JSON node.
    1.58 +    String          Name;       // Name part of the {Name, Value} pair in a parent object.
    1.59 +    String          Value;
    1.60 +    double          dValue;
    1.61 +
    1.62 +public:
    1.63 +    ~JSON();
    1.64 +
    1.65 +    // *** Creation of NEW JSON objects
    1.66 +
    1.67 +    static JSON*    CreateObject()               { return new JSON(JSON_Object);}
    1.68 +    static JSON*    CreateNull()                 { return new JSON(JSON_Null); }
    1.69 +    static JSON*    CreateArray()                { return new JSON(JSON_Array); }
    1.70 +    static JSON*    CreateBool(bool b)           { return createHelper(JSON_Bool, b ? 1.0 : 0.0); }
    1.71 +    static JSON*    CreateNumber(double num)     { return createHelper(JSON_Number, num); }
    1.72 +    static JSON*    CreateString(const char *s)  { return createHelper(JSON_String, 0.0, s); }
    1.73 +
    1.74 +    // Creates a new JSON object from parsing string.
    1.75 +    // Returns null pointer and fills in *perror in case of parse error.
    1.76 +    static JSON*    Parse(const char* buff, const char** perror = 0);
    1.77 +
    1.78 +    // Loads and parses a JSON object from a file.
    1.79 +    // Returns 0 and assigns perror with error message on fail.
    1.80 +    static JSON*    Load(const char* path, const char** perror = 0);
    1.81 +
    1.82 +    // Saves a JSON object to a file.
    1.83 +    bool            Save(const char* path);
    1.84 +
    1.85 +
    1.86 +    // *** Object Member Access
    1.87 +
    1.88 +    // These provide access to child items of the list.
    1.89 +    bool            HasItems() const         { return Children.IsEmpty(); }
    1.90 +    // Returns first/last child item, or null if child list is empty
    1.91 +    JSON*           GetFirstItem()           { return (!Children.IsEmpty()) ? Children.GetFirst() : 0; }
    1.92 +    JSON*           GetLastItem()            { return (!Children.IsEmpty()) ? Children.GetLast() : 0; }
    1.93 +
    1.94 +    // Counts the number of items in the object; these methods are inefficient.
    1.95 +    unsigned        GetItemCount() const;
    1.96 +    JSON*           GetItemByIndex(unsigned i);
    1.97 +    JSON*           GetItemByName(const char* name);
    1.98 +
    1.99 +    // Returns next item in a list of children; 0 if no more items exist.
   1.100 +    JSON*           GetNextItem(JSON* item)  { return Children.IsNull(item->pNext) ? 0 : item->pNext; }
   1.101 +    JSON*           GetPrevItem(JSON* item)  { return Children.IsNull(item->pPrev) ? 0 : item->pPrev; }
   1.102 +
   1.103 +
   1.104 +    // Child item access functions
   1.105 +    void            AddItem(const char *string, JSON* item);
   1.106 +    void            AddNullItem(const char* name)                    { AddItem(name, CreateNull()); }
   1.107 +    void            AddBoolItem(const char* name, bool b)            { AddItem(name, CreateBool(b)); }
   1.108 +    void            AddNumberItem(const char* name, double n)        { AddItem(name, CreateNumber(n)); }
   1.109 +    void            AddStringItem(const char* name, const char* s)   { AddItem(name, CreateString(s)); }
   1.110 +//    void            ReplaceItem(unsigned index, JSON* new_item);
   1.111 +//    void            DeleteItem(unsigned index);
   1.112 +
   1.113 +    // *** Array Element Access
   1.114 +
   1.115 +    // Add new elements to the end of array.
   1.116 +    void            AddArrayElement(JSON *item);
   1.117 +    void            AddArrayNumber(double n)        { AddArrayElement(CreateNumber(n)); }
   1.118 +    void            AddArrayString(const char* s)   { AddArrayElement(CreateString(s)); }
   1.119 +
   1.120 +    // Accessed array elements; currently inefficient.
   1.121 +    int             GetArraySize();
   1.122 +    double          GetArrayNumber(int index);
   1.123 +    const char*     GetArrayString(int index);
   1.124 +
   1.125 +
   1.126 +protected:
   1.127 +    JSON(JSONItemType itemType = JSON_Object);
   1.128 +
   1.129 +    static JSON*    createHelper(JSONItemType itemType, double dval, const char* strVal = 0);
   1.130 +
   1.131 +    // JSON Parsing helper functions.
   1.132 +    const char*     parseValue(const char *buff, const char** perror);
   1.133 +    const char*     parseNumber(const char *num);
   1.134 +    const char*     parseArray(const char* value, const char** perror);
   1.135 +    const char*     parseObject(const char* value, const char** perror);
   1.136 +    const char*     parseString(const char* str, const char** perror);
   1.137 +
   1.138 +    char*           PrintValue(int depth, bool fmt);
   1.139 +    char*           PrintObject(int depth, bool fmt);
   1.140 +    char*           PrintArray(int depth, bool fmt);
   1.141 +};
   1.142 +
   1.143 +
   1.144 +}
   1.145 +
   1.146 +#endif