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