summaryrefslogtreecommitdiff
path: root/ccss/ccss-property-parser.c
blob: 419d20d7298e5d6077d6a6dcbff05c467bb61f3a (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* vim: set ts=8 sw=8 noexpandtab: */

/* The `C' CSS Library.
 * Copyright (C) 2008 Robert Staudinger
 *
 * This  library is free  software; you can  redistribute it and/or
 * modify it  under  the terms  of the  GNU Lesser  General  Public
 * License  as published  by the Free  Software  Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed  in the hope that it will be useful,
 * but  WITHOUT ANY WARRANTY; without even  the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License  along  with  this library;  if not,  write to  the Free
 * Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <string.h>
#include <glib.h>
#include <libcroco/libcroco.h>
#include "ccss-grammar.h"
#include "ccss-property-generic.h"
#include "ccss-property-impl.h"
#include "ccss-property-parser.h"
#include "config.h"

static ccss_property_class_t const *
peek_property_class (void);

static bool
property_factory (ccss_grammar_t const	*grammar,
		  ccss_block_t		*block,
		  char const		*name,
		  CRTerm const		*values,
		  void			*user_data)
{
	ccss_property_generic_t	*property, p;

	memset (&p, 0, sizeof (p));

	p.base.vtable = peek_property_class ();
	p.base.state = ccss_property_parse_state (&values);
	p.name = g_strdup (name);
	if (p.base.state == CCSS_PROPERTY_STATE_SET) {
		p.values = (CRTerm *) values;
		cr_term_ref (p.values);
	}

	property = g_new0 (ccss_property_generic_t, 1);
	*property = p;
	ccss_block_add_property (block, name, &property->base);

	return true;
}

static void
property_destroy (ccss_property_generic_t *self)
{
	g_return_if_fail (self);

	g_free (self->name);

	if (self->values) {
		cr_term_unref (self->values);
	}

	g_free (self);
}

static bool
property_convert (ccss_property_generic_t	*self,
		  ccss_property_type_t		 target,
		  void				*value)
{
	char const	*str;
	double		 num;

	g_return_val_if_fail (self && value, false);

	switch (target) {
	case CCSS_PROPERTY_TYPE_DOUBLE:
		if (self->values->type == TERM_NUMBER) {
			* (double *) value = self->values->content.num->val;
			return true;
		}
		g_warning (G_STRLOC "Cannot convert property '%s' to 'double'",
			   self->name);
		return false;
		break;
	case CCSS_PROPERTY_TYPE_STRING:
		switch (self->base.state) {
		case CCSS_PROPERTY_STATE_NONE:
			* (char **) value = g_strdup ("none");
			return true;
		case CCSS_PROPERTY_STATE_INHERIT:
			* (char **) value = g_strdup ("inherit");
			return true;
		case CCSS_PROPERTY_STATE_SET:
			if (self->values == NULL) {
				g_warning (G_STRLOC "NULL property '%s'",
					   self->name);
				return false;			
			}
			break;
		case CCSS_PROPERTY_STATE_INVALID:
		default:
			g_warning (G_STRLOC "Invalid property '%s'",
				   self->name);
			return false;
		}

		switch (self->values->type) {
		case TERM_NUMBER:
			num = self->values->content.num->val;
			* (char **) value = g_strdup_printf ("%f", num);
			return true;
		case TERM_HASH:
			str = cr_string_peek_raw_str (self->values->content.str);
			* (char **) value = g_strdup_printf ("#%s", str);
			return true;
		case TERM_IDENT: /* Fall thru. */
		case TERM_STRING:
			str = cr_string_peek_raw_str (self->values->content.str);
			* (char **) value = g_strdup (str);
			return true;
		default:
			g_warning (G_STRLOC "Unhandled property type '%d'",
				   self->values->type);
			return false;
		}

		break;
	default:
		g_assert_not_reached ();
		return false;
	}

	return true;
}

ccss_property_class_t const _ptable[] = {
  { 
	.name = "*",
	.create = NULL,
	.destroy = (ccss_property_destroy_f) property_destroy,
	.convert = (ccss_property_convert_f) property_convert,
	.factory = property_factory,
	.inherit = NULL,
	.serialize = NULL
  }, { 
	.name = NULL
  }
};

static ccss_property_class_t const *
peek_property_class (void)
{
	return &_ptable[0];
}

ccss_property_class_t const *
ccss_property_parser_get_property_classes (void)
{
	return _ptable;
}