]> git.tdb.fi Git - gldbg.git/blob - source/glprint.c
Rewrite the Makefile to have proper dependencies and stuff
[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         if(!data)
78                 return "NULL";
79
80         for(cptr=fmt; (type<2 && *cptr); ++cptr)
81         {
82                 if(*cptr=='%')
83                         type = 1;
84                 else if(type==1 && isalpha(*cptr))
85                         type = *cptr;
86         }
87
88         count /= elem_size;
89         buf_size = count*20;
90         if(type=='s')
91                 buf_size *= 50;
92         buffer = tmpalloc(buf_size);
93         ptr = buffer;
94         *ptr++ = '{';
95         for(i=0; i<count; ++i)
96         {
97                 long long element = 0;
98                 unsigned len;
99
100                 if(i>0)
101                 {
102                         *ptr++ = ',';
103                         *ptr++ = ' ';
104                 }
105                 memcpy(&element, (const char *)data+i*elem_size, elem_size);
106                 if(type>='e' && type<='g' && elem_size==sizeof(float))
107                         *(double *)&element = *(float *)&element;
108                 len = snprintf(ptr, buf_size, fmt, element);
109                 ptr += len;
110                 buf_size -= len;
111         }
112         *ptr++ = '}';
113         *ptr = 0;
114
115         return buffer;
116 }
117
118 static const char *print_data(const void *data, unsigned size)
119 {
120         if(!data)
121                 return "NULL";
122         else if(!size)
123                 return "/* data */";
124         else
125         {
126                 char *buffer = tmpalloc(50);
127                 snprintf(buffer, 50, "/* data: %d bytes */", size);
128                 return buffer;
129         }
130 }
131
132 #include "gensrc/glprint.funcs"
133
134 static void print_gldError(void *user_data, GLenum code)
135 {
136         GlPrintData *gpd = (GlPrintData *)user_data;
137         snprintf(gpd->buffer, gpd->bufsize, "ERROR: %s", describe_enum(code, "ErrorCode"));
138 }