1/* Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. */
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
8
9#include "error_handling/string_stream.h"
10void ssinit(sstream_t* stream) {
11  stream->data = NULL;
12  stream->length = 0;
13}
14
15void ssfree(sstream_t* stream) {
16  free(stream->data);
17  stream->data = 0;
18  stream->length = 0;
19}
20
21int ssvprintf(sstream_t* stream, const char* format, va_list args) {
22  va_list hold;
23  int len;
24  char* outstr;
25
26  va_copy(hold, args);
27  len = vsnprintf(NULL, 0, format, args);
28
29  outstr = malloc(stream->length + len + 1);
30  if (stream->data) {
31    memcpy(outstr, stream->data, stream->length);
32    free(stream->data);
33  }
34
35  stream->data = outstr;
36  vsprintf(&stream->data[stream->length], format, hold);
37  stream->length += len;
38
39  return len;
40}
41
42int ssprintf(sstream_t* stream, const char* format, ...) {
43  int out;
44  va_list args;
45  va_start(args, format);
46  out = ssvprintf(stream, format, args);
47  va_end(args);
48
49  return out;
50}
51