Changed "double fault" message.
[libsex.git] / source / utility.cxx
blobfba45acbabd173f3ccf34086b8895cbcdf200b8a
1 #include <source/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;
38 bool format(
39 char* const buffer,
40 size_t length,
41 const char* const file,
42 unsigned short line,
43 const char* const message,
44 ...) throw()
46 va_list ap;
47 va_start(ap, message);
49 bool result = vformat(
50 buffer, length, file, line, message, ap);
52 va_end(ap);
53 return result;
56 } // namespace