Exception.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Copyright (c) 2006-2012 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 "Exception.h"
  21. #include "common/config.h"
  22. #include <iostream>
  23. using namespace std;
  24. namespace love
  25. {
  26. Exception::Exception(const char *fmt, ...)
  27. {
  28. va_list args;
  29. int size_buffer = 256, size_out;
  30. char *buffer;
  31. while (true)
  32. {
  33. buffer = new char[size_buffer];
  34. memset(buffer, 0, size_buffer);
  35. va_start(args, fmt);
  36. size_out = vsnprintf(buffer, size_buffer, fmt, args);
  37. va_end(args);
  38. // see http://perfec.to/vsnprintf/pasprintf.c
  39. // if size_out ...
  40. // == -1 --> output was truncated
  41. // == size_buffer --> output was truncated
  42. // == size_buffer-1 --> ambiguous, /may/ have been truncated
  43. // > size_buffer --> output was truncated, and size_out
  44. // bytes would have been written
  45. if (size_out == size_buffer || size_out == -1 || size_out == size_buffer-1)
  46. size_buffer *= 2;
  47. else if (size_out > size_buffer)
  48. size_buffer = size_out + 2; // to avoid the ambiguous case
  49. else
  50. break;
  51. delete[] buffer;
  52. }
  53. message = std::string(buffer);
  54. delete[] buffer;
  55. }
  56. }