Ejemplo n.º 1
0
/** See dropt_vsnprintf. */
int
dropt_snprintf(dropt_char* s, size_t n, const dropt_char* format, ...)
{
    int ret;
    va_list args;
    va_start(args, format);
    ret = dropt_vsnprintf(s, n, format, args);
    va_end(args);
    return ret;
}
Ejemplo n.º 2
0
/** dropt_vssprintf
  *
  *     Appends a formatted string with vprintf semantics to a
  *     dropt_stringstream.
  *
  * PARAMETERS:
  *     IN/OUT ss : The dropt_stringstream.
  *     IN format : printf-style format specifier.  Must not be NULL.
  *     IN args   : Arguments to insert into the formatted string.
  *
  * RETURNS:
  *     The number of characters written to the dropt_stringstream,
  *       excluding the NUL-terminator.
  *     Returns a negative value on error.
  */
int
dropt_vssprintf(dropt_stringstream * ss, const dropt_char * format, va_list args)
{
  int n;
  va_list argsCopy;
  assert(ss != NULL);
  assert(format != NULL);

  va_copy(argsCopy, args);
  n = dropt_vsnprintf(NULL, 0, format, argsCopy);
  va_end(argsCopy);

  if (n > 0) {
    size_t available = dropt_ssgetfreespace(ss);
    if ((unsigned int) n >= available) {
      /* It's possible that newSize < ss->maxSize if
       * GROWN_STRINGSTREAM_BUFFER_SIZE overflows, but it should be
       * safe since we'll recompute the available space.
       */
      size_t newSize = GROWN_STRINGSTREAM_BUFFER_SIZE(ss->maxSize, n);
      dropt_ssresize(ss, newSize);
      available = dropt_ssgetfreespace(ss);
    }
    assert(available > 0); /* Space always is reserved for NUL. */

    /* snprintf's family of functions return the number of characters
     * that would be output with a sufficiently large buffer, excluding
     * NUL.
     */
    n = dropt_vsnprintf(ss->string + ss->used, available, format, args);

    /* We couldn't allocate enough space. */
    if ((unsigned int) n >= available) {
      n = -1;
    }

    if (n > 0) {
      ss->used += n;
    }
  }
  return n;
}
Ejemplo n.º 3
0
/** dropt_vasprintf
  *
  *     Allocates a formatted string with vprintf semantics.
  *
  * PARAMETERS:
  *     IN format : printf-style format specifier.  Must not be NULL.
  *     IN args   : Arguments to insert into the formatted string.
  *
  * RETURNS:
  *     The formatted string, which is always NUL-terminated.  The caller
  *       is responsible for calling free() on it when no longer needed.
  *     Returns NULL on error.
  */
dropt_char *
dropt_vasprintf(const dropt_char * format, va_list args)
{
  dropt_char * s = NULL;
  int len;
  va_list argsCopy;
  assert(format != NULL);

  va_copy(argsCopy, args);
  len = dropt_vsnprintf(NULL, 0, format, argsCopy);
  va_end(argsCopy);

  if (len >= 0) {
    size_t n = len + 1 /* NUL */;
    s = dropt_safe_malloc(n, sizeof * s);
    if (s != NULL) {
      dropt_vsnprintf(s, n, format, args);
    }
  }

  return s;
}