fixes for ultra fail not C99 supporting VC wannebe compiler

Mon, 19 Aug 2013 13:41:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 19 Aug 2013 13:41:53 +0200
changeset 150
1cf2eabf94ed
parent 149
3bf87676d42d
child 151
fc8d05972af7

fixes for ultra fail not C99 supporting VC wannebe compiler

ucx/utils.c file | annotate | diff | comparison | revisions
ucx/utils.h file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/utils.c	Mon Aug 19 11:54:54 2013 +0200
     1.2 +++ b/ucx/utils.c	Mon Aug 19 13:41:53 2013 +0200
     1.3 @@ -134,11 +134,17 @@
     1.4  
     1.5  /* PRINTF FUNCTIONS */
     1.6  
     1.7 +#ifdef va_copy
     1.8  #define UCX_PRINTF_BUFSIZE 256
     1.9 +#else
    1.10 +#pragma message("WARNING: C99 va_copy macro not supported by this platform" \
    1.11 +                " - limiting ucx_*printf to 2 KiB")
    1.12 +#define UCX_PRINTF_BUFSIZE 0x800
    1.13 +#endif
    1.14  
    1.15  int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) {
    1.16 +    int ret;
    1.17      va_list ap;
    1.18 -    int ret;
    1.19      va_start(ap, fmt);
    1.20      ret = ucx_vfprintf(stream, wfc, fmt, ap);
    1.21      va_end(ap);
    1.22 @@ -147,6 +153,7 @@
    1.23  
    1.24  int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) {
    1.25      char buf[UCX_PRINTF_BUFSIZE];
    1.26 +#ifdef va_copy
    1.27      va_list ap2;
    1.28      va_copy(ap2, ap);
    1.29      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    1.30 @@ -167,13 +174,23 @@
    1.31          }
    1.32          
    1.33          ret = vsnprintf(newbuf, len, fmt, ap2);
    1.34 -        va_end(ap2);
    1.35          if (ret > 0) {
    1.36              ret = (int)wfc(newbuf, 1, ret, stream);
    1.37          }
    1.38          free(newbuf);
    1.39      }
    1.40      return ret;
    1.41 +#else
    1.42 +    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    1.43 +    if (ret < 0) {
    1.44 +        return ret;
    1.45 +    } else if (ret < UCX_PRINTF_BUFSIZE) {
    1.46 +        return (int)wfc(buf, 1, ret, stream);
    1.47 +    } else {
    1.48 +        errno = ENOMEM;
    1.49 +        return -1;
    1.50 +    }
    1.51 +#endif
    1.52  }
    1.53  
    1.54  sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) {
    1.55 @@ -189,9 +206,10 @@
    1.56      sstr_t s;
    1.57      s.ptr = NULL;
    1.58      s.length = 0;
    1.59 +    char buf[UCX_PRINTF_BUFSIZE];
    1.60 +#ifdef va_copy
    1.61      va_list ap2;
    1.62      va_copy(ap2, ap);
    1.63 -    char buf[UCX_PRINTF_BUFSIZE];
    1.64      int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    1.65      if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
    1.66          s.ptr = (char*)a->malloc(a->pool, ret + 1);
    1.67 @@ -204,7 +222,6 @@
    1.68          int len = ret + 1;
    1.69          s.ptr = (char*)a->malloc(a->pool, len);
    1.70          ret = vsnprintf(s.ptr, len, fmt, ap2);
    1.71 -        va_end(ap2);
    1.72          if (ret < 0) {
    1.73              free(s.ptr);
    1.74              s.ptr = NULL;
    1.75 @@ -212,5 +229,16 @@
    1.76              s.length = (size_t)ret;
    1.77          }
    1.78      }
    1.79 +#else
    1.80 +    int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap);
    1.81 +    if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) {
    1.82 +        s.ptr = (char*)a->malloc(a->pool, ret + 1);
    1.83 +        s.length = (size_t)ret;
    1.84 +        memcpy(s.ptr, buf, ret);
    1.85 +        s.ptr[s.length] = '\0';
    1.86 +    } else {
    1.87 +        errno = ENOMEM;
    1.88 +    }
    1.89 +#endif
    1.90      return s;
    1.91  }
     2.1 --- a/ucx/utils.h	Mon Aug 19 11:54:54 2013 +0200
     2.2 +++ b/ucx/utils.h	Mon Aug 19 13:41:53 2013 +0200
     2.3 @@ -36,9 +36,9 @@
     2.4   */
     2.5  
     2.6  #ifndef UCX_UTILS_H
     2.7 -#define	UCX_UTILS_H
     2.8 +#define UCX_UTILS_H
     2.9  
    2.10 -#ifdef	__cplusplus
    2.11 +#ifdef __cplusplus
    2.12  extern "C" {
    2.13  #endif
    2.14  
    2.15 @@ -242,9 +242,9 @@
    2.16  #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \
    2.17          (write_func)ucx_buffer_write, __VA_ARGS__)
    2.18  
    2.19 -#ifdef	__cplusplus
    2.20 +#ifdef __cplusplus
    2.21  }
    2.22  #endif
    2.23  
    2.24 -#endif	/* UCX_UTILS_H */
    2.25 +#endif /* UCX_UTILS_H */
    2.26  

mercurial