]> git.tdb.fi Git - gldbg.git/blob - source/enums.c
Replace per-file license notices with License.txt
[gldbg.git] / source / enums.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "enums.h"
5 #include "tmpalloc.h"
6
7 typedef struct sEnumInfo
8 {
9         GLenum value;
10         const char *category;
11         const char *name;
12 } EnumInfo;
13
14 #include "gensrc/enums.table"
15
16 const char *describe_enum(GLenum value, const char *categ)
17 {
18         unsigned low = 0;
19         unsigned high = enum_count;
20         char *ptr;
21
22         while(high>low+1)
23         {
24                 unsigned mid = (low+high)/2;
25                 int cmp;
26
27                 if(enums[mid].value<value)
28                         cmp = -1;
29                 else if(enums[mid].value>value)
30                         cmp = 1;
31                 else
32                         cmp = strcmp(enums[mid].category, categ);
33
34                 if(cmp<0)
35                         low = mid;
36                 else if(cmp>0)
37                         high = mid;
38                 else
39                         return enums[mid].name;
40         }
41
42         if(enums[low].value==value)
43                 return enums[low].name;
44         if(enums[high].value==value)
45                 return enums[high].name;
46
47         ptr = (char *)tmpalloc(20);
48         snprintf(ptr, 20, "GLenum(0x%X)", value);
49         return ptr;
50 }
51
52 const char *describe_bitfield(int mask, const char *categ)
53 {
54         int bit;
55         char *buf = (char *)tmpalloc(200);
56         char *ptr = buf;
57
58         for(bit=1; bit; bit<<=1)
59                 if(mask&bit)
60                 {
61                         const char *bdesc = describe_enum(bit, categ);
62                         if(ptr!=buf)
63                                 *ptr++ = '|';
64                         while(*bdesc)
65                                 *ptr++ = *bdesc++;
66                 }
67         *ptr = 0;
68         return buf;
69 }