_stdexcept_base.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. __Named_exception::__Named_exception(const string& __str) {
  19. size_t __size = strlen(_STLP_PRIV __get_c_string(__str)) + 1;
  20. if (__size > _S_bufsize) {
  21. _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char)));
  22. if (!_M_name) {
  23. __size = _S_bufsize;
  24. _M_name = _M_static_name;
  25. }
  26. else {
  27. *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char);
  28. }
  29. }
  30. else {
  31. _M_name = _M_static_name;
  32. }
  33. #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
  34. strncpy(_M_name, _STLP_PRIV __get_c_string(__str), __size - 1);
  35. _M_name[__size - 1] = '\0';
  36. #else
  37. strncpy_s(_M_name, __size, _STLP_PRIV __get_c_string(__str), __size - 1);
  38. #endif
  39. }
  40. __Named_exception::__Named_exception(const __Named_exception& __x) {
  41. size_t __size = strlen(__x._M_name) + 1;
  42. if (__size > _S_bufsize) {
  43. _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char)));
  44. if (!_M_name) {
  45. __size = _S_bufsize;
  46. _M_name = _M_static_name;
  47. }
  48. else {
  49. *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char);
  50. }
  51. }
  52. else {
  53. _M_name = _M_static_name;
  54. }
  55. #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
  56. strncpy(_M_name, __x._M_name, __size - 1);
  57. _M_name[__size - 1] = '\0';
  58. #else
  59. strncpy_s(_M_name, __size, __x._M_name, __size - 1);
  60. #endif
  61. }
  62. __Named_exception& __Named_exception::operator = (const __Named_exception& __x) {
  63. size_t __size = strlen(__x._M_name) + 1;
  64. size_t __buf_size = _M_name != _M_static_name ? *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) : _S_bufsize;
  65. if (__size > __buf_size) {
  66. // Being here necessarily mean that we need to allocate a buffer:
  67. if (_M_name != _M_static_name) free(_M_name);
  68. _M_name = __STATIC_CAST(char*, malloc(__size * sizeof(char)));
  69. if (!_M_name) {
  70. __size = _S_bufsize;
  71. _M_name = _M_static_name;
  72. }
  73. else {
  74. *(__REINTERPRET_CAST(size_t*, &_M_static_name[0])) = __size * sizeof(char);
  75. }
  76. }
  77. #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
  78. strncpy(_M_name, __x._M_name, __size - 1);
  79. _M_name[__size - 1] = '\0';
  80. #else
  81. strncpy_s(_M_name, __size, __x._M_name, __size - 1);
  82. #endif
  83. return *this;
  84. }
  85. __Named_exception::~__Named_exception() _STLP_NOTHROW_INHERENTLY {
  86. if (_M_name != _M_static_name)
  87. free(_M_name);
  88. }
  89. const char* __Named_exception::what() const _STLP_NOTHROW_INHERENTLY
  90. { return _M_name; }