coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
syserr.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
6  * *
7  * Redistribution and use in source and binary forms, with or without *
8  * modification, are permitted provided that the following conditions *
9  * are met: *
10  * *
11  * Redistributions of source code must retain the above copyright *
12  * notice, this list of conditions and the following disclaimer. *
13  * *
14  * Redistributions in binary form must reproduce the above copyright *
15  * notice, this list of conditions and the following disclaimer in the *
16  * documentation and/or other materials provided with the distribution. *
17  * *
18  * Neither the name of Mike Kinghan nor the names of its contributors *
19  * may be used to endorse or promote products derived from this software *
20  * without specific prior written permission. *
21  * *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
32  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
33  * DAMAGE. *
34  * *
35  **************************************************************************/
36 
37 #include "platform.h"
38 #include "syserr.h"
39 
44 using namespace std;
45 
46 template<char Delim>
47 string system_error_message(unsigned errnum);
48 
49 #if PATH_DELIM == '/'
50 #include <cstring>
51 
52 template<>
53 string system_error_message<'/'>(unsigned errnum)
54 {
55  return strerror(errnum);
56 }
57 
58 #else
59 #include <sstream>
60 #include <windows.h>
61 
62 template<>
63 string system_error_message<'\\'>(unsigned errnum)
64 {
65  std::ostringstream gripe;
66  LPTSTR mess_buf = 0;
67  DWORD chars = FormatMessage(
68  FORMAT_MESSAGE_ALLOCATE_BUFFER |
69  FORMAT_MESSAGE_FROM_SYSTEM,
70  nullptr,
71  errnum,
72  LANG_USER_DEFAULT,
73  (LPTSTR)&mess_buf,
74  0,
75  nullptr);
76  if (!chars) {
77  gripe << "system error " << errnum;
78  } else {
79  gripe << mess_buf;
80  }
81  if (mess_buf) {
82  LocalFree(mess_buf);
83  }
84  return gripe.str();
85 }
86 
87 #endif
88 
89 std::string system_error_message(unsigned errnum)
90 {
91  return system_error_message<PATH_DELIM>(errnum);
92 }
93 
94 // EOF
string system_error_message(unsigned errnum)
Get the system error message for a system error number.
Definition: syserr.cpp:89