source: trunk/src/core/RpOutcome.cc @ 5673

Last change on this file since 5673 was 5673, checked in by ldelgass, 9 years ago

Fix line endings, set eol-style to native on all C/C++ sources.

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1/*
2 * ======================================================================
3 *  Rappture::Outcome
4 *
5 *  AUTHOR:  Michael McLennan, Purdue University
6 *  Copyright (c) 2004-2012  HUBzero Foundation, LLC
7 * ----------------------------------------------------------------------
8 *  See the file "license.terms" for information on usage and
9 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 * ======================================================================
11 */
12#include <cstdarg>
13#include <cstdio>
14#include <cstdlib>
15#include "RpOutcome.h"
16using namespace Rappture;
17
18/**
19 *  Create a negative outcome, with the given error message.
20 */
21Outcome::Outcome(const char *errmsg) :
22    _status(0)
23{
24    if (errmsg) {
25        error(errmsg);
26    }
27}
28
29/// Copy constructor
30Outcome::Outcome(const Outcome& oc) :
31    _status(oc._status),
32    _remark(oc._remark),
33    _context(oc._context)
34{
35}
36
37/// Assignment operator
38Outcome&
39Outcome::operator=(const Outcome& oc)
40{
41    _status = oc._status;
42    _remark = oc._remark;
43    _context = oc._context;
44    return *this;
45}
46
47/// Destructor
48Outcome::~Outcome()
49{}
50
51/**
52 *  Assign an error condition to the outcome.
53 */
54Outcome&
55Outcome::error(const char* errmsg, int status)
56{
57    _status = status;
58    _remark = errmsg;
59    return *this;
60}
61
62Outcome&
63Outcome::addError(const char* format, ...)
64{
65    char stackSpace[1024];
66    va_list lst;
67    size_t n;
68    char *bufPtr;
69
70    va_start(lst, format);
71    bufPtr = stackSpace;
72    n = vsnprintf(bufPtr, 1024, format, lst);
73    if (n >= 1024) {
74        bufPtr = (char *)malloc(n);
75        vsnprintf(bufPtr, n, format, lst);
76    }
77    if (_remark == "") {
78        _remark = _context;
79        _remark.append(":\n");
80        _remark.append(bufPtr);
81    } else {
82        _remark.append("\n");
83        _remark.append(bufPtr);
84    }
85    /* fprintf(stderr, "Outcome: %s\n", _remark.c_str()); */
86    _status = 1;                /* Set to error */
87    if (bufPtr != stackSpace) {
88        free(bufPtr);
89    }
90    return *this;
91}
92
93/**
94 *  Clear the status of this outcome.
95 */
96Outcome&
97Outcome::clear()
98{
99    _status = 0;
100    _remark.clear();
101    _context.clear();
102    return *this;
103}
104
105/**
106 *  Returns the status of this outcome as an integer.
107 *  As in Unix systems, 0 = okay.
108 */
109Outcome::operator int() const
110{
111    return _status;
112}
113
114/**
115 *  For !error tests.
116 */
117int
118Outcome::operator!() const
119{
120    return (_status == 0);
121}
122
123/**
124 *  Use this to concatenate many different outcomes.
125 */
126Outcome&
127Outcome::operator&=(Outcome oc)
128{
129    _status &= oc._status;
130    _remark = oc._remark;
131    _context = oc._context;
132    return *this;
133}
134
135/**
136 *  Query the error remark from an outcome.
137 */
138const char *
139Outcome::remark() const
140{
141    return _remark.c_str();
142}
143
144/**
145 *  Add information to the context stack for an outcome.
146 */
147Outcome&
148Outcome::addContext(const char *rem)
149{
150    // FIXME: There should be a stack of contexts
151    _context = rem;
152    _context.append("\n");
153    return *this;
154}
155
156#ifdef soon
157/**
158 *  Push context information on to the context stack for an outcome.
159 */
160void
161Outcome::pushContext(const char* format, ...)
162{
163    char stackSpace[1024];
164    va_list lst;
165    size_t n;
166    char *bufPtr;
167
168    va_start(lst, format);
169    bufPtr = stackSpace;
170    n = vsnprintf(bufPtr, 1024, format, lst);
171    if (n >= 1024) {
172        bufPtr = (char *)malloc(n);
173        vsnprintf(bufPtr, n, format, lst);
174    }
175    _contexts.push_front(bufPtr);
176}
177
178/**
179 *  Pop the last context from the stack.
180 */
181void
182Outcome::popContext(void)
183{
184    _contexts.pop_front();
185}
186
187void
188Outcome::printContext(void)
189{
190    list<const char *>::interator iter;
191
192    for (iter = _contexts.begin(); iter != _contexts.end(); iter++) {
193        fprintf(stderr, "Called from %s\n", *iter);
194    }
195}
196
197#endif /*soon*/
198
199/**
200 *  Query the context stack from an outcome.
201 */
202
203const char *
204Outcome::context() const
205{
206    return _context.c_str();
207}
Note: See TracBrowser for help on using the repository browser.