Ejemplo n.º 1
0
/** dropt_ssclear
  *
  *     Clears and re-initializes a dropt_stringstream.
  *
  * PARAMETERS:
  *     IN/OUT ss : The dropt_stringstream
  */
void
dropt_ssclear(dropt_stringstream* ss)
{
    assert(ss != NULL);

    ss->string[0] = DROPT_TEXT_LITERAL('\0');
    ss->used = 0;

    dropt_ssresize(ss, default_stringstream_buffer_size);
}
Ejemplo n.º 2
0
/** dropt_ssfinalize
  *
  *     Finalizes a dropt_stringstream; returns the contained string and
  *     destroys the dropt_stringstream.
  *
  * PARAMETERS:
  *     IN/OUT ss : The dropt_stringstream.
  *
  * RETURNS:
  *     The dropt_stringstream's string, which is always NUL-terminated.
  *       Note that the caller assumes ownership of the returned string and
  *       is responsible for calling free() on it when no longer needed.
  */
dropt_char*
dropt_ssfinalize(dropt_stringstream* ss)
{
    dropt_char* s;
    assert(ss != NULL);

    /* Shrink to fit. */
    dropt_ssresize(ss, 0);

    s = ss->string;
    ss->string = NULL;

    dropt_ssclose(ss);

    return s;
}
Ejemplo n.º 3
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;
}