source: trunk/src/core/RpLibrary.h @ 1427

Last change on this file since 1427 was 1041, checked in by gah, 16 years ago

hide scew from external rappture interface

File size: 7.8 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Header
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick S. Kearney, Purdue University
7 *  Copyright (c) 2004-2007  Purdue Research Foundation
8 *
9 *  See the file "license.terms" for information on usage and
10 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * ======================================================================
12 */
13
14#ifndef _RpLIBRARY_H
15#define _RpLIBRARY_H
16
17enum RP_LIBRARY_CONSTS {
18    RPLIB_OVERWRITE     = 0,
19    RPLIB_APPEND        = 1,
20    RPLIB_NO_TRANSLATE  = 0,
21    RPLIB_TRANSLATE     = 1,
22    RPLIB_NO_COMPRESS   = 0,
23    RPLIB_COMPRESS      = 1,
24};
25
26
27#ifdef __cplusplus
28
29typedef struct _scew_parser scew_parser;
30typedef struct _scew_tree scew_tree;
31typedef struct _scew_element scew_element;
32
33#include <list>
34#include "RpBuffer.h"
35#include "RpOutcome.h"
36
37/* indentation size (in whitespaces) */
38
39#define INDENT_SIZE 4
40#define CREATE_PATH 1
41#define NO_CREATE_PATH 0
42
43class RpLibrary
44{
45    public:
46
47        // users member fxns
48
49        RpLibrary* element (std::string path = "") const;
50        RpLibrary* parent (std::string path = "") const;
51
52        std::list<std::string> entities  (std::string path = "") const;
53        std::list<std::string> value     (std::string path = "") const;
54        std::list<std::string> diff      ( RpLibrary* otherLib,
55                                            std::string path) const;
56
57        bool isNull() const;
58
59        // should return RpObject& but for simplicity right now it doesnt
60        // RpObject will either be an Array, RpString, RpNumber ...
61
62        RpLibrary*  children  ( std::string path = "",
63                                RpLibrary* rpChildNode = NULL,
64                                std::string type = "",
65                                int* childCount = NULL  );
66
67        RpLibrary& childCount ( std::string path,
68                                int* childCount);
69
70        RpLibrary& copy       ( std::string toPath,
71                                RpLibrary* fromObj,
72                                std::string fromPath);
73
74        std::string get       ( std::string path = "",
75                                int translateFlag = RPLIB_TRANSLATE) const;
76        std::string getString ( std::string path = "",
77                                int translateFlag = RPLIB_TRANSLATE) const;
78
79        double      getDouble ( std::string path = "") const;
80        int         getInt    ( std::string path = "") const;
81        bool        getBool   ( std::string path = "") const;
82        Rappture::Buffer getData ( std::string path = "") const;
83
84        /*
85         * Should return some kind of RpError object
86        RpLibrary&  get       ( std::string path = "",
87                                std::string retVal = "",
88                                int translateFlag = RPLIB_TRANSLATE);
89
90        RpLibrary&  get       ( std::string path = "",
91                                double retVal = 0.0,
92                                int translateFlag = RPLIB_TRANSLATE);
93
94        RpLibrary&  get       ( std::string path = "",
95                                int retVal = 0,
96                                int translateFlag = RPLIB_TRANSLATE);
97
98        RpLibrary&  get       ( std::string path = "",
99                                bool retVal = false,
100                                int translateFlag = RPLIB_TRANSLATE);
101        */
102
103        RpLibrary& put (    std::string path,
104                            std::string value,
105                            std::string id = "",
106                            unsigned int append = RPLIB_OVERWRITE,
107                            unsigned int translateFlag = RPLIB_TRANSLATE   );
108
109        RpLibrary& put (    std::string path,
110                            double value,
111                            std::string id = "",
112                            unsigned int append = RPLIB_OVERWRITE    );
113
114        RpLibrary& put (    std::string path,
115                            RpLibrary* value,
116                            std::string id = "",
117                            unsigned int append = RPLIB_OVERWRITE    );
118
119        RpLibrary& putData( std::string path,
120                            const char* bytes,
121                            int nbytes,
122                            unsigned int append = RPLIB_OVERWRITE    );
123
124        RpLibrary& putFile( std::string path,
125                            std::string fileName,
126                            unsigned int compress = RPLIB_COMPRESS,
127                            unsigned int append = RPLIB_OVERWRITE    );
128
129        RpLibrary* remove (std::string path = "");
130
131        std::string xml() const;
132
133        std::string nodeType() const;
134        std::string nodeId() const;
135        std::string nodeComp() const;
136        std::string nodePath() const;
137
138        Rappture::Outcome& outcome() const;
139
140        void result(int exitStatus=0);
141
142
143        RpLibrary ();
144        RpLibrary (const std::string filePath);
145        RpLibrary (const RpLibrary& other);
146        RpLibrary& operator= (const RpLibrary& other);
147        virtual ~RpLibrary ();
148
149    private:
150
151        scew_parser* parser;
152        scew_tree* tree;
153        scew_element* root;
154
155        // flag to tell if we are responsible for calling scew_tree_free
156        // on the tree structure. if we get our tree by using the
157        // scew_tree_create
158        // fxn, we need to free it. if we get our tree using the
159        // scew_parser_tree
160        // fxn, then it will be free'd when the parser is free'd.
161        int freeTree;
162
163        // some object (like those returned from children() and element )
164        // are previously allocated scew objects with an artificial RpLibrary
165        // wrapper. these objects never really allocated any memory, they
166        // just point to memory. they should not free any memory when they
167        // are deleted (because none was allocated when they were created).
168        // when the larger Rappture Library is deleted, they will have
169        // pointers to bad/deleted memory...libscew has the same problem in
170        // their implementation of a similar children() fxn.
171        //
172        // this flag tells the destructor that when above said object is
173        // deleted, dont act on the root pointer
174        int freeRoot;
175
176        mutable Rappture::Outcome status;
177
178        RpLibrary ( scew_element* node, scew_tree* tree)
179            :   parser      (NULL),
180                tree        (tree),
181                root        (node)
182
183        {
184            freeTree = 0;
185            freeRoot = 0;
186        }
187
188
189        std::string _get_attribute (scew_element* element,
190                                    std::string attributeName) const;
191        int _path2list (std::string& path,
192                        std::string** list,
193                        int listLen) const;
194        std::string _node2name (scew_element* node) const;
195        std::string _node2comp (scew_element* node) const;
196        std::string _node2path (scew_element* node) const;
197
198        int _splitPath (std::string& path,
199                        std::string& tagName,
200                        int* idx,
201                        std::string& id ) const;
202        scew_element* _find (std::string path, int create) const;
203        int _checkPathConflict (scew_element *nodeA, scew_element *nodeB) const;
204        void print_indent ( unsigned int indent,
205                            std::stringstream& outString) const;
206        void print_attributes ( scew_element* element,
207                                std::stringstream& outString) const;
208        void print_element( scew_element* element,
209                            unsigned int indent,
210                            std::stringstream& outString ) const;
211
212};
213
214/*--------------------------------------------------------------------------*/
215/*--------------------------------------------------------------------------*/
216
217#endif // ifdef __cplusplus
218
219#endif // ifndef _RpLIBRARY_H
Note: See TracBrowser for help on using the repository browser.