1/* 2 * Copyright (c) 1996,1997 3 * Silicon Graphics Computer Systems, Inc. 4 * 5 * Copyright (c) 1999 6 * Boris Fomitchev 7 * 8 * This material is provided "as is", with absolutely no warranty expressed 9 * or implied. Any use is at your own risk. 10 * 11 * Permission to use or copy this software for any purpose is hereby granted 12 * without fee, provided the above notices are retained on all copies. 13 * Permission to modify the code and to distribute modified code is granted, 14 * provided the above notices are retained, and a notice that the code was 15 * modified is included with the above copyright notice. 16 * 17 */ 18 19__Named_exception::__Named_exception(const string& __str) { 20 size_t __size = strlen(_STLP_PRIV __get_c_string(__str)) + 1; 21 if (__size > _S_bufsize) { 22 _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char))); 23 if (!_M_name) { 24 __size = _S_bufsize; 25 _M_name = _M_static_name; 26 } 27 else { 28 *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char); 29 } 30 } 31 else { 32 _M_name = _M_static_name; 33 } 34#if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) 35 strncpy(_M_name, _STLP_PRIV __get_c_string(__str), __size - 1); 36 _M_name[__size - 1] = '\0'; 37#else 38 strncpy_s(_M_name, __size, _STLP_PRIV __get_c_string(__str), __size - 1); 39#endif 40} 41 42__Named_exception::__Named_exception(const __Named_exception& __x) { 43 size_t __size = strlen(__x._M_name) + 1; 44 if (__size > _S_bufsize) { 45 _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char))); 46 if (!_M_name) { 47 __size = _S_bufsize; 48 _M_name = _M_static_name; 49 } 50 else { 51 *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char); 52 } 53 } 54 else { 55 _M_name = _M_static_name; 56 } 57#if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) 58 strncpy(_M_name, __x._M_name, __size - 1); 59 _M_name[__size - 1] = '\0'; 60#else 61 strncpy_s(_M_name, __size, __x._M_name, __size - 1); 62#endif 63} 64 65__Named_exception& __Named_exception::operator = (const __Named_exception& __x) { 66 size_t __size = strlen(__x._M_name) + 1; 67 size_t __buf_size = _M_name != _M_static_name ? *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) : static_cast<size_t>(_S_bufsize); 68 if (__size > __buf_size) { 69 // Being here necessarily mean that we need to allocate a buffer: 70 if (_M_name != _M_static_name) free(_M_name); 71 _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char))); 72 if (!_M_name) { 73 __size = _S_bufsize; 74 _M_name = _M_static_name; 75 } 76 else { 77 *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char); 78 } 79 } 80#if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS) 81 strncpy(_M_name, __x._M_name, __size - 1); 82 _M_name[__size - 1] = '\0'; 83#else 84 strncpy_s(_M_name, __size, __x._M_name, __size - 1); 85#endif 86 return *this; 87} 88 89__Named_exception::~__Named_exception() _STLP_NOTHROW_INHERENTLY { 90 if (_M_name != _M_static_name) 91 free(_M_name); 92} 93 94const char* __Named_exception::what() const _STLP_NOTHROW_INHERENTLY 95{ return _M_name; } 96