Doc/syntax work.
[libsex.git] / source / utility.cxx
blobe038ebf0e4009b31552f36d5e6cf27d8345b3928
1 #include <source/utility.hxx>
2 #include <cassert>
4 bool libsex::vformat(
5 char* const buf,
6 size_t len,
7 const char* const file,
8 unsigned short line,
9 const char* const msg,
10 va_list ap) throw()
12 size_t chars = snprintf(buf, len, "%s:%d: ", file, line);
14 // If buffer was too small, chars contains length
15 // of chars that would've been written. Chars must
16 // be lower than length or else we could not fit
17 // the message into buffer, too.
18 bool didFitIntoBuffer = chars < len;
20 if (didFitIntoBuffer) {
21 size_t newLength = len - chars;
22 char* const newBuffer = buf + chars;
23 chars = vsnprintf(newBuffer, newLength, msg, ap);
24 didFitIntoBuffer = chars <= newLength;
27 return didFitIntoBuffer;
30 bool libsex::format(
31 char* const buf,
32 size_t len,
33 const char* const file,
34 unsigned short line,
35 const char* const msg,
36 ...) throw()
38 va_list ap;
39 va_start(ap, msg);
41 bool result = vformat(buf, len, file, line, msg, ap);
43 va_end(ap);
44 return result;