blob: 2d39db64793245086c013f80f4c4b020f386d924 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <ucommon-config.h>
40
41#ifndef _XOPEN_SOURCE
42#define _XOPEN_SOURCE 600
43#endif
44
45#include <commoncpp/config.h>
46#include <commoncpp/export.h>
47#include <commoncpp/string.h>
48#include <commoncpp/thread.h>
49#include <commoncpp/exception.h>
50
51#if defined(CCXX_EXCEPTIONS)
52
53using namespace COMMONCPP_NAMESPACE;
54
55Exception::Exception(const String& what_arg) throw():
56_what(what_arg)
57{}
58
59Exception::~Exception() throw()
60{}
61
62const char *Exception::what() const throw()
63{
64 return _what.c_str();
65}
66
67const char *Exception::getString() const
68{
69 return _what.c_str();
70}
71
72IOException::IOException(const String &what_arg, long systemError) throw() :
73 Exception(what_arg), _systemError(systemError),
74 _systemErrorString(NULL) {
75}
76
77IOException::~IOException() throw()
78{
79 delete [] _systemErrorString;
80}
81
82long IOException::getSystemError() const throw()
83{
84 return _systemError;
85}
86
87const char* IOException::getSystemErrorString() const throw()
88{
89 const uint32_t errStrSize = 2048;
90 if ( !_systemErrorString )
91 _systemErrorString = new char[errStrSize];
92#ifndef _MSWINDOWS_
93#ifdef HAVE_STRERROR_R
94#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) || !defined(APPLE))
95 assert(strerror_r(_systemError, _systemErrorString, errStrSize) == 0);
96#else
97 _systemErrorString = strerror_r(_systemError, _systemErrorString, errStrSize);
98#endif
99#else
100 static Mutex mlock;
101
102 mlock.enter();
103 String::set(_systemErrorString, errStrSize, strerror(_systemError));
104 mlock.leave();
105#endif
106 return _systemErrorString;
107#else
108 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, _systemError,
109 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
110 _systemErrorString, errStrSize, NULL);
111 return _systemErrorString;
112#endif
113}
114
115#endif