os_stream: add printf facility
authorLuca Barbieri <luca@luca-barbieri.com>
Fri, 20 Aug 2010 09:31:24 +0000 (11:31 +0200)
committerLuca Barbieri <luca@luca-barbieri.com>
Fri, 20 Aug 2010 16:18:28 +0000 (18:18 +0200)
src/gallium/auxiliary/Makefile
src/gallium/auxiliary/SConscript
src/gallium/auxiliary/os/os_stream.c [new file with mode: 0644]
src/gallium/auxiliary/os/os_stream.h
src/gallium/auxiliary/os/os_stream_log.c
src/gallium/auxiliary/os/os_stream_null.c
src/gallium/auxiliary/os/os_stream_stdc.c
src/gallium/auxiliary/os/os_stream_str.c

index 2dae47927594ed1e14ad7726492b0937bb259f6b..7bd6a33a19d77979133f7e268b1ecfcd806e55e5 100644 (file)
@@ -47,6 +47,7 @@ C_SOURCES = \
        indices/u_indices_gen.c \
        indices/u_unfilled_gen.c \
        os/os_misc.c \
+       os/os_stream.c \
        os/os_stream_log.c \
        os/os_stream_stdc.c \
        os/os_stream_str.c \
index 43774e33110d39b439d9e2aac617db95ae647280..0ece469f365a512bf0c73b614587ff06ea4579cf 100644 (file)
@@ -95,6 +95,7 @@ source = [
     'indices/u_indices_gen.c',
     'indices/u_unfilled_gen.c',
     'os/os_misc.c',
+    'os/os_stream.c',
     'os/os_stream_log.c',
     'os/os_stream_stdc.c',
     'os/os_stream_str.c',
diff --git a/src/gallium/auxiliary/os/os_stream.c b/src/gallium/auxiliary/os/os_stream.c
new file mode 100644 (file)
index 0000000..2d4e185
--- /dev/null
@@ -0,0 +1,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;
+}
index 693a0621e2d374c215ee65026569723ef053c0a0..6c6050bb0284d5ae159c1f141a73f058697ab7c5 100644 (file)
@@ -50,6 +50,9 @@ struct os_stream
 
    void
    (*flush)(struct os_stream *stream);
+
+   int
+   (*vprintf)(struct os_stream *stream, const char* format, va_list ap);
 };
 
 
@@ -90,6 +93,27 @@ os_stream_flush(struct os_stream *stream)
    stream->flush(stream);
 }
 
+int
+os_default_stream_vprintf (struct os_stream* stream, const char *format, va_list ap);
+
+static INLINE int
+os_stream_vprintf (struct os_stream* stream, const char *format, va_list ap)
+{
+   return stream->vprintf(stream, format, ap);
+}
+
+static INLINE int
+os_stream_printf (struct os_stream* stream, const char *format, ...)
+{
+   int retval;
+   va_list args;
+
+   va_start (args, format);
+   retval = stream->vprintf(stream, format, args);
+   va_end (args);
+
+   return retval;
+}
 
 struct os_stream *
 os_file_stream_create(const char *filename);
@@ -118,5 +142,4 @@ os_str_stream_get_and_close(struct os_stream *stream);
 #define os_file_stream_create(_filename) os_null_stream_create()
 #endif
 
-
 #endif /* _OS_STREAM_H_ */
index 7cc2028a22cd643de05027d4b46ae2e6ee67de9f..b01377c346833af25febc3f99b518f161f8fbdd8 100644 (file)
@@ -73,7 +73,8 @@ static struct os_stream
 os_log_stream_struct = {
    &os_log_stream_close,
    &os_log_stream_write,
-   &os_log_stream_flush
+   &os_log_stream_flush,
+   &os_default_stream_vprintf,
 };
 
 
index 128c4e8f0e097159e4241c7cc25ed15c8133a228..a549a789e620638083ee7c754581ce5d8af8889b 100644 (file)
@@ -56,12 +56,18 @@ os_null_stream_flush(struct os_stream *stream)
    (void)stream;
 }
 
+static int
+os_null_stream_vprintf (struct os_stream* stream, const char *format, va_list ap)
+{
+   return 0;
+}
 
 static struct os_stream
 os_null_stream = {
    &os_null_stream_close,
    &os_null_stream_write,
-   &os_null_stream_flush
+   &os_null_stream_flush,
+   &os_null_stream_vprintf
 };
 
 
index 9e7ed7110763d9c9d039bf4ad30c86f0ede62679..37e7d063e2bf4c7f1e9d7d3204b262956a8e2aa0 100644 (file)
@@ -83,6 +83,14 @@ os_stdc_stream_flush(struct os_stream *_stream)
    fflush(stream->file);
 }
 
+static int
+os_stdc_stream_vprintf (struct os_stream* _stream, const char *format, va_list ap)
+{
+   struct os_stdc_stream *stream = os_stdc_stream(_stream);
+
+   return vfprintf(stream->file, format, ap);
+}
+
 
 struct os_stream *
 os_file_stream_create(const char *filename)
@@ -96,6 +104,7 @@ os_file_stream_create(const char *filename)
    stream->base.close = &os_stdc_stream_close;
    stream->base.write = &os_stdc_stream_write;
    stream->base.flush = &os_stdc_stream_flush;
+   stream->base.vprintf = &os_stdc_stream_vprintf;
 
    stream->file = fopen(filename, "w");
    if(!stream->file)
index b5c7270d2ae08b6439cd6dbb833b42866227de7a..be9478b2a170ba099fb112fbad6d05dcce201d11 100644 (file)
@@ -118,6 +118,7 @@ os_str_stream_create(size_t size)
    stream->base.close = &os_str_stream_close;
    stream->base.write = &os_str_stream_write;
    stream->base.flush = &os_str_stream_flush;
+   stream->base.vprintf = &os_default_stream_vprintf;
 
    stream->str = os_malloc(size);
    if(!stream->str)