source: branches/blt4/src/core/RpOutcome.cc @ 1780

Last change on this file since 1780 was 1571, checked in by gah, 15 years ago

Fixups for bit rot: gcc-4.4.1

File size: 3.8 KB
Line 
1/*
2 * ======================================================================
3 *  Rappture::Outcome
4 *
5 *  AUTHOR:  Michael McLennan, Purdue University
6 *  Copyright (c) 2004-2007  Purdue Research Foundation
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    _status = 1;                /* Set to error */
86    if (bufPtr != stackSpace) {
87        free(bufPtr);
88    }
89    return *this;
90}
91
92/**
93 *  Clear the status of this outcome.
94 */
95Outcome&
96Outcome::clear()
97{
98    _status = 0;
99    _remark.clear();
100    _context.clear();
101    return *this;
102}
103
104/**
105 *  Returns the status of this outcome as an integer.
106 *  As in Unix systems, 0 = okay.
107 */
108Outcome::operator int() const
109{
110    return _status;
111}
112
113/**
114 *  For !error tests.
115 */
116int
117Outcome::operator!() const
118{
119    return (_status == 0);
120}
121
122/**
123 *  Use this to concatenate many different outcomes.
124 */
125Outcome&
126Outcome::operator&=(Outcome oc)
127{
128    _status &= oc._status;
129    _remark = oc._remark;
130    _context = oc._context;
131    return *this;
132}
133
134/**
135 *  Query the error remark from an outcome.
136 */
137const char *
138Outcome::remark() const
139{
140    return _remark.c_str();
141}
142
143/**
144 *  Add information to the context stack for an outcome.
145 */
146Outcome&
147Outcome::addContext(const char *rem)
148{
149    // FIXME: There should be a stack of contexts
150    _context = rem;
151    _context.append("\n");
152    return *this;
153}
154
155#ifdef soon
156/**
157 *  Push context information on to the context stack for an outcome.
158 */
159void
160Outcome::pushContext(const char* format, ...)
161{
162    char stackSpace[1024];
163    va_list lst;
164    size_t n;
165    char *bufPtr;
166
167    va_start(lst, format);
168    bufPtr = stackSpace;
169    n = vsnprintf(bufPtr, 1024, format, lst);
170    if (n >= 1024) {
171        bufPtr = (char *)malloc(n);
172        vsnprintf(bufPtr, n, format, lst);
173    }
174    _contexts.push_front(bufPtr);
175}
176
177/**
178 *  Pop the last context from the stack.
179 */
180void
181Outcome::popContext(void)
182{
183    _contexts.pop_front();
184}
185
186void
187Outcome::printContext(void)
188{
189    list<const char *>::interator iter;
190
191    for (iter = _contexts.begin(); iter != _contexts.end(); iter++) {
192        fprintf(stderr, "Called from %s\n", *iter);
193    }
194}
195
196#endif /*soon*/
197
198/**
199 *  Query the context stack from an outcome.
200 */
201
202const char *
203Outcome::context() const
204{
205    return _context.c_str();
206}
Note: See TracBrowser for help on using the repository browser.