libwreport  2.4
compat.h
00001 #ifndef WIBBLE_COMPAT_H
00002 #define WIBBLE_COMPAT_H
00003 
00004 #include "config.h"
00005 #include <cstdarg>
00006 #include <cstring>
00007 #include <cstdlib>
00008 #include <cmath>
00009 
00010 #if USE_OWN_VASPRINTF
00011 static int vasprintf (char **result, const char *format, va_list args)
00012 {
00013   const char *p = format;
00014   /* Add one to make sure that it is never zero, which might cause malloc
00015      to return NULL.  */
00016   int total_width = strlen (format) + 1;
00017   va_list ap;
00018 
00019   memcpy ((void *)&ap, (void *)&args, sizeof (va_list));
00020 
00021   while (*p != '\0')
00022     {
00023       if (*p++ == '%')
00024     {
00025       while (strchr ("-+ #0", *p))
00026         ++p;
00027       if (*p == '*')
00028         {
00029           ++p;
00030           total_width += abs (va_arg (ap, int));
00031         }
00032       else
00033         total_width += strtoul (p, (char **) &p, 10);
00034       if (*p == '.')
00035         {
00036           ++p;
00037           if (*p == '*')
00038         {
00039           ++p;
00040           total_width += abs (va_arg (ap, int));
00041         }
00042           else
00043           total_width += strtoul (p, (char **) &p, 10);
00044         }
00045       while (strchr ("hlL", *p))
00046         ++p;
00047       /* Should be big enough for any format specifier except %s and floats.  */
00048       total_width += 30;
00049       switch (*p)
00050         {
00051         case 'd':
00052         case 'i':
00053         case 'o':
00054         case 'u':
00055         case 'x':
00056         case 'X':
00057         case 'c':
00058           (void) va_arg (ap, int);
00059           break;
00060         case 'f':
00061         case 'e':
00062         case 'E':
00063         case 'g':
00064         case 'G':
00065           (void) va_arg (ap, double);
00066           /* Since an ieee double can have an exponent of 307, we'll
00067          make the buffer wide enough to cover the gross case. */
00068           total_width += 307;
00069           break;
00070         case 's':
00071           total_width += strlen (va_arg (ap, char *));
00072           break;
00073         case 'p':
00074         case 'n':
00075           (void) va_arg (ap, char *);
00076           break;
00077         }
00078       p++;
00079     }
00080     }
00081   *result = (char*)malloc (total_width);
00082   if (*result != NULL) {
00083     return vsprintf (*result, format, args);}
00084   else {
00085     return 0;
00086   }
00087 }
00088 static int asprintf (char **result, const char *format, ...)
00089 {
00090     va_list ap;
00091     va_start(ap, format);
00092     int res = vasprintf(result, format, ap);
00093     va_end(ap);
00094     return res;
00095 }
00096 #endif
00097 
00098 #endif