]> git.tdb.fi Git - gldbg.git/blob - source/glprint.c
dff66f47f080156cf08b6958e9d68c66654ba560
[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 #define UNUSED __attribute__((unused))
18
19 typedef struct sGlPrintData
20 {
21         char *buffer;
22         unsigned bufsize;
23         int free_buf;
24 } GlPrintData;
25
26 static void init_print(GlDecoder *);
27 static void glprint_free(void *);
28 static void print_gldError(void *, GLenum);
29 static void print_unhandled(void *, unsigned short);
30
31 GlDecoder *glprint_new(char *buffer, unsigned bufsize)
32 {
33         GlDecoder *dec;
34         GlPrintData *gpd;
35
36         gpd = (GlPrintData *)malloc(sizeof(GlPrintData));
37         gpd->buffer = buffer;
38         gpd->bufsize = bufsize;
39         gpd->free_buf = 0;
40         if(!gpd->buffer)
41         {
42                 if(!gpd->bufsize)
43                         gpd->bufsize = 1024;
44                 gpd->buffer = (char *)malloc(gpd->bufsize);
45                 gpd->free_buf = 1;
46         }
47         dec = gldecoder_new(gpd, glprint_free);
48
49         init_print(dec);
50         dec->gldError = print_gldError;
51         dec->unhandled = print_unhandled;
52
53         return dec;
54 }
55
56 char *glprint_get_buffer(GlDecoder *dec)
57 {
58         return ((GlPrintData *)dec->user_data)->buffer;
59 }
60
61 static void glprint_free(void *data)
62 {
63         GlPrintData *gpd = (GlPrintData *)data;
64         if(gpd->free_buf)
65         {
66                 free(gpd->buffer);
67                 free(gpd);
68         }
69         tmpfree();
70 }
71
72 static const char *print_array(const char *fmt, const void *data, unsigned elem_size, unsigned count)
73 {
74         const char *cptr;
75         char type = 0;
76         char *buffer;
77         unsigned buf_size;
78         char *ptr;
79         unsigned i;
80
81         if(!data)
82                 return "NULL";
83
84         for(cptr=fmt; (type<2 && *cptr); ++cptr)
85         {
86                 if(*cptr=='%')
87                         type = 1;
88                 else if(type==1 && isalpha(*cptr))
89                         type = *cptr;
90         }
91
92         count /= elem_size;
93         buf_size = count*20;
94         if(type=='s')
95                 buf_size *= 50;
96         buffer = tmpalloc(buf_size);
97         ptr = buffer;
98         *ptr++ = '{';
99         for(i=0; i<count; ++i)
100         {
101                 long long element = 0;
102                 unsigned len;
103
104                 if(i>0)
105                 {
106                         *ptr++ = ',';
107                         *ptr++ = ' ';
108                 }
109                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
110                 if(type>='e' && type<='g' && elem_size==sizeof(float))
111                         *(double *)&element = *(float *)&element;
112                 len = snprintf(ptr, buf_size, fmt, element);
113                 ptr += len;
114                 buf_size -= len;
115         }
116         *ptr++ = '}';
117         *ptr = 0;
118
119         return buffer;
120 }
121
122 static const char *print_array_described(const char *(*describe)(GLenum, const char *), const char *categ, const void *data, unsigned elem_size, unsigned count)
123 {
124         char *buffer;
125         unsigned buf_size;
126         char *ptr;
127         unsigned i;
128
129         if(!data)
130                 return NULL;
131
132         count /= elem_size;
133         buf_size = count*50;
134         buffer = tmpalloc(buf_size);
135         ptr = buffer;
136         *ptr++ = '{';
137         for(i=0; i<count; ++i)
138         {
139                 int element = 0;
140                 unsigned len;
141
142                 if(i>0)
143                 {
144                         *ptr++ = ',';
145                         *ptr++ = ' ';
146                 }
147                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
148                 len = snprintf(ptr, buf_size, "%s", describe(element, categ));
149                 ptr += len;
150                 buf_size -= len;
151         }
152         *ptr++ = '}';
153         *ptr = 0;
154
155         return buffer;
156 }
157
158 static const char *print_data(const void *data, unsigned size)
159 {
160         if(!data)
161                 return "NULL";
162         else if((unsigned long)data<0x100000)
163         {
164                 char *buffer = tmpalloc(20);
165                 snprintf(buffer, 20, "%p", data);
166                 return buffer;
167         }
168         else if(!size)
169                 return "/* data */";
170         else
171         {
172                 char *buffer = tmpalloc(50);
173                 snprintf(buffer, 50, "/* data: %d bytes */", size);
174                 return buffer;
175         }
176 }
177
178 #include "gensrc/glprint.funcs"
179
180 static void print_gldError(void *user_data, GLenum code)
181 {
182         GlPrintData *gpd = (GlPrintData *)user_data;
183         snprintf(gpd->buffer, gpd->bufsize, "ERROR: %s", describe_enum(code, "ErrorCode"));
184 }
185
186 static void print_unhandled(void *user_data, unsigned short func UNUSED)
187 {
188         GlPrintData *gpd = (GlPrintData *)user_data;
189         gpd->buffer[0] = 0;
190 }