Exception.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Copyright (c) 2006-2024 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. #include "Exception.h"
  22. #include <iostream>
  23. namespace love
  24. {
  25. Exception::Exception(const char *fmt, ...)
  26. {
  27. va_list args;
  28. int size_buffer = 256, size_out;
  29. char *buffer;
  30. while (true)
  31. {
  32. buffer = new char[size_buffer];
  33. memset(buffer, 0, size_buffer);
  34. va_start(args, fmt);
  35. size_out = vsnprintf(buffer, size_buffer, fmt, args);
  36. va_end(args);
  37. // see http://perfec.to/vsnprintf/pasprintf.c
  38. // if size_out ...
  39. // == -1 --> output was truncated
  40. // == size_buffer --> output was truncated
  41. // == size_buffer-1 --> ambiguous, /may/ have been truncated
  42. // > size_buffer --> output was truncated, and size_out
  43. // bytes would have been written
  44. if (size_out == size_buffer || size_out == -1 || size_out == size_buffer-1)
  45. size_buffer *= 2;
  46. else if (size_out > size_buffer)
  47. size_buffer = size_out + 2; // to avoid the ambiguous case
  48. else
  49. break;
  50. delete[] buffer;
  51. }
  52. message = std::string(buffer);
  53. delete[] buffer;
  54. }
  55. Exception::~Exception() throw()
  56. {
  57. }
  58. }