source: trunk/include/core/RpLibrary.h @ 83

Last change on this file since 83 was 83, checked in by dkearney, 19 years ago
  1. More cleaning of RpUnits and RpLibrary? code
  2. added rp_result code to c++/fortran/c code
  3. added rp_children, rp_lib_node[comp,type,id] for fortran code (need to test)
  4. adjusted convert function to recognize statements as follows:

convert("5J","neV")
convert("3.12075e+28neV","J")

  1. made joules a metric unit in RpUnits.cc
  2. tested examples/app-fermi/fortran/fermi.f with new rappture library.

added units conversion.

File size: 12.1 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Header
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick Kearney, Purdue University
7 *  Copyright (c) 2005
8 *  Purdue Research Foundation, West Lafayette, IN
9 * ======================================================================
10 */
11
12#include "scew.h"
13#include "scew_extras.h"
14#include <iostream>
15#include <string>
16#include <sstream>
17#include <fstream>
18#include <stdlib.h>
19#include <errno.h>
20#include <time.h>
21
22/* indentation size (in whitespaces) */
23#define INDENT_SIZE 4
24
25#ifndef _RpLIBRARY_H
26#define _RpLIBRARY_H
27
28class RpLibrary
29{
30    public:
31
32        // users member fxns
33
34        RpLibrary* element (std::string path = "", std::string as = "object");
35
36        // should return RpObject& but for simplicity right now it doesnt
37        // RpObject will either be an Array, RpString, RpNumber ...
38
39        RpLibrary*  children (  std::string path = "",
40                                RpLibrary* rpChildNode = NULL,
41                                std::string type = "",
42                                int* childCount = NULL  );
43
44        RpLibrary*  get (std::string path = "");
45        std::string getString (std::string path = "");
46        double      getDouble (std::string path = "");
47
48        RpLibrary& put (    std::string path,
49                            std::string value,
50                            std::string id = "",
51                            int append = 0  );
52
53        RpLibrary& put (    std::string path,
54                            double value,
55                            std::string id = "",
56                            int append = 0  );
57
58        RpLibrary& remove (std::string path = "");
59
60        std::string xml();
61
62        std::string nodeType();
63        std::string nodeId();
64        std::string nodeComp();
65
66        void result();
67        const char* nodeTypeC();
68        const char* nodeIdC();
69        const char* nodeCompC();
70
71        // no arg constructor
72        // used when we dont want to read an xml file to populate the xml tree
73        // we are building a new xml structure
74        RpLibrary ()
75            :   parser      (NULL),
76                tree        (NULL),
77                root        (NULL)
78        {
79            tree = scew_tree_create();
80            freeTree = 1;
81            root = scew_tree_add_root(tree, "run");
82        }
83
84        RpLibrary (
85                    std::string filePath
86                )
87            :   parser      (NULL),
88                tree        (NULL),
89                root        (NULL)
90        {
91
92            if (filePath.length() != 0) {
93                // file path should not be null or empty string unless we are
94                // creating a new xml file
95
96                parser = scew_parser_create();
97
98                scew_parser_ignore_whitespaces(parser, 1);
99
100                /* Loads an XML file */
101                if (!scew_parser_load_file(parser, filePath.c_str()))
102                {
103                    scew_error code = scew_error_code();
104                    printf("Unable to load file (error #%d: %s)\n", code,
105                           scew_error_string(code));
106
107                    /*
108                    std::cout << "Unable to load file (error #" \
109                              << code << ": " << scew_error_string(code) \
110                              << ")\n" << std::endl;
111                    */
112
113                    if (code == scew_error_expat)
114                    {
115                        enum XML_Error expat_code =
116                            scew_error_expat_code(parser);
117                        printf("Expat error #%d (line %d, column %d): %s\n",
118                               expat_code,
119                               scew_error_expat_line(parser),
120                               scew_error_expat_column(parser),
121                               scew_error_expat_string(expat_code));
122                    }
123                    // should probably exit program or something
124                    // return EXIT_FAILURE;
125
126                }
127
128                tree = scew_parser_tree(parser);
129                freeTree = 0;
130                root = scew_tree_root(tree);
131            }
132            else {
133                // create a new xml (from an empty file)
134            }
135        }
136
137
138        // copy constructor
139        // for some reason making this a const gives me problems when calling xml()
140        // need help looking into this
141        // RpLibrary ( const RpLibrary& other )
142        RpLibrary ( RpLibrary& other )
143            : parser    (NULL),
144              tree      (NULL),
145              root      (NULL)
146        {
147            std::string buffer;
148            int buffLen;
149
150            // fill in the current RpLibrary's data with other's data
151            parser = scew_parser_create();
152            scew_parser_ignore_whitespaces(parser, 1);
153
154            // Loads the XML from other
155            // the length cannot be 0 because xml() should not be returning
156            // empty strings
157            buffer = other.xml();
158            buffLen = buffer.length();
159
160            if (buffLen > 0) {
161                if (!scew_parser_load_buffer(parser,buffer.c_str(),buffLen))
162                {
163                    // there was an error loading the buffer
164                    // how do you tell the user, you couldn't make a copy?
165                    scew_error code = scew_error_code();
166                    printf("Unable to load buffer (error #%d: %s)\n", code,
167                           scew_error_string(code));
168
169                    if (code == scew_error_expat)
170                    {
171                        enum XML_Error expat_code =
172                            scew_error_expat_code(parser);
173                        printf("Expat error #%d (line %d, column %d): %s\n",
174                               expat_code,
175                               scew_error_expat_line(parser),
176                               scew_error_expat_column(parser),
177                               scew_error_expat_string(expat_code));
178                    }
179
180                    // return an empty RpLibrary?
181                    // return EXIT_FAILURE;
182
183                    parser = NULL;
184                }
185                else {
186
187                    // parsing the buffer was successful
188                    // populate the new data members.
189
190                    tree = scew_parser_tree(parser);
191                    freeTree = 0;
192                    root = scew_tree_root(tree);
193
194                }
195
196            } // end if (buffer.length() != 0) {
197        }// end copy constructor
198
199        // for some reason making this a const gives me problems when calling xml()
200        // need help looking into this
201        // RpLibrary& operator= (const RpLibrary& other) {
202        RpLibrary& operator= (RpLibrary& other) {
203
204            std::string buffer;
205            int buffLen;
206
207            scew_parser* tmp_parser;
208            scew_tree* tmp_tree;
209            scew_element* tmp_root;
210            int tmp_freeTree;
211
212            if (this != &other) {
213
214                tmp_parser   = parser;
215                tmp_tree     = tree;
216                tmp_root     = root;
217                tmp_freeTree = freeTree;
218
219                // fill in the current RpLibrary's data with other's data
220                parser = scew_parser_create();
221                scew_parser_ignore_whitespaces(parser, 1);
222
223                // Loads the XML from other
224                // the length cannot be 0 because xml() should not be returning
225                // empty strings
226                buffer = other.xml();
227                buffLen = buffer.length();
228
229                if (buffLen > 0) {
230                    if (!scew_parser_load_buffer(parser,buffer.c_str(),buffLen))
231                    {
232                        // there was an error loading the buffer
233                        // how do you tell the user, you couldn't make a copy?
234                        scew_error code = scew_error_code();
235                        printf("Unable to load buffer (error #%d: %s)\n", code,
236                               scew_error_string(code));
237
238                        if (code == scew_error_expat)
239                        {
240                            enum XML_Error expat_code =
241                                scew_error_expat_code(parser);
242                            printf("Expat error #%d (line %d, column %d): %s\n",
243                                   expat_code,
244                                   scew_error_expat_line(parser),
245                                   scew_error_expat_column(parser),
246                                   scew_error_expat_string(expat_code));
247                        }
248
249                        // return things back to the way they used to be
250                        // or maybe return an empty RpLibrary?
251                        // return EXIT_FAILURE;
252
253                        // return this object to its previous state.
254                        parser = tmp_parser;
255                    }
256                    else {
257
258                        // parsing the buffer was successful
259                        // populate the new data members.
260
261                        tree = scew_parser_tree(parser);
262                        freeTree = 0;
263                        root = scew_tree_root(tree);
264
265                        // free the current RpLibrary's data
266                        // we do the free so far down so we can see if
267                        // parsing the other object's xml fails.
268                        // if the parsing fails, we can still return this
269                        // object to its previous state.
270                        if (tmp_tree && tmp_freeTree) {
271                            scew_tree_free(tmp_tree);
272                            tmp_tree = NULL;
273                        }
274                        if (tmp_parser) {
275                            scew_parser_free(tmp_parser);
276                            tmp_parser = NULL;
277                        }
278                        if (tmp_root) {
279                            tmp_root = NULL;
280                        }
281                    }
282
283                } // end if (buffer.length() != 0) {
284            } // end if (this != &other)
285
286            return *this;
287        } // end operator=
288
289
290        // default destructor
291        virtual ~RpLibrary ()
292        {
293            // clean up dynamic memory
294
295            if (tree && freeTree) {
296                scew_tree_free(tree);
297                tree = NULL;
298            }
299            if (parser) {
300                scew_parser_free(parser);
301                parser = NULL;
302            }
303            if (root) {
304                // scew_element_free(root);
305                root = NULL;
306            }
307        }
308
309    private:
310
311        scew_parser* parser;
312        scew_tree* tree;
313        scew_element* root;
314
315        // flag to tell if we are responsible for calling scew_tree_free
316        // on the tree structure. if we get our tree by using the
317        // scew_tree_create
318        // fxn, we need to free it. if we get our tree using the
319        // scew_parser_tree
320        // fxn, then it will be free'd when the parser is free'd.
321        int freeTree;
322
323
324        RpLibrary ( scew_element* node)
325            :   parser      (NULL),
326                tree        (NULL),
327                root        (node)
328
329        {}
330
331
332        std::string _get_attribute(scew_element* element, std::string attributeName);
333        int _path2list (std::string& path, std::string** list, int listLen);
334        std::string _node2name (scew_element* node);
335        std::string _node2comp (scew_element* node);
336        int _splitPath (std::string& path,
337                        std::string& tagName,
338                        int* idx,
339                        std::string& id );
340        scew_element* _find (std::string path, int create);
341        void print_indent (unsigned int indent, std::stringstream& outString);
342        void print_attributes (scew_element* element, std::stringstream& outString);
343        void print_element( scew_element* element,
344                            unsigned int indent,
345                            std::stringstream& outString    );
346
347
348};
349
350/*--------------------------------------------------------------------------*/
351/*--------------------------------------------------------------------------*/
352
353#endif
Note: See TracBrowser for help on using the repository browser.