1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
3 /*
4 * This file is part of The Croco Library
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 *
20 * Author: Dodji Seketeli
21 * see COPYRIGHTS file for copyright information.
22 */
24 /**
25 *@file
26 *The definition of the #CRToken class.
27 *Abstracts a css2 token.
28 */
29 #include <string.h>
30 #include "cr-token.h"
32 /*
33 *TODO: write a CRToken::to_string() method.
34 */
36 /**
37 *Frees the attributes of the current instance
38 *of #CRtoken.
39 *@param a_this the current instance of #CRToken.
40 */
41 static void
42 cr_token_clear (CRToken * a_this)
43 {
44 g_return_if_fail (a_this);
46 switch (a_this->type) {
47 case S_TK:
48 case CDO_TK:
49 case INCLUDES_TK:
50 case DASHMATCH_TK:
51 case PAGE_SYM_TK:
52 case MEDIA_SYM_TK:
53 case FONT_FACE_SYM_TK:
54 case CHARSET_SYM_TK:
55 case IMPORT_SYM_TK:
56 case IMPORTANT_SYM_TK:
57 case SEMICOLON_TK:
58 case NO_TK:
59 case DELIM_TK:
60 case CBO_TK:
61 case CBC_TK:
62 case BO_TK:
63 case BC_TK:
64 break;
66 case STRING_TK:
67 case IDENT_TK:
68 case HASH_TK:
69 case URI_TK:
70 case FUNCTION_TK:
71 case COMMENT_TK:
72 case ATKEYWORD_TK:
73 if (a_this->u.str) {
74 cr_string_destroy (a_this->u.str);
75 a_this->u.str = NULL;
76 }
77 break;
79 case EMS_TK:
80 case EXS_TK:
81 case LENGTH_TK:
82 case ANGLE_TK:
83 case TIME_TK:
84 case FREQ_TK:
85 case PERCENTAGE_TK:
86 case NUMBER_TK:
87 case PO_TK:
88 case PC_TK:
89 if (a_this->u.num) {
90 cr_num_destroy (a_this->u.num);
91 a_this->u.num = NULL;
92 }
93 break;
95 case DIMEN_TK:
96 if (a_this->u.num) {
97 cr_num_destroy (a_this->u.num);
98 a_this->u.num = NULL;
99 }
101 if (a_this->dimen) {
102 cr_string_destroy (a_this->dimen);
103 a_this->dimen = NULL;
104 }
106 break;
108 case RGB_TK:
109 if (a_this->u.rgb) {
110 cr_rgb_destroy (a_this->u.rgb) ;
111 a_this->u.rgb = NULL ;
112 }
113 break ;
115 case UNICODERANGE_TK:
116 /*not supported yet. */
117 break;
119 default:
120 cr_utils_trace_info ("I don't know how to clear this token\n") ;
121 break;
122 }
124 a_this->type = NO_TK;
125 }
127 /**
128 *Default constructor of
129 *the #CRToken class.
130 *@return the newly built instance of #CRToken.
131 */
132 CRToken *
133 cr_token_new (void)
134 {
135 CRToken *result = (CRToken *)g_try_malloc (sizeof (CRToken));
137 if (result == NULL) {
138 cr_utils_trace_info ("Out of memory");
139 return NULL;
140 }
142 memset (result, 0, sizeof (CRToken));
144 return result;
145 }
147 /**
148 *Sets the type of curren instance of
149 *#CRToken to 'S_TK' (S in the css2 spec)
150 *@param a_this the current instance of #CRToken.
151 *@return CR_OK upon successfull completion, an error
152 *code otherwise.
153 */
154 enum CRStatus
155 cr_token_set_s (CRToken * a_this)
156 {
157 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
159 cr_token_clear (a_this);
161 a_this->type = S_TK;
163 return CR_OK;
164 }
166 /**
167 *Sets the type of the current instance of
168 *#CRToken to 'CDO_TK' (CDO as said by the css2 spec)
169 *@param a_this the current instance of #CRToken.
170 *@return CR_OK upon successfull completion, an error
171 *code otherwise.
172 */
173 enum CRStatus
174 cr_token_set_cdo (CRToken * a_this)
175 {
176 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
178 cr_token_clear (a_this);
180 a_this->type = CDO_TK;
182 return CR_OK;
183 }
185 /**
186 *Sets the type of the current token to
187 *CDC_TK (CDC as said by the css2 spec).
188 *@param a_this the current instance of #CRToken.
189 *@return CR_OK upon successfull completion, an error
190 *code otherwise.
191 */
192 enum CRStatus
193 cr_token_set_cdc (CRToken * a_this)
194 {
195 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
197 cr_token_clear (a_this);
199 a_this->type = CDC_TK;
201 return CR_OK;
202 }
204 /**
205 *Sets the type of the current instance of
206 *#CRToken to INCLUDES_TK (INCLUDES as said by the css2 spec).
207 *@param a_this the current instance of #CRToken.
208 *@return CR_OK upon successfull completion, an error
209 *code otherwise.
210 */
211 enum CRStatus
212 cr_token_set_includes (CRToken * a_this)
213 {
214 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
216 cr_token_clear (a_this);
218 a_this->type = INCLUDES_TK;
220 return CR_OK;
221 }
223 /**
224 *Sets the type of the current instance of
225 *#CRToken to DASHMATCH_TK (DASHMATCH as said by the css2 spec).
226 *@param a_this the current instance of #CRToken.
227 *@return CR_OK upon successfull completion, an error
228 *code otherwise.
229 */
230 enum CRStatus
231 cr_token_set_dashmatch (CRToken * a_this)
232 {
233 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
235 cr_token_clear (a_this);
237 a_this->type = DASHMATCH_TK;
239 return CR_OK;
240 }
242 enum CRStatus
243 cr_token_set_comment (CRToken * a_this, CRString * a_str)
244 {
245 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
247 cr_token_clear (a_this);
248 a_this->type = COMMENT_TK;
249 a_this->u.str = a_str ;
250 return CR_OK;
251 }
253 enum CRStatus
254 cr_token_set_string (CRToken * a_this, CRString * a_str)
255 {
256 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
258 cr_token_clear (a_this);
260 a_this->type = STRING_TK;
262 a_this->u.str = a_str ;
264 return CR_OK;
265 }
267 enum CRStatus
268 cr_token_set_ident (CRToken * a_this, CRString * a_ident)
269 {
270 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
272 cr_token_clear (a_this);
273 a_this->type = IDENT_TK;
274 a_this->u.str = a_ident;
275 return CR_OK;
276 }
279 enum CRStatus
280 cr_token_set_function (CRToken * a_this, CRString * a_fun_name)
281 {
282 g_return_val_if_fail (a_this,
283 CR_BAD_PARAM_ERROR);
285 cr_token_clear (a_this);
286 a_this->type = FUNCTION_TK;
287 a_this->u.str = a_fun_name;
288 return CR_OK;
289 }
291 enum CRStatus
292 cr_token_set_hash (CRToken * a_this, CRString * a_hash)
293 {
294 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
296 cr_token_clear (a_this);
297 a_this->type = HASH_TK;
298 a_this->u.str = a_hash;
300 return CR_OK;
301 }
303 enum CRStatus
304 cr_token_set_rgb (CRToken * a_this, CRRgb * a_rgb)
305 {
306 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
308 cr_token_clear (a_this);
309 a_this->type = RGB_TK;
310 a_this->u.rgb = a_rgb;
312 return CR_OK;
313 }
315 enum CRStatus
316 cr_token_set_import_sym (CRToken * a_this)
317 {
318 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
320 cr_token_clear (a_this);
322 a_this->type = IMPORT_SYM_TK;
324 return CR_OK;
325 }
327 enum CRStatus
328 cr_token_set_page_sym (CRToken * a_this)
329 {
330 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
332 cr_token_clear (a_this);
334 a_this->type = PAGE_SYM_TK;
336 return CR_OK;
337 }
339 enum CRStatus
340 cr_token_set_media_sym (CRToken * a_this)
341 {
342 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
344 cr_token_clear (a_this);
346 a_this->type = MEDIA_SYM_TK;
348 return CR_OK;
349 }
351 enum CRStatus
352 cr_token_set_font_face_sym (CRToken * a_this)
353 {
354 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
356 cr_token_clear (a_this);
357 a_this->type = FONT_FACE_SYM_TK;
359 return CR_OK;
360 }
362 enum CRStatus
363 cr_token_set_charset_sym (CRToken * a_this)
364 {
365 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
367 cr_token_clear (a_this);
368 a_this->type = CHARSET_SYM_TK;
370 return CR_OK;
371 }
373 enum CRStatus
374 cr_token_set_atkeyword (CRToken * a_this, CRString * a_atname)
375 {
376 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
378 cr_token_clear (a_this);
379 a_this->type = ATKEYWORD_TK;
380 a_this->u.str = a_atname;
381 return CR_OK;
382 }
384 enum CRStatus
385 cr_token_set_important_sym (CRToken * a_this)
386 {
387 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
388 cr_token_clear (a_this);
389 a_this->type = IMPORTANT_SYM_TK;
390 return CR_OK;
391 }
393 enum CRStatus
394 cr_token_set_ems (CRToken * a_this, CRNum * a_num)
395 {
396 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
397 cr_token_clear (a_this);
398 a_this->type = EMS_TK;
399 a_this->u.num = a_num;
400 return CR_OK;
401 }
403 enum CRStatus
404 cr_token_set_exs (CRToken * a_this, CRNum * a_num)
405 {
406 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
407 cr_token_clear (a_this);
408 a_this->type = EXS_TK;
409 a_this->u.num = a_num;
410 return CR_OK;
411 }
413 enum CRStatus
414 cr_token_set_length (CRToken * a_this, CRNum * a_num,
415 enum CRTokenExtraType a_et)
416 {
417 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
419 cr_token_clear (a_this);
421 a_this->type = LENGTH_TK;
422 a_this->extra_type = a_et;
423 a_this->u.num = a_num;
425 return CR_OK;
426 }
428 enum CRStatus
429 cr_token_set_angle (CRToken * a_this, CRNum * a_num,
430 enum CRTokenExtraType a_et)
431 {
432 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
434 cr_token_clear (a_this);
436 a_this->type = ANGLE_TK;
437 a_this->extra_type = a_et;
438 a_this->u.num = a_num;
440 return CR_OK;
441 }
443 enum CRStatus
444 cr_token_set_time (CRToken * a_this, CRNum * a_num,
445 enum CRTokenExtraType a_et)
446 {
447 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
449 cr_token_clear (a_this);
451 a_this->type = TIME_TK;
452 a_this->extra_type = a_et;
453 a_this->u.num = a_num;
455 return CR_OK;
456 }
458 enum CRStatus
459 cr_token_set_freq (CRToken * a_this, CRNum * a_num,
460 enum CRTokenExtraType a_et)
461 {
462 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
464 cr_token_clear (a_this);
466 a_this->type = FREQ_TK;
467 a_this->extra_type = a_et;
468 a_this->u.num = a_num;
470 return CR_OK;
471 }
473 enum CRStatus
474 cr_token_set_dimen (CRToken * a_this, CRNum * a_num,
475 CRString * a_dim)
476 {
477 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
478 cr_token_clear (a_this);
479 a_this->type = DIMEN_TK;
480 a_this->u.num = a_num;
481 a_this->dimen = a_dim;
482 return CR_OK;
484 }
486 enum CRStatus
487 cr_token_set_percentage (CRToken * a_this, CRNum * a_num)
488 {
489 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
491 cr_token_clear (a_this);
493 a_this->type = PERCENTAGE_TK;
494 a_this->u.num = a_num;
496 return CR_OK;
497 }
499 enum CRStatus
500 cr_token_set_number (CRToken * a_this, CRNum * a_num)
501 {
502 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
504 cr_token_clear (a_this);
506 a_this->type = NUMBER_TK;
507 a_this->u.num = a_num;
508 return CR_OK;
509 }
511 enum CRStatus
512 cr_token_set_uri (CRToken * a_this, CRString * a_uri)
513 {
514 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
516 cr_token_clear (a_this);
518 a_this->type = URI_TK;
519 a_this->u.str = a_uri;
521 return CR_OK;
522 }
524 enum CRStatus
525 cr_token_set_delim (CRToken * a_this, guint32 a_char)
526 {
527 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
529 cr_token_clear (a_this);
531 a_this->type = DELIM_TK;
532 a_this->u.unichar = a_char;
534 return CR_OK;
535 }
537 enum CRStatus
538 cr_token_set_semicolon (CRToken * a_this)
539 {
540 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
542 cr_token_clear (a_this);
544 a_this->type = SEMICOLON_TK;
546 return CR_OK;
547 }
549 enum CRStatus
550 cr_token_set_cbo (CRToken * a_this)
551 {
552 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
554 cr_token_clear (a_this);
556 a_this->type = CBO_TK;
558 return CR_OK;
559 }
561 enum CRStatus
562 cr_token_set_cbc (CRToken * a_this)
563 {
564 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
566 cr_token_clear (a_this);
568 a_this->type = CBC_TK;
570 return CR_OK;
571 }
573 enum CRStatus
574 cr_token_set_po (CRToken * a_this)
575 {
576 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
578 cr_token_clear (a_this);
580 a_this->type = PO_TK;
582 return CR_OK;
583 }
585 enum CRStatus
586 cr_token_set_pc (CRToken * a_this)
587 {
588 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
590 cr_token_clear (a_this);
592 a_this->type = PC_TK;
594 return CR_OK;
595 }
597 enum CRStatus
598 cr_token_set_bo (CRToken * a_this)
599 {
600 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
602 cr_token_clear (a_this);
604 a_this->type = BO_TK;
606 return CR_OK;
607 }
609 enum CRStatus
610 cr_token_set_bc (CRToken * a_this)
611 {
612 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
614 cr_token_clear (a_this);
616 a_this->type = BC_TK;
618 return CR_OK;
619 }
621 /**
622 *The destructor of the #CRToken class.
623 *@param a_this the current instance of #CRToken.
624 */
625 void
626 cr_token_destroy (CRToken * a_this)
627 {
628 g_return_if_fail (a_this);
630 cr_token_clear (a_this);
632 g_free (a_this);
633 }