File: cssvalue.c

package info (click to toggle)
libgtkhtml2 2.11.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,740 kB
  • ctags: 3,830
  • sloc: ansic: 33,179; sh: 8,414; makefile: 597
file content (219 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
   Copyright (C) 2000 CodeFactory AB
   Copyright (C) 2000 Jonas Borgstr\366m <jonas@codefactory.se>
   Copyright (C) 2000 Anders Carlsson <andersca@codefactory.se>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include "cssvalue.h"

gchar *
css_value_to_string (CssValue *value)
{
	switch (value->value_type) {
	case CSS_IDENT:
		return g_strdup (html_atom_list_get_string (html_atom_list, value->v.atom));
		break;
	case CSS_STRING:
		return g_strdup (value->v.s);
		break;
	case CSS_NUMBER:
		return g_strdup_printf ("%f", value->v.d);
		break;
	default:
		return NULL;
	}
	return NULL;
}

CssValue *
css_value_dimension_new (gdouble d, CssValueType type)
{
	CssValue *result;

	result = g_new (CssValue, 1);
	result->ref_count = 1;
	result->value_type = type;
	result->v.d = d;

	return result;
}

CssValue *
css_value_function_new (HtmlAtom name, CssValue *args)
{
	CssValue *result;
	CssFunction *function;

	function = g_new (CssFunction, 1);
	function->name = name;
	function->args = args;
	
	result = g_new (CssValue, 1);
	result->ref_count = 1;
	result->value_type = CSS_FUNCTION;
	result->v.function = function;

	return result;
}

CssValue *
css_value_string_new (gchar *str)
{
	CssValue *result;

	result = g_new (CssValue, 1);
	result->ref_count = 1;
	result->value_type = CSS_STRING;
	result->v.s = g_strdup (str);

	return result;
}

CssValue *
css_value_ident_new (HtmlAtom atom)
{
	CssValue *result;

	result = g_new (CssValue, 1);
	result->ref_count = 1;
	result->value_type = CSS_IDENT;
	result->v.atom = atom;

	return result;
}

CssValue *
css_value_list_new (void)
{
	CssValue *result;

	result = g_new (CssValue, 1);
	result->ref_count = 1;
	result->value_type = CSS_VALUE_LIST;
	result->v.entry = NULL;

	return result;
}


gint
css_value_list_get_length (CssValue *list)
{
	CssValueEntry *entry;
	gint i = 0;
	
	if (list->value_type != CSS_VALUE_LIST)
		return -1;

	entry = list->v.entry;

	while (entry) {
		i++;
		entry = entry->next;
	}

	return i;
}

void
css_value_list_append (CssValue *list, CssValue *element, gchar list_sep)
{
	CssValueEntry *entry, *tmp_entry;
	
	if (list->value_type != CSS_VALUE_LIST)
		return;

	entry = g_new (CssValueEntry, 1);
	entry->value = element;
	entry->list_sep = list_sep;
	entry->next = NULL;
	
	if (list->v.entry == NULL)
		list->v.entry = entry;
	else {
		tmp_entry = list->v.entry;

		while (tmp_entry->next) {
			tmp_entry = tmp_entry->next;
		}

		tmp_entry->next = entry;
	}
}

void
css_value_unref (CssValue *val)
{
	CssValueEntry *entry;
	
	g_return_if_fail (val != NULL);

	val->ref_count--;
	
	if (val->ref_count == 0) {
		switch (val->value_type) {
		case CSS_STRING:
			g_free (val->v.s);
			break;
		case CSS_PX:
		case CSS_EMS:
		case CSS_IDENT:
		case CSS_NUMBER:
		case CSS_PERCENTAGE:
		case CSS_DEG:
		case CSS_IN:
		case CSS_PT:
		case CSS_EXS:
		case CSS_CM:
		case CSS_MM:
		case CSS_PC:
			break;
		case CSS_VALUE_LIST:
			entry = val->v.entry;

			while (entry) {
				CssValueEntry *tmp_entry = entry->next;

				css_value_unref (entry->value);
				g_free (entry);
				
				entry = tmp_entry;
			}
			break;
		case CSS_FUNCTION:
			css_value_unref (val->v.function->args);
			g_free (val->v.function);
			break;
		default:
			g_warning ("css_value_unref: Unhandled case: %d\n", val->value_type);
		}

		g_free (val);
	}
}

CssValue*
css_value_ref (CssValue *val)
{
	g_return_val_if_fail (val != NULL, NULL);
	g_return_val_if_fail (val->ref_count > 0, NULL);

	val->ref_count +=1;
	return val;
}