Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
| 4 | #include "stringprintf.h" |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | void StringAppendV(std::string* dst, const char* format, va_list ap) { |
| 9 | // First try with a small fixed size buffer |
| 10 | char space[1024]; |
| 11 | |
| 12 | // It's possible for methods that use a va_list to invalidate |
| 13 | // the data in it upon use. The fix is to make a copy |
| 14 | // of the structure before using it and use that copy instead. |
| 15 | va_list backup_ap; |
| 16 | va_copy(backup_ap, ap); |
| 17 | int result = vsnprintf(space, sizeof(space), format, backup_ap); |
| 18 | va_end(backup_ap); |
| 19 | |
| 20 | if (result < int(sizeof(space))) { |
| 21 | if (result >= 0) { |
| 22 | // Normal case -- everything fit. |
| 23 | dst->append(space, result); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | if (result < 0) { |
| 28 | // Just an error. |
| 29 | return; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Increase the buffer size to the size requested by vsnprintf, |
| 34 | // plus one for the closing \0. |
| 35 | int length = result+1; |
| 36 | char* buf = new char[length]; |
| 37 | |
| 38 | // Restore the va_list before we use it again |
| 39 | va_copy(backup_ap, ap); |
| 40 | result = vsnprintf(buf, length, format, backup_ap); |
| 41 | va_end(backup_ap); |
| 42 | |
| 43 | if (result >= 0 && result < length) { |
| 44 | // It fit |
| 45 | dst->append(buf, result); |
| 46 | } |
| 47 | delete[] buf; |
| 48 | } |
| 49 | |
| 50 | std::string StringPrintf(const char* fmt, ...) { |
| 51 | va_list ap; |
| 52 | va_start(ap, fmt); |
| 53 | std::string result; |
| 54 | StringAppendV(&result, fmt, ap); |
| 55 | va_end(ap); |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | void StringAppendF(std::string* dst, const char* format, ...) { |
| 60 | va_list ap; |
| 61 | va_start(ap, format); |
| 62 | StringAppendV(dst, format, ap); |
| 63 | va_end(ap); |
| 64 | } |