source: branches/blt4/src/objects/RpLibObj.cc @ 4988

Last change on this file since 4988 was 3959, checked in by gah, 11 years ago

sync with trunk

File size: 7.6 KB
Line 
1/*
2 * ----------------------------------------------------------------------
3 *  Rappture Library Source
4 *
5 * ======================================================================
6 *  AUTHOR:  Derrick S. Kearney, Purdue University
7 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
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#include "expat.h"
15#include "RpParserXML.h"
16#include "RpLibObj.h"
17#include "RpBuffer.h"
18#include "RpEntityRef.h"
19#include "RpEncode.h"
20#include "RpTree.h"
21
22#include "RpNumber.h"
23
24#include <stdlib.h>
25#include <time.h>
26#include <cctype>
27#include <fstream>
28
29using namespace Rappture;
30
31Library::Library ()
32{
33    __libInit();
34}
35
36Library::~Library ()
37{
38    __libFree();
39}
40
41void
42Library::__libInit()
43{
44    _status.addContext("Rappture::Library::__libInit");
45    return;
46}
47
48void
49Library::__libFree()
50{
51    return;
52}
53
54Outcome &
55Library::loadXml (const char *xmltext)
56{
57    Rp_ParserXml *p = NULL;
58
59    _status.addContext("Rappture::Library::loadXml");
60
61    if (xmltext == NULL) {
62        _status.addError("xmltext was NULL");
63        return _status;
64    }
65
66    // parse xml file
67    p = Rp_ParserXmlCreate();
68    if (p == NULL) {
69        _status.addError("error while creating xml parser");
70        return _status;
71    }
72    Rp_ParserXmlParse(p, xmltext);
73    // FIXME: add error check for Rp_ParserXmlParse
74
75    //_objStorage.backup();
76    _objStorage.clear();
77    __parseTree2ObjectList(p);
78    /*
79    if (!_status) {
80        _objStorage.save();
81    } else {
82        _objStorage.restore()
83    }
84    */
85
86    return _status;
87}
88
89Outcome &
90Library::loadFile (const char *filename)
91{
92    _status.addContext("Rappture::Library::loadFile");
93
94    // load xml file
95    Rappture::Buffer fileBuf;
96    if (!fileBuf.load(_status, filename)) {
97        return _status;
98    }
99
100    // null terminate string holding file
101    fileBuf.append("\0",1);
102
103    loadXml(fileBuf.bytes());
104
105    return _status;
106}
107
108Library &
109Library::value (
110    const char *key,
111    void *storage,
112    size_t numHints,
113    ...)
114{
115    _status.addContext("Rappture::Library::value");
116    Rappture::Object *o = _objStorage.find(key);
117
118    if (o == NULL) {
119        _status.addError("Error while retrieving object "
120            "with key \"%s\": object does not exist", key);
121        return *this;
122    }
123
124    va_list arg;
125
126    va_start(arg,numHints);
127    o->vvalue(storage,numHints,arg);
128    _status = o->outcome();
129    va_end(arg);
130
131    return *this;
132}
133
134const char *
135Library::xml() const
136{
137    _status.addContext("Rappture::Library::xml");
138    Rp_ParserXml *p = Rp_ParserXmlCreate();
139    Rp_ChainLink *l = NULL;
140
141    const Rp_Chain *objList = _objStorage.contains();
142
143    l = Rp_ChainFirstLink(objList);
144
145    while (l != NULL) {
146        Rappture::Object *o = (Rappture::Object *) Rp_ChainGetValue(l);
147        if (o != NULL) {
148            // FIXME: we can remove the if statement when
149            // LibraryStorage is fixed to clean up holes
150            // in the object chain.
151            o->dump(RPCONFIG_TREE,(ClientData)p);
152        }
153        l = Rp_ChainNextLink(l);
154    }
155
156    return Rp_ParserXmlXml(p);
157}
158
159void
160Library::__parseTree2ObjectList(Rp_ParserXml *p)
161{
162    _status.addContext("Rappture::Library::__parseTree2ObjectList");
163
164    if (p == NULL) {
165        _status.addError("parser is NULL");
166        return;
167    }
168
169    Rp_Chain *children = Rp_ChainCreate();
170
171    // parse out the tool information
172    // Rp_ParserXmlChildren(p, "tool", NULL, children);
173    // if there is a child called tool, grab its
174    //      title                   char *
175    //      about                   char *
176    //      command                 char *
177    //      limits.cputime          double
178    //      layout                  char *
179    //      control                 char *
180    //      analyzer                char *
181    //      reportJobFailures       int
182
183    // get the children nodes of "input", and "output"
184    Rp_ParserXmlChildren(p, "input", NULL, children);
185    Rp_ParserXmlChildren(p, "output", NULL, children);
186
187    Rp_ChainLink *l = Rp_ChainFirstLink(children);
188    while (l != NULL) {
189        Rp_TreeNode child = (Rp_TreeNode) Rp_ChainGetValue(l);
190        const char *label = Rp_TreeNodeLabel(child);
191
192        Rp_ParserXmlBaseNode(p,child);
193
194        // find the correct object to create
195        // Rappture::Object *nobj = NULL;
196        if (('n' == *label) && (strcmp(label,"number") == 0)) {
197            Rappture::Number *obj = new Rappture::Number();
198            obj->configure(RPCONFIG_TREE,(void*)p);
199            _objStorage.store(obj->name(),obj);
200            _objStorage.link(obj->name(),obj->path());
201            // Rp_ChainAppend(retObjList,(void*)obj);
202
203        // FIXME: add code to check for strings, choices,
204        //        groups, curves, notes, images, molecules...
205        //        all of the rappture object types
206        } else {
207            _status.addError("unrecognized object type: %s",label);
208        }
209
210        l = Rp_ChainNextLink(l);
211    }
212
213    // FIXME: should return the base node to the previous base
214    Rp_ParserXmlBaseNode(p,NULL);
215
216    return;
217}
218
219
220
221Outcome &
222Library::outcome() const
223{
224    return _status;
225}
226
227int
228Library::error() const
229{
230    return (int) _status;
231}
232
233/*
234Outcome &
235Library::result(int status)
236{
237    Rappture::SimpleCharBuffer tmpBuf;
238    std::fstream file;
239    const char *xmlText = NULL;
240    time_t t = 0;
241    struct tm* timeinfo = NULL;
242    timestamp = "";
243    std::string username = "";
244    std::string hostname = "";
245    char *user = NULL;
246
247    tmpBuf.appendf("run%i.xml",(int)time(&t));
248    file.open(tmpBuf.bytes(),std::ios::out);
249
250//    put("tool.version.rappture.revision",
251//        "$LastChangedRevision: 1527 $");
252//    put("tool.version.rappture.modified",
253//        "$LastChangedDate: 2009-06-22 15:38:49 -0400"
254//        " (Mon, 22 Jun 2009) $");
255//    if ( "" == get("tool.version.rappture.language") ) {
256//        put("tool.version.rappture.language","c++");
257//    }
258
259    // generate a timestamp for the run file
260    timeinfo = localtime(&t);
261    timestamp = std::string(ctime(&t));
262    // erase the 24th character because it is a newline
263    timestamp.erase(24);
264    // concatinate the timezone
265    timestamp.append(" ");
266#ifdef _WIN32
267    timestamp.append(_tzname[_daylight]);
268    // username is left blank for windows because i dont know
269    // how to retrieve username on win32 environment.
270    username = "";
271    hostname = "";
272#else
273    timestamp.append(timeinfo->tm_zone);
274    user = getenv("USER");
275    if (user != NULL) {
276        username = std::string(user);
277    } else {
278        user = getenv("LOGNAME");
279        if (user != NULL) {
280            username = std::string(user);
281        }
282    }
283#endif
284
285    // add the timestamp to the run file
286//    put("output.time", timestamp);
287//    put("output.status",exitStatus);
288//    put("output.user",username);
289//    put("output.host",hostname);
290
291    if ( file.is_open() ) {
292        xmlText = xml();
293        if (xmlText == NULL) {
294        }
295        file << xmlText;
296        // check to make sure there were no
297        // errors while writing the run.xml file.
298        if (   (!file.good())
299            || (strlen(xmlText) != ((long)file.tellp()-(long)1))
300           ) {
301             status.error("Error while writing run file");
302             status.addContext("RpLibrary::result()");
303        }
304        file.close();
305    }
306    else {
307        status.error("Error while opening run file");
308        status.addContext("RpLibrary::result()");
309    }
310    std::printf("=RAPPTURE-RUN=>%s\n",outputFile.bytes());
311}
312*/
313
314const Rp_Chain *
315Library::contains() const
316{
317    return _objStorage.contains();
318}
319
Note: See TracBrowser for help on using the repository browser.