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

Last change on this file since 2408 was 2408, checked in by dkearney, 11 years ago

adding a getFile function for rappture library and fortran bindings so fortran users no longer need to know the length of a string a user provides them as input, before compile time. they can now just dump the string into a file, and read the file as they are probably already used to doing. also fixed a bug in teh Rappture::Buffer dump function where it would correctly open a file pointer, but failed the check to see if the file pointer was opened.

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