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" |
---|
16 | using namespace Rappture; |
---|
17 | |
---|
18 | /** |
---|
19 | * Create a negative outcome, with the given error message. |
---|
20 | */ |
---|
21 | Outcome::Outcome(const char *errmsg) : |
---|
22 | _status(0) |
---|
23 | { |
---|
24 | if (errmsg) { |
---|
25 | error(errmsg); |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | /// Copy constructor |
---|
30 | Outcome::Outcome(const Outcome& oc) : |
---|
31 | _status(oc._status), |
---|
32 | _remark(oc._remark), |
---|
33 | _context(oc._context) |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | /// Assignment operator |
---|
38 | Outcome& |
---|
39 | Outcome::operator=(const Outcome& oc) |
---|
40 | { |
---|
41 | _status = oc._status; |
---|
42 | _remark = oc._remark; |
---|
43 | _context = oc._context; |
---|
44 | return *this; |
---|
45 | } |
---|
46 | |
---|
47 | /// Destructor |
---|
48 | Outcome::~Outcome() |
---|
49 | {} |
---|
50 | |
---|
51 | /** |
---|
52 | * Assign an error condition to the outcome. |
---|
53 | */ |
---|
54 | Outcome& |
---|
55 | Outcome::error(const char* errmsg, int status) |
---|
56 | { |
---|
57 | _status = status; |
---|
58 | _remark = errmsg; |
---|
59 | return *this; |
---|
60 | } |
---|
61 | |
---|
62 | Outcome& |
---|
63 | Outcome::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 | */ |
---|
96 | Outcome& |
---|
97 | Outcome::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 | */ |
---|
109 | Outcome::operator int() const |
---|
110 | { |
---|
111 | return _status; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * For !error tests. |
---|
116 | */ |
---|
117 | int |
---|
118 | Outcome::operator!() const |
---|
119 | { |
---|
120 | return (_status == 0); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Use this to concatenate many different outcomes. |
---|
125 | */ |
---|
126 | Outcome& |
---|
127 | Outcome::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 | */ |
---|
138 | const char * |
---|
139 | Outcome::remark() const |
---|
140 | { |
---|
141 | return _remark.c_str(); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Add information to the context stack for an outcome. |
---|
146 | */ |
---|
147 | Outcome& |
---|
148 | Outcome::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 | */ |
---|
160 | void |
---|
161 | Outcome::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 | */ |
---|
181 | void |
---|
182 | Outcome::popContext(void) |
---|
183 | { |
---|
184 | _contexts.pop_front(); |
---|
185 | } |
---|
186 | |
---|
187 | void |
---|
188 | Outcome::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 | |
---|
203 | const char * |
---|
204 | Outcome::context() const |
---|
205 | { |
---|
206 | return _context.c_str(); |
---|
207 | } |
---|