Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
status.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2011 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * error handling system: defines status codes, translates them to/from
25  * other schemes (e.g. errno), associates them with descriptive text,
26  * simplifies propagating errors / checking if functions failed.
27  */
28 
29 #include "precompiled.h"
30 #include "lib/status.h"
31 
32 #include <cstring>
33 #include <cstdio>
34 
35 #include "lib/posix/posix_errno.h"
36 
37 
39 
41 {
42  // insert at front of list
44  buckets = bucket;
45  return next;
46 }
47 
48 
50 {
51  for(const StatusDefinitionBucket* bucket = buckets; bucket; bucket = bucket->next)
52  {
53  for(size_t i = 0; i < bucket->numDefinitions; i++)
54  {
55  if(bucket->definitions[i].status == status)
56  return &bucket->definitions[i];
57  }
58  }
59 
60  return 0;
61 }
62 
63 
64 static const StatusDefinition* DefinitionFromErrno(int errno_equivalent)
65 {
66  for(const StatusDefinitionBucket* bucket = buckets; bucket; bucket = bucket->next)
67  {
68  for(size_t i = 0; i < bucket->numDefinitions; i++)
69  {
70  if(bucket->definitions[i].errno_equivalent == errno_equivalent)
71  return &bucket->definitions[i];
72  }
73  }
74 
75  return 0;
76 }
77 
78 
79 wchar_t* StatusDescription(Status status, wchar_t* buf, size_t max_chars)
80 {
81  const StatusDefinition* def = DefinitionFromStatus(status);
82  if(def)
83  {
84  wcscpy_s(buf, max_chars, def->description);
85  return buf;
86  }
87 
88  swprintf_s(buf, max_chars, L"Unknown error (%lld, 0x%llX)", (long long)status, (unsigned long long)status);
89  return buf;
90 }
91 
92 
94 {
95  const StatusDefinition* def = DefinitionFromStatus(status);
96  if(def && def->errno_equivalent != 0)
97  return def->errno_equivalent;
98 
99  // the set of errnos in wposix.h doesn't have an "unknown error".
100  // we use this one as a default because it's not expected to come up often.
101  return EPERM;
102 }
103 
104 
106 {
107  if(errno == 0)
108  return INFO::OK;
109  const StatusDefinition* def = DefinitionFromErrno(errno);
110  return def? def->status : ERR::FAIL;
111 }
112 
113 
114 //-----------------------------------------------------------------------------
115 
116 static const StatusDefinition statusDefs[] = {
117 
118 // INFO::OK doesn't really need a string because calling StatusDescription(0)
119 // should never happen, but we'll play it safe.
120 { INFO::OK, L"No error reported here" },
121 { ERR::FAIL, L"Function failed (no details available)" },
122 
123 { INFO::SKIPPED, L"Skipped (not an error)" },
124 { INFO::CANNOT_HANDLE, L"Cannot handle (not an error)" },
125 { INFO::ALL_COMPLETE, L"All complete (not an error)" },
126 
127 { ERR::LOGIC, L"Logic error in code" },
128 { ERR::EXCEPTION, L"Caught an exception" },
129 { ERR::TIMED_OUT, L"Timed out" },
130 { ERR::REENTERED, L"Single-call function was reentered" },
131 { ERR::CORRUPTED, L"File/memory data is corrupted" },
132 { ERR::ABORTED, L"Operation aborted" },
133 
134 { ERR::INVALID_ALIGNMENT, L"Invalid alignment", EINVAL },
135 { ERR::INVALID_OFFSET, L"Invalid offset", EINVAL },
136 { ERR::INVALID_HANDLE, L"Invalid handle", EINVAL },
137 { ERR::INVALID_POINTER, L"Invalid pointer", EINVAL },
138 { ERR::INVALID_SIZE, L"Invalid size", EINVAL },
139 { ERR::INVALID_FLAG, L"Invalid flag", EINVAL },
140 { ERR::INVALID_PARAM, L"Invalid parameter", EINVAL },
141 { ERR::INVALID_VERSION, L"Invalid version", EINVAL },
142 
143 { ERR::AGAIN, L"Try again later", EAGAIN },
144 { ERR::LIMIT, L"Fixed limit exceeded", E2BIG },
145 { ERR::NOT_SUPPORTED, L"Function not supported", ENOSYS },
146 { ERR::NO_MEM, L"Not enough memory", ENOMEM},
147 
148 { ERR::_1, L"Case 1" },
149 { ERR::_2, L"Case 2" },
150 { ERR::_3, L"Case 3" },
151 { ERR::_4, L"Case 4" },
152 { ERR::_5, L"Case 5" },
153 { ERR::_6, L"Case 6" },
154 { ERR::_7, L"Case 7" },
155 { ERR::_8, L"Case 8" },
156 { ERR::_9, L"Case 9" },
157 { ERR::_11, L"Case 11" },
158 { ERR::_12, L"Case 12" },
159 { ERR::_13, L"Case 13" },
160 { ERR::_14, L"Case 14" },
161 { ERR::_15, L"Case 15" },
162 { ERR::_16, L"Case 16" },
163 { ERR::_17, L"Case 17" },
164 { ERR::_18, L"Case 18" },
165 { ERR::_19, L"Case 19" },
166 { ERR::_21, L"Case 21" },
167 { ERR::_22, L"Case 22" },
168 { ERR::_23, L"Case 23" },
169 { ERR::_24, L"Case 24" },
170 { ERR::_25, L"Case 25" },
171 { ERR::_26, L"Case 26" },
172 { ERR::_27, L"Case 27" },
173 { ERR::_28, L"Case 28" },
174 { ERR::_29, L"Case 29" }
175 
176 };
177 STATUS_ADD_DEFINITIONS(statusDefs);
const Status _7
Definition: status.h:447
const Status LOGIC
Definition: status.h:409
const Status _29
Definition: status.h:467
const Status _14
Definition: status.h:453
const Status _4
Definition: status.h:444
const Status _1
Definition: status.h:441
const Status _6
Definition: status.h:446
const Status _24
Definition: status.h:462
const Status OK
Definition: status.h:386
const Status _13
Definition: status.h:452
const Status _9
Definition: status.h:449
const Status _16
Definition: status.h:455
StatusDefinitionBucket * next
Definition: status.h:196
const Status TIMED_OUT
Definition: status.h:411
const Status _3
Definition: status.h:443
const Status CORRUPTED
Definition: status.h:413
static StatusDefinitionBucket * buckets
Definition: status.cpp:38
int swprintf_s(wchar_t *buf, size_t max_chars, const wchar_t *fmt,...) WPRINTF_ARGS(3)
const Status CANNOT_HANDLE
Definition: status.h:396
const Status INVALID_HANDLE
Definition: status.h:419
const Status ALL_COMPLETE
Definition: status.h:400
const Status AGAIN
Definition: status.h:427
int wcscpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
const Status _25
Definition: status.h:463
const Status ABORTED
Definition: status.h:414
const Status NOT_SUPPORTED
Definition: status.h:429
const Status INVALID_OFFSET
Definition: status.h:418
const Status _17
Definition: status.h:456
int ErrnoFromStatus(Status status)
Definition: status.cpp:93
const Status INVALID_VERSION
Definition: status.h:424
const Status _21
Definition: status.h:459
static const StatusDefinition * DefinitionFromStatus(Status status)
Definition: status.cpp:49
const wchar_t * description
Definition: status.h:179
const Status LIMIT
Definition: status.h:428
const Status INVALID_POINTER
Definition: status.h:420
const Status _5
Definition: status.h:445
const Status _27
Definition: status.h:465
const Status REENTERED
Definition: status.h:412
const Status _15
Definition: status.h:454
const Status _19
Definition: status.h:458
const Status INVALID_PARAM
Definition: status.h:423
i64 Status
Error handling system.
Definition: status.h:171
const Status INVALID_SIZE
Definition: status.h:421
const Status INVALID_ALIGNMENT
Definition: status.h:417
#define STATUS_ADD_DEFINITIONS(definitions)
add a module&#39;s array of StatusDefinition to the list.
Definition: status.h:216
Status StatusFromErrno()
Definition: status.cpp:105
static const StatusDefinition statusDefs[]
Definition: status.cpp:116
const Status SKIPPED
Definition: status.h:392
const Status _26
Definition: status.h:464
const Status _11
Definition: status.h:450
const Status _28
Definition: status.h:466
const Status _18
Definition: status.h:457
wchar_t * StatusDescription(Status status, wchar_t *buf, size_t max_chars)
generate textual description of a Status.
Definition: status.cpp:79
Status status
Definition: status.h:176
const Status _8
Definition: status.h:448
const Status INVALID_FLAG
Definition: status.h:422
static const StatusDefinition * DefinitionFromErrno(int errno_equivalent)
Definition: status.cpp:64
const Status _22
Definition: status.h:460
const Status FAIL
Definition: status.h:406
const Status _2
Definition: status.h:442
const Status EXCEPTION
Definition: status.h:410
StatusDefinitionBucket * StatusAddDefinitions(StatusDefinitionBucket *bucket)
(called via STATUS_ADD_DEFINITIONS)
Definition: status.cpp:40
const Status NO_MEM
Definition: status.h:430
const Status _23
Definition: status.h:461
const Status _12
Definition: status.h:451
int errno_equivalent
Definition: status.h:182