]> git.tdb.fi Git - gldbg.git/blob - source/gldecoder.c
Initial revision
[gldbg.git] / source / gldecoder.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 <string.h>
10 #include "functions.h"
11 #include "gldecoder.h"
12
13 GlDecoder *gldecoder_new(void *user_data, void (*destroy)(void *))
14 {
15         GlDecoder *dec;
16
17         dec = (GlDecoder *)malloc(sizeof(GlDecoder));
18         memset(dec, 0, sizeof(GlDecoder));
19         dec->user_data = user_data;
20         dec->destroy = destroy;
21
22         return dec;
23 }
24
25 void gldecoder_delete(GlDecoder *dec)
26 {
27         if(dec->destroy)
28                 dec->destroy(dec->user_data);
29         free(dec);
30 }
31
32 unsigned read_char(char *v, const char *data, unsigned len)
33 {
34         *v = *data;
35         return 1;
36 }
37
38 unsigned read_short(short *v, const char *data, unsigned len)
39 {
40         *v = *(short *)data;
41         return sizeof(short);
42 }
43
44 unsigned read_int(int *v, const char *data, unsigned len)
45 {
46         *v = *(int *)data;
47         return sizeof(int);
48 }
49
50 unsigned read_long(long *v, const char *data, unsigned len)
51 {
52         *v = *(long *)data;
53         return sizeof(long);
54 }
55
56 unsigned read_ulong(unsigned long *v, const char *data, unsigned len)
57 {
58         *v = *(unsigned long *)data;
59         return sizeof(unsigned long);
60 }
61
62 unsigned read_longlong(long long *v, const char *data, unsigned len)
63 {
64         *v = *(long long *)data;
65         return sizeof(long long);
66 }
67
68 unsigned read_float(float *v, const char *data, unsigned len)
69 {
70         *v = *(float *)data;
71         return sizeof(float);
72 }
73
74 unsigned read_double(double *v, const char *data, unsigned len)
75 {
76         *v = *(double *)data;
77         return sizeof(double);
78 }
79
80 unsigned read_pointer(void **v, const char *data, unsigned len)
81 {
82         *v = *(void **)data;
83         return sizeof(void *);
84 }
85
86 unsigned read_data(const void **v, const char *data, unsigned len)
87 {
88         int vlen;
89         unsigned pos = 0;
90         pos += read_int(&vlen, data, len);
91         *v = data+pos;
92         return pos+vlen;
93 }
94
95 unsigned read_string(const unsigned char **v, const char *data, unsigned len)
96 {
97         return read_data((const void **)v, data, len);
98 }
99
100 #include "gldecoder.funcs"