]> git.tdb.fi Git - gldbg.git/blob - source/glprint.c
Enable warnings and fix them
[gldbg.git] / source / glprint.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include "arraysize.h"
13 #include "enums.h"
14 #include "glprint.h"
15 #include "tmpalloc.h"
16
17 typedef struct sGlPrintData
18 {
19         char *buffer;
20         unsigned bufsize;
21         int free_buf;
22 } GlPrintData;
23
24 static void init_print(GlDecoder *);
25 static void glprint_free(void *);
26 static void print_gldError(void *, GLenum);
27
28 GlDecoder *glprint_new(char *buffer, unsigned bufsize)
29 {
30         GlDecoder *dec;
31         GlPrintData *gpd;
32
33         gpd = (GlPrintData *)malloc(sizeof(GlPrintData));
34         gpd->buffer = buffer;
35         gpd->bufsize = bufsize;
36         gpd->free_buf = 0;
37         if(!gpd->buffer)
38         {
39                 if(!gpd->bufsize)
40                         gpd->bufsize = 1024;
41                 gpd->buffer = (char *)malloc(gpd->bufsize);
42                 gpd->free_buf = 1;
43         }
44         dec = gldecoder_new(gpd, glprint_free);
45
46         init_print(dec);
47         dec->gldError = print_gldError;
48
49         return dec;
50 }
51
52 char *glprint_get_buffer(GlDecoder *dec)
53 {
54         return ((GlPrintData *)dec->user_data)->buffer;
55 }
56
57 static void glprint_free(void *data)
58 {
59         GlPrintData *gpd = (GlPrintData *)data;
60         if(gpd->free_buf)
61         {
62                 free(gpd->buffer);
63                 free(gpd);
64         }
65         tmpfree();
66 }
67
68 static const char *print_array(const char *fmt, const void *data, unsigned elem_size, unsigned count)
69 {
70         const char *cptr;
71         char type = 0;
72         char *buffer;
73         unsigned buf_size;
74         char *ptr;
75         unsigned i;
76
77         for(cptr=fmt; (type<2 && *cptr); ++cptr)
78         {
79                 if(*cptr=='%')
80                         type = 1;
81                 else if(type==1 && isalpha(*cptr))
82                         type = *cptr;
83         }
84
85         count /= elem_size;
86         buf_size = count*20;
87         if(type=='s')
88                 buf_size *= 50;
89         buffer = tmpalloc(buf_size);
90         ptr = buffer;
91         *ptr++ = '{';
92         for(i=0; i<count; ++i)
93         {
94                 long long element = 0;
95                 unsigned len;
96
97                 if(i>0)
98                 {
99                         *ptr++ = ',';
100                         *ptr++ = ' ';
101                 }
102                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
103                 if(type>='e' && type<='g' && elem_size==sizeof(float))
104                         *(double *)&element = *(float *)&element;
105                 len = snprintf(ptr, buf_size, fmt, element);
106                 ptr += len;
107                 buf_size -= len;
108         }
109         *ptr++ = '}';
110         *ptr = 0;
111
112         return buffer;
113 }
114
115 static const char *print_data(const void *data, unsigned size)
116 {
117         if(!data)
118                 return "NULL";
119         else if(!size)
120                 return "/* data */";
121         else
122         {
123                 char *buffer = tmpalloc(50);
124                 snprintf(buffer, 50, "/* data: %d bytes */", size);
125                 return buffer;
126         }
127 }
128
129 #include "glprint.funcs"
130
131 static void print_gldError(void *user_data, GLenum code)
132 {
133         GlPrintData *gpd = (GlPrintData *)user_data;
134         snprintf(gpd->buffer, gpd->bufsize, "ERROR: %s", describe_enum(code, "ErrorCode"));
135 }