summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/os/os_stream.c
blob: 2d4e1852ba476542c8079501c70badb5fdc303f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "pipe/p_config.h"

#include "os_stream.h"
#include "util/u_memory.h"
#include "util/u_string.h"

int
os_default_stream_vprintf (struct os_stream* stream, const char *format, va_list ap)
{
   char buf[1024];
   int retval;

   retval = util_vsnprintf(buf, sizeof(buf), format, ap);
   if(retval <= 0)
   {}
   else if(retval < sizeof(buf))
      stream->write(stream, buf, retval);
   else
   {
      int alloc = sizeof(buf);
      char* str = NULL;
      for(;;)
      {
         alloc += alloc;
         if(str)
            FREE(str);
         str = MALLOC(alloc);
         if(!str)
            return -1;

         retval = util_vsnprintf(str, alloc, format, ap);
      } while(retval >= alloc);

      if(retval > 0)
         stream->write(stream, str, retval);
      FREE(str);
   }

   return retval;
}