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

Last change on this file since 625 was 625, checked in by dkearney, 17 years ago

moving functions in the library from header to cc file and fixing const correctness of member functions.

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