libsex  3.1.0
simple exception library (C++)
/home/ptr/dev/dev-libs/libsex/libsex/utility.cxx
Go to the documentation of this file.
00001 /**
00002  * @file
00003  *
00004  * Non-template definitions of formatting-related internal
00005  * functions.
00006  */
00007 
00008 #include <libsex/utility.hxx>
00009 #include <cassert>
00010 
00011 bool libsex::vformat(
00012         char* const buf,
00013         size_t len,
00014         const char* const file,
00015         unsigned short line,
00016         const char* const msg,
00017         va_list ap) throw()
00018 {
00019         size_t chars = snprintf(buf, len, "%s:%d: ", file, line);
00020 
00021         // If buffer was too small, chars contains length
00022         // of chars that would've been written. Chars must
00023         // be lower than length or else we could not fit
00024         // the message into buffer, too.
00025         bool didFitIntoBuffer = chars < len;
00026 
00027         if (didFitIntoBuffer) {
00028                 size_t      newLength = len - chars;
00029                 char* const newBuffer = buf + chars;
00030                 chars = vsnprintf(newBuffer, newLength, msg, ap);
00031                 didFitIntoBuffer = chars <= newLength;
00032         }
00033 
00034         return didFitIntoBuffer;
00035 }
00036 
00037 bool libsex::format(
00038         char* const buf,
00039         size_t len,
00040         const char* const file,
00041         unsigned short line,
00042         const char* const msg,
00043         ...) throw()
00044 {
00045         va_list ap;
00046         va_start(ap, msg);
00047 
00048         bool result = vformat(buf, len, file, line, msg, ap);
00049 
00050         va_end(ap);
00051         return result;
00052 }
 All Classes Files Functions Variables Defines