Added throw specification.
[libsex.git] / src / utility.cxx
blobbda28a711ad5c54e9fc819cb411345bbfc887792
1 #include <libsex/utility.hxx>
2 #include <cassert>
4 namespace libsex {
6 bool vformat(
7 char* const buffer,
8 size_t length,
9 const char* const file,
10 unsigned short line,
11 const char* const message,
12 va_list ap) throw()
14 size_t chars
15 = snprintf(
16 buffer, length,
17 "%s:%d: ", file, line);
19 // If buffer was too small, chars contains length
20 // of chars that would've been written. Chars must
21 // be lower than length or else we could not fit
22 // the message into buffer, too.
23 bool didFitIntoBuffer = chars < length;
25 if (didFitIntoBuffer) {
26 size_t newLength = length - chars;
27 char* const newBuffer = buffer + chars;
28 chars = vsnprintf(
29 newBuffer, newLength,
30 message, ap);
31 // See comment above.
32 didFitIntoBuffer = chars <= newLength;
35 return didFitIntoBuffer;
39 bool format(
40 char* const buffer,
41 size_t length,
42 const char* const file,
43 unsigned short line,
44 const char* const message,
45 ...) throw()
47 va_list ap;
48 va_start(ap, message);
50 bool result = vformat(
51 buffer, length, file, line, message, ap);
53 va_end(ap);
54 return result;
57 }; // namespace