/
style.c
103 lines (85 loc) · 2.03 KB
/
style.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "style.h"
#include <simple-tiles/util.h>
#include <simple-tiles/style.h>
VALUE cSimplerTilesStyle;
static simplet_style_t *
get_style(VALUE self){
simplet_style_t *style;
Data_Get_Struct(self, simplet_style_t, style);
return style;
}
static void
mark_style(void *style){
simplet_style_t *stl = style;
VALUE refs = (VALUE)simplet_style_get_user_data(stl);
if(refs) rb_gc_mark(refs);
}
static void
style_free(void *style){
simplet_style_t *stl = style;
simplet_style_free(stl);
}
static VALUE
alloc_style(VALUE klass){
simplet_style_t *style;
if(!(style = simplet_style_new(NULL, NULL)))
rb_fatal("Could not allocate space for a new SimplerTiles::Style in memory.");
return Data_Wrap_Struct(klass, mark_style, style_free, style);
}
/*
Set the arg for this Style.
@param (String)
@return (String)
*/
static VALUE
set_arg(VALUE self, VALUE arg){
Check_Type(arg, T_STRING);
simplet_style_t *style = get_style(self);
simplet_style_set_arg(style, RSTRING_PTR(arg));
return arg;
}
/*
Get the arg for this Style.
@return (String)
*/
static VALUE
get_arg(VALUE self){
simplet_style_t *style = get_style(self);
char *arg;
simplet_style_get_arg(style, &arg);
return rb_str_new2(arg);
}
/*
Set the key for this Style.
@param (String)
@return (String)
*/
static VALUE
set_key(VALUE self, VALUE key){
Check_Type(key, T_STRING);
simplet_style_t *style = get_style(self);
simplet_style_set_key(style, RSTRING_PTR(key));
return key;
}
/*
Get the key for this Style.
@param (String)
@return (String)
*/
static VALUE
get_key(VALUE self){
simplet_style_t *style = get_style(self);
char *key;
simplet_style_get_key(style, &key);
return rb_str_new2(key);
}
void
init_style(){
VALUE rstyle = rb_define_class_under(mSimplerTiles, "Style", rb_cObject);
rb_define_alloc_func(rstyle, alloc_style);
rb_define_method(rstyle, "arg=", set_arg, 1);
rb_define_method(rstyle, "arg", get_arg, 0);
rb_define_method(rstyle, "key=", set_key, 1);
rb_define_method(rstyle, "key", get_key, 0);
cSimplerTilesStyle = rstyle;
}