| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2009 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 11:22:30 +02:00
										 |  |  | // The code in GetErrorMessage can't handle some systems having the
 | 
					
						
							|  |  |  | // GNU version of strerror_r and other systems having the XSI version,
 | 
					
						
							|  |  |  | // so we undefine _GNU_SOURCE here in an attempt to always get the XSI version.
 | 
					
						
							|  |  |  | // We include cstring before all other headers in case cstring is included
 | 
					
						
							|  |  |  | // indirectly (without undefining _GNU_SOURCE) by some other header.
 | 
					
						
							|  |  |  | #ifdef _GNU_SOURCE
 | 
					
						
							|  |  |  | #undef _GNU_SOURCE
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #define _GNU_SOURCE
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2017-05-13 11:22:30 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <cstddef>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-18 23:17:41 -05:00
										 |  |  | #include "Common/CommonFuncs.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 23:08:45 -08:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  | #define strerror_r(err, buf, len) strerror_s(buf, len, err)
 | 
					
						
							| 
									
										
										
										
											2017-01-22 23:08:45 -08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  | constexpr size_t BUFFER_SIZE = 256; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wrapper function to get last strerror(errno) string.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | // This function might change the error code.
 | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  | std::string LastStrerrorString() | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  |   char error_message[BUFFER_SIZE]; | 
					
						
							| 
									
										
										
										
											2011-03-16 21:08:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-13 11:22:30 +02:00
										 |  |  |   // We assume that the XSI-compliant version of strerror_r (returns int) is used
 | 
					
						
							|  |  |  |   // rather than the GNU version (returns char*). The returned value is stored to
 | 
					
						
							|  |  |  |   // an int variable to get a compile-time check that the return type is not char*.
 | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  |   const int result = strerror_r(errno, error_message, BUFFER_SIZE); | 
					
						
							| 
									
										
										
										
											2017-05-13 11:22:30 +02:00
										 |  |  |   if (result != 0) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     return ""; | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  |   return std::string(error_message); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-16 21:08:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | // Wrapper function to get GetLastError() string.
 | 
					
						
							|  |  |  | // This function might change the error code.
 | 
					
						
							|  |  |  | std::string GetLastErrorString() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char error_message[BUFFER_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), | 
					
						
							|  |  |  |                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr); | 
					
						
							|  |  |  |   return std::string(error_message); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-17 20:12:44 +01:00
										 |  |  | #endif
 |