Add encoding for VSTR.
[oota-llvm.git] / utils / KillTheDoctor / system_error.cpp
1 //===---------------------- system_error.cpp ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This was lifted from libc++ and modified for C++03.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Config/config.h"
15 #include "system_error.h"
16 #include <string>
17 #include <cstring>
18
19 namespace llvm {
20
21 // class error_category
22
23 error_category::error_category() {
24 }
25
26 error_category::~error_category() {
27 }
28
29 error_condition
30 error_category::default_error_condition(int ev) const {
31   return error_condition(ev, *this);
32 }
33
34 bool
35 error_category::equivalent(int code, const error_condition& condition) const {
36   return default_error_condition(code) == condition;
37 }
38
39 bool
40 error_category::equivalent(const error_code& code, int condition) const {
41   return *this == code.category() && code.value() == condition;
42 }
43
44 std::string
45 _do_message::message(int ev) const {
46   return std::string(std::strerror(ev));
47 }
48
49 class _generic_error_category : public _do_message {
50 public:
51   virtual const char* name() const;
52   virtual std::string message(int ev) const;
53 };
54
55 const char*
56 _generic_error_category::name() const {
57   return "generic";
58 }
59
60 std::string
61 _generic_error_category::message(int ev) const {
62 #ifdef ELAST
63   if (ev > ELAST)
64     return std::string("unspecified generic_category error");
65 #endif  // ELAST
66   return _do_message::message(ev);
67 }
68
69 const error_category&
70 generic_category() {
71   static _generic_error_category s;
72   return s;
73 }
74
75 class _system_error_category : public _do_message {
76 public:
77   virtual const char* name() const;
78   virtual std::string message(int ev) const;
79   virtual error_condition default_error_condition(int ev) const;
80 };
81
82 const char*
83 _system_error_category::name() const {
84   return "system";
85 }
86
87 // std::string _system_error_category::message(int ev) const {
88 // Is in Platform/system_error.inc
89
90 // error_condition _system_error_category::default_error_condition(int ev) const
91 // Is in Platform/system_error.inc
92
93 const error_category&
94 system_category() {
95   static _system_error_category s;
96   return s;
97 }
98
99 // error_condition
100
101 std::string
102 error_condition::message() const {
103   return _cat_->message(_val_);
104 }
105
106 // error_code
107
108 std::string
109 error_code::message() const {
110   return _cat_->message(_val_);
111 }
112
113 // system_error
114
115 std::string
116 system_error::_init(const error_code& ec, std::string what_arg) {
117   if (ec)
118   {
119     if (!what_arg.empty())
120       what_arg += ": ";
121     what_arg += ec.message();
122   }
123   return what_arg;
124 }
125
126 system_error::system_error(error_code ec, const std::string& what_arg)
127   : runtime_error(_init(ec, what_arg)), _ec_(ec) {
128 }
129
130 system_error::system_error(error_code ec, const char* what_arg)
131   : runtime_error(_init(ec, what_arg)), _ec_(ec) {
132 }
133
134 system_error::system_error(error_code ec)
135   : runtime_error(_init(ec, "")), _ec_(ec) {
136 }
137
138 system_error::system_error(int ev, const error_category& ecat,
139                            const std::string& what_arg)
140   : runtime_error(_init(error_code(ev, ecat), what_arg))
141   , _ec_(error_code(ev, ecat)) {
142 }
143
144 system_error::system_error(int ev, const error_category& ecat,
145                            const char* what_arg)
146   : runtime_error(_init(error_code(ev, ecat), what_arg))
147   , _ec_(error_code(ev, ecat)) {
148 }
149
150 system_error::system_error(int ev, const error_category& ecat)
151   : runtime_error(_init(error_code(ev, ecat), "")), _ec_(error_code(ev, ecat)) {
152 }
153
154 system_error::~system_error() throw() {
155 }
156
157 void
158 _throw_system_error(int ev, const char* what_arg) {
159   throw system_error(error_code(ev, system_category()), what_arg);
160 }
161
162 } // end namespace llvm
163
164 #ifdef LLVM_ON_WIN32
165 #include <Windows.h>
166 #include <WinError.h>
167
168 namespace llvm {
169
170 std::string
171 _system_error_category::message(int ev) const {
172   LPVOID lpMsgBuf = 0;
173   DWORD retval = ::FormatMessageA(
174     FORMAT_MESSAGE_ALLOCATE_BUFFER |
175     FORMAT_MESSAGE_FROM_SYSTEM |
176     FORMAT_MESSAGE_IGNORE_INSERTS,
177     NULL,
178     ev,
179     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
180     (LPSTR) &lpMsgBuf,
181     0,
182     NULL);
183   if (retval == 0) {
184     ::LocalFree(lpMsgBuf);
185     return std::string("Unknown error");
186   }
187
188   std::string str( static_cast<LPCSTR>(lpMsgBuf) );
189   ::LocalFree(lpMsgBuf);
190
191   while (str.size()
192      && (str[str.size()-1] == '\n' || str[str.size()-1] == '\r'))
193     str.erase( str.size()-1 );
194   if (str.size() && str[str.size()-1] == '.')
195     str.erase( str.size()-1 );
196   return str;
197 }
198
199 error_condition
200 _system_error_category::default_error_condition(int ev) const {
201   switch (ev)
202   {
203   case 0: return make_error_condition(errc::success);
204   // Windows system -> posix_errno decode table  ---------------------------//
205   // see WinError.h comments for descriptions of errors
206   case ERROR_ACCESS_DENIED: return make_error_condition(errc::permission_denied);
207   case ERROR_ALREADY_EXISTS: return make_error_condition(errc::file_exists);
208   case ERROR_BAD_UNIT: return make_error_condition(errc::no_such_device);
209   case ERROR_BUFFER_OVERFLOW: return make_error_condition(errc::filename_too_long);
210   case ERROR_BUSY: return make_error_condition(errc::device_or_resource_busy);
211   case ERROR_BUSY_DRIVE: return make_error_condition(errc::device_or_resource_busy);
212   case ERROR_CANNOT_MAKE: return make_error_condition(errc::permission_denied);
213   case ERROR_CANTOPEN: return make_error_condition(errc::io_error);
214   case ERROR_CANTREAD: return make_error_condition(errc::io_error);
215   case ERROR_CANTWRITE: return make_error_condition(errc::io_error);
216   case ERROR_CURRENT_DIRECTORY: return make_error_condition(errc::permission_denied);
217   case ERROR_DEV_NOT_EXIST: return make_error_condition(errc::no_such_device);
218   case ERROR_DEVICE_IN_USE: return make_error_condition(errc::device_or_resource_busy);
219   case ERROR_DIR_NOT_EMPTY: return make_error_condition(errc::directory_not_empty);
220   case ERROR_DIRECTORY: return make_error_condition(errc::invalid_argument);
221   case ERROR_DISK_FULL: return make_error_condition(errc::no_space_on_device);
222   case ERROR_FILE_EXISTS: return make_error_condition(errc::file_exists);
223   case ERROR_FILE_NOT_FOUND: return make_error_condition(errc::no_such_file_or_directory);
224   case ERROR_HANDLE_DISK_FULL: return make_error_condition(errc::no_space_on_device);
225   case ERROR_INVALID_ACCESS: return make_error_condition(errc::permission_denied);
226   case ERROR_INVALID_DRIVE: return make_error_condition(errc::no_such_device);
227   case ERROR_INVALID_FUNCTION: return make_error_condition(errc::function_not_supported);
228   case ERROR_INVALID_HANDLE: return make_error_condition(errc::invalid_argument);
229   case ERROR_INVALID_NAME: return make_error_condition(errc::invalid_argument);
230   case ERROR_LOCK_VIOLATION: return make_error_condition(errc::no_lock_available);
231   case ERROR_LOCKED: return make_error_condition(errc::no_lock_available);
232   case ERROR_NEGATIVE_SEEK: return make_error_condition(errc::invalid_argument);
233   case ERROR_NOACCESS: return make_error_condition(errc::permission_denied);
234   case ERROR_NOT_ENOUGH_MEMORY: return make_error_condition(errc::not_enough_memory);
235   case ERROR_NOT_READY: return make_error_condition(errc::resource_unavailable_try_again);
236   case ERROR_NOT_SAME_DEVICE: return make_error_condition(errc::cross_device_link);
237   case ERROR_OPEN_FAILED: return make_error_condition(errc::io_error);
238   case ERROR_OPEN_FILES: return make_error_condition(errc::device_or_resource_busy);
239   case ERROR_OPERATION_ABORTED: return make_error_condition(errc::operation_canceled);
240   case ERROR_OUTOFMEMORY: return make_error_condition(errc::not_enough_memory);
241   case ERROR_PATH_NOT_FOUND: return make_error_condition(errc::no_such_file_or_directory);
242   case ERROR_READ_FAULT: return make_error_condition(errc::io_error);
243   case ERROR_RETRY: return make_error_condition(errc::resource_unavailable_try_again);
244   case ERROR_SEEK: return make_error_condition(errc::io_error);
245   case ERROR_SHARING_VIOLATION: return make_error_condition(errc::permission_denied);
246   case ERROR_TOO_MANY_OPEN_FILES: return make_error_condition(errc::too_many_files_open);
247   case ERROR_WRITE_FAULT: return make_error_condition(errc::io_error);
248   case ERROR_WRITE_PROTECT: return make_error_condition(errc::permission_denied);
249   case ERROR_SEM_TIMEOUT: return make_error_condition(errc::timed_out);
250   case WSAEACCES: return make_error_condition(errc::permission_denied);
251   case WSAEADDRINUSE: return make_error_condition(errc::address_in_use);
252   case WSAEADDRNOTAVAIL: return make_error_condition(errc::address_not_available);
253   case WSAEAFNOSUPPORT: return make_error_condition(errc::address_family_not_supported);
254   case WSAEALREADY: return make_error_condition(errc::connection_already_in_progress);
255   case WSAEBADF: return make_error_condition(errc::bad_file_descriptor);
256   case WSAECONNABORTED: return make_error_condition(errc::connection_aborted);
257   case WSAECONNREFUSED: return make_error_condition(errc::connection_refused);
258   case WSAECONNRESET: return make_error_condition(errc::connection_reset);
259   case WSAEDESTADDRREQ: return make_error_condition(errc::destination_address_required);
260   case WSAEFAULT: return make_error_condition(errc::bad_address);
261   case WSAEHOSTUNREACH: return make_error_condition(errc::host_unreachable);
262   case WSAEINPROGRESS: return make_error_condition(errc::operation_in_progress);
263   case WSAEINTR: return make_error_condition(errc::interrupted);
264   case WSAEINVAL: return make_error_condition(errc::invalid_argument);
265   case WSAEISCONN: return make_error_condition(errc::already_connected);
266   case WSAEMFILE: return make_error_condition(errc::too_many_files_open);
267   case WSAEMSGSIZE: return make_error_condition(errc::message_size);
268   case WSAENAMETOOLONG: return make_error_condition(errc::filename_too_long);
269   case WSAENETDOWN: return make_error_condition(errc::network_down);
270   case WSAENETRESET: return make_error_condition(errc::network_reset);
271   case WSAENETUNREACH: return make_error_condition(errc::network_unreachable);
272   case WSAENOBUFS: return make_error_condition(errc::no_buffer_space);
273   case WSAENOPROTOOPT: return make_error_condition(errc::no_protocol_option);
274   case WSAENOTCONN: return make_error_condition(errc::not_connected);
275   case WSAENOTSOCK: return make_error_condition(errc::not_a_socket);
276   case WSAEOPNOTSUPP: return make_error_condition(errc::operation_not_supported);
277   case WSAEPROTONOSUPPORT: return make_error_condition(errc::protocol_not_supported);
278   case WSAEPROTOTYPE: return make_error_condition(errc::wrong_protocol_type);
279   case WSAETIMEDOUT: return make_error_condition(errc::timed_out);
280   case WSAEWOULDBLOCK: return make_error_condition(errc::operation_would_block);
281   default: return error_condition(ev, system_category());
282   }
283 }
284
285 } // end namespace llvm
286
287 #endif // LLVM_ON_WIN32