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

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

fixes for the copy function within the core rappture library. this fix also includes updates to the rappture library flavor of the put command and cleans up some commented out code.

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
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                                RpLibrary* fromObj,
70                                std::string fromPath);
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 = "") const;
81
82        /*
83         * Should return some kind of RpError object
84        RpLibrary&  get       ( std::string path = "",
85                                std::string retVal = "",
86                                int translateFlag = RPLIB_TRANSLATE);
87
88        RpLibrary&  get       ( std::string path = "",
89                                double retVal = 0.0,
90                                int translateFlag = RPLIB_TRANSLATE);
91
92        RpLibrary&  get       ( std::string path = "",
93                                int retVal = 0,
94                                int translateFlag = RPLIB_TRANSLATE);
95
96        RpLibrary&  get       ( std::string path = "",
97                                bool retVal = false,
98                                int translateFlag = RPLIB_TRANSLATE);
99        */
100
101        RpLibrary& put (    std::string path,
102                            std::string value,
103                            std::string id = "",
104                            unsigned int append = RPLIB_OVERWRITE,
105                            unsigned int translateFlag = RPLIB_TRANSLATE   );
106
107        RpLibrary& put (    std::string path,
108                            double value,
109                            std::string id = "",
110                            unsigned int append = RPLIB_OVERWRITE    );
111
112        RpLibrary& put (    std::string path,
113                            RpLibrary* value,
114                            std::string id = "",
115                            unsigned int append = RPLIB_OVERWRITE    );
116
117        RpLibrary& putData( std::string path,
118                            const char* bytes,
119                            int nbytes,
120                            unsigned int append = RPLIB_OVERWRITE    );
121
122        RpLibrary& putFile( std::string path,
123                            std::string fileName,
124                            unsigned int compress = RPLIB_COMPRESS,
125                            unsigned int append = RPLIB_OVERWRITE    );
126
127        RpLibrary* remove (std::string path = "");
128
129        std::string xml() const;
130
131        std::string nodeType() const;
132        std::string nodeId() const;
133        std::string nodeComp() const;
134        std::string nodePath() const;
135
136        Rappture::Outcome& outcome() const;
137
138        void result(int exitStatus=0);
139
140
141        RpLibrary ();
142        RpLibrary (const std::string filePath);
143        RpLibrary (const RpLibrary& other);
144        RpLibrary& operator= (const RpLibrary& other);
145        virtual ~RpLibrary ();
146
147    private:
148
149        scew_parser* parser;
150        scew_tree* tree;
151        scew_element* root;
152
153        // flag to tell if we are responsible for calling scew_tree_free
154        // on the tree structure. if we get our tree by using the
155        // scew_tree_create
156        // fxn, we need to free it. if we get our tree using the
157        // scew_parser_tree
158        // fxn, then it will be free'd when the parser is free'd.
159        int freeTree;
160
161        // some object (like those returned from children() and element )
162        // are previously allocated scew objects with an artificial RpLibrary
163        // wrapper. these objects never really allocated any memory, they
164        // just point to memory. they should not free any memory when they
165        // are deleted (because none was allocated when they were created).
166        // when the larger Rappture Library is deleted, they will have
167        // pointers to bad/deleted memory...libscew has the same problem in
168        // their implementation of a similar children() fxn.
169        //
170        // this flag tells the destructor that when above said object is
171        // deleted, dont act on the root pointer
172        int freeRoot;
173
174        mutable Rappture::Outcome status;
175
176        RpLibrary ( scew_element* node, scew_tree* tree)
177            :   parser      (NULL),
178                tree        (tree),
179                root        (node)
180
181        {
182            freeTree = 0;
183            freeRoot = 0;
184        }
185
186
187        std::string _get_attribute (scew_element* element,
188                                    std::string attributeName) const;
189        int _path2list (std::string& path,
190                        std::string** list,
191                        int listLen) const;
192        std::string _node2name (scew_element* node) const;
193        std::string _node2comp (scew_element* node) const;
194        std::string _node2path (scew_element* node) const;
195
196        int _splitPath (std::string& path,
197                        std::string& tagName,
198                        int* idx,
199                        std::string& id ) const;
200        scew_element* _find (std::string path, int create) const;
201        int _checkPathConflict (scew_element *nodeA, scew_element *nodeB) const;
202        void print_indent ( unsigned int indent,
203                            std::stringstream& outString) const;
204        void print_attributes ( scew_element* element,
205                                std::stringstream& outString) const;
206        void print_element( scew_element* element,
207                            unsigned int indent,
208                            std::stringstream& outString ) const;
209
210};
211
212/*--------------------------------------------------------------------------*/
213/*--------------------------------------------------------------------------*/
214
215#endif // ifdef __cplusplus
216
217#endif // ifndef _RpLIBRARY_H
Note: See TracBrowser for help on using the repository browser.