]> git.tdb.fi Git - gldbg.git/blobdiff - source/glprint.c
Print the contents of arrays and references
[gldbg.git] / source / glprint.c
index ec8d47e5f007c0a9b5ba4a61fd77dedad556314b..9721463eff76a4f552f954886553b4cf8f6c244c 100644 (file)
@@ -7,17 +7,21 @@ Distributed under the GPL
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
+#include "arraysize.h"
 #include "enums.h"
 #include "glprint.h"
+#include "tmpalloc.h"
 
 typedef struct sGlPrintData
 {
        char *buffer;
        unsigned bufsize;
+       int free_buf;
 } GlPrintData;
 
 static void init_print(GlDecoder *);
-static void glprint_data_free(void *);
+static void glprint_free(void *);
 static void print_gldError(void *, GLenum);
 
 GlDecoder *glprint_new(char *buffer, unsigned bufsize)
@@ -28,15 +32,15 @@ GlDecoder *glprint_new(char *buffer, unsigned bufsize)
        gpd = (GlPrintData *)malloc(sizeof(GlPrintData));
        gpd->buffer = buffer;
        gpd->bufsize = bufsize;
+       gpd->free_buf = 0;
        if(!gpd->buffer)
        {
                if(!gpd->bufsize)
                        gpd->bufsize = 1024;
                gpd->buffer = (char *)malloc(gpd->bufsize);
-               dec = gldecoder_new(gpd, glprint_data_free);
+               gpd->free_buf = 1;
        }
-       else
-               dec = gldecoder_new(gpd, free);
+       dec = gldecoder_new(gpd, glprint_free);
 
        init_print(dec);
        dec->gldError = print_gldError;
@@ -49,17 +53,82 @@ char *glprint_get_buffer(GlDecoder *dec)
        return ((GlPrintData *)dec->user_data)->buffer;
 }
 
-static void glprint_data_free(void *data)
+static void glprint_free(void *data)
 {
        GlPrintData *gpd = (GlPrintData *)data;
-       free(gpd->buffer);
-       free(gpd);
+       if(gpd->free_buf)
+       {
+               free(gpd->buffer);
+               free(gpd);
+       }
+       tmpfree();
 }
 
+static const char *print_array(const char *fmt, const void *data, unsigned elem_size, unsigned count)
+{
+       const char *cptr;
+       char type = 0;
+       char *buffer;
+       unsigned buf_size;
+       char *ptr;
+       unsigned i;
+
+       for(cptr=fmt; (type<2 && *cptr); ++cptr)
+       {
+               if(*cptr=='%')
+                       type = 1;
+               else if(type==1 && isalpha(*cptr))
+                       type = *cptr;
+       }
+
+       count /= elem_size;
+       buf_size = count*20;
+       if(type=='s')
+               buf_size *= 50;
+       buffer = tmpalloc(buf_size);
+       ptr = buffer;
+       *ptr++ = '{';
+       for(i=0; i<count; ++i)
+       {
+               long long element = 0;
+               unsigned len;
+
+               if(i>0)
+               {
+                       *ptr++ = ',';
+                       *ptr++ = ' ';
+               }
+               memcpy(&element, (const char *)data+i*elem_size, elem_size);
+               if(type>='e' && type<='g' && elem_size==sizeof(float))
+                       *(double *)&element = *(float *)&element;
+               len = snprintf(ptr, buf_size, fmt, element);
+               ptr += len;
+               buf_size -= len;
+       }
+       *ptr++ = '}';
+       *ptr = 0;
+
+       return buffer;
+}
+
+static const char *print_data(const void *data, unsigned size)
+{
+       if(!data)
+               return "NULL";
+       else if(!size)
+               return "/* data */";
+       else
+       {
+               char *buffer = tmpalloc(50);
+               snprintf(buffer, 50, "/* data: %d bytes */", size);
+               return buffer;
+       }
+}
+
+#include "glprint.funcs"
+
 static void print_gldError(void *user_data, GLenum code)
 {
        GlPrintData *gpd = (GlPrintData *)user_data;
        snprintf(gpd->buffer, gpd->bufsize, "ERROR: %s", describe_enum(code, "ErrorCode"));
 }
-
-#include "glprint.funcs"