X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglprint.c;h=8dacdb338b1834765dcb25554835cea5fb31343e;hb=03c86c2f632b642aa94f721e326787e91aa69c25;hp=fbf6e0e07e35bceb47ecc824bb4552f25feb6d95;hpb=6475e8951e1901b247f5d1d1dd9fcfc1e77e163a;p=gldbg.git diff --git a/source/glprint.c b/source/glprint.c index fbf6e0e..8dacdb3 100644 --- a/source/glprint.c +++ b/source/glprint.c @@ -7,17 +7,22 @@ Distributed under the GPL #include #include +#include +#include +#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,18 +33,20 @@ 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; + + return dec; } char *glprint_get_buffer(GlDecoder *dec) @@ -47,17 +54,85 @@ 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; + + if(!data) + return "NULL"; + + 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; i0) + { + *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 "gensrc/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"