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 = NULL;
137 result = g_try_malloc (sizeof (CRToken));
139 if (result == NULL) {
140 cr_utils_trace_info ("Out of memory");
141 return NULL;
142 }
144 memset (result, 0, sizeof (CRToken));
146 return result;
147 }
149 /**
150 *Sets the type of curren instance of
151 *#CRToken to 'S_TK' (S in the css2 spec)
152 *@param a_this the current instance of #CRToken.
153 *@return CR_OK upon successfull completion, an error
154 *code otherwise.
155 */
156 enum CRStatus
157 cr_token_set_s (CRToken * a_this)
158 {
159 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
161 cr_token_clear (a_this);
163 a_this->type = S_TK;
165 return CR_OK;
166 }
168 /**
169 *Sets the type of the current instance of
170 *#CRToken to 'CDO_TK' (CDO as said by the css2 spec)
171 *@param a_this the current instance of #CRToken.
172 *@return CR_OK upon successfull completion, an error
173 *code otherwise.
174 */
175 enum CRStatus
176 cr_token_set_cdo (CRToken * a_this)
177 {
178 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
180 cr_token_clear (a_this);
182 a_this->type = CDO_TK;
184 return CR_OK;
185 }
187 /**
188 *Sets the type of the current token to
189 *CDC_TK (CDC as said by the css2 spec).
190 *@param a_this the current instance of #CRToken.
191 *@return CR_OK upon successfull completion, an error
192 *code otherwise.
193 */
194 enum CRStatus
195 cr_token_set_cdc (CRToken * a_this)
196 {
197 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
199 cr_token_clear (a_this);
201 a_this->type = CDC_TK;
203 return CR_OK;
204 }
206 /**
207 *Sets the type of the current instance of
208 *#CRToken to INCLUDES_TK (INCLUDES as said by the css2 spec).
209 *@param a_this the current instance of #CRToken.
210 *@return CR_OK upon successfull completion, an error
211 *code otherwise.
212 */
213 enum CRStatus
214 cr_token_set_includes (CRToken * a_this)
215 {
216 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
218 cr_token_clear (a_this);
220 a_this->type = INCLUDES_TK;
222 return CR_OK;
223 }
225 /**
226 *Sets the type of the current instance of
227 *#CRToken to DASHMATCH_TK (DASHMATCH as said by the css2 spec).
228 *@param a_this the current instance of #CRToken.
229 *@return CR_OK upon successfull completion, an error
230 *code otherwise.
231 */
232 enum CRStatus
233 cr_token_set_dashmatch (CRToken * a_this)
234 {
235 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
237 cr_token_clear (a_this);
239 a_this->type = DASHMATCH_TK;
241 return CR_OK;
242 }
244 enum CRStatus
245 cr_token_set_comment (CRToken * a_this, CRString * a_str)
246 {
247 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
249 cr_token_clear (a_this);
250 a_this->type = COMMENT_TK;
251 a_this->u.str = a_str ;
252 return CR_OK;
253 }
255 enum CRStatus
256 cr_token_set_string (CRToken * a_this, CRString * a_str)
257 {
258 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
260 cr_token_clear (a_this);
262 a_this->type = STRING_TK;
264 a_this->u.str = a_str ;
266 return CR_OK;
267 }
269 enum CRStatus
270 cr_token_set_ident (CRToken * a_this, CRString * a_ident)
271 {
272 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
274 cr_token_clear (a_this);
275 a_this->type = IDENT_TK;
276 a_this->u.str = a_ident;
277 return CR_OK;
278 }
281 enum CRStatus
282 cr_token_set_function (CRToken * a_this, CRString * a_fun_name)
283 {
284 g_return_val_if_fail (a_this,
285 CR_BAD_PARAM_ERROR);
287 cr_token_clear (a_this);
288 a_this->type = FUNCTION_TK;
289 a_this->u.str = a_fun_name;
290 return CR_OK;
291 }
293 enum CRStatus
294 cr_token_set_hash (CRToken * a_this, CRString * a_hash)
295 {
296 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
298 cr_token_clear (a_this);
299 a_this->type = HASH_TK;
300 a_this->u.str = a_hash;
302 return CR_OK;
303 }
305 enum CRStatus
306 cr_token_set_rgb (CRToken * a_this, CRRgb * a_rgb)
307 {
308 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
310 cr_token_clear (a_this);
311 a_this->type = RGB_TK;
312 a_this->u.rgb = a_rgb;
314 return CR_OK;
315 }
317 enum CRStatus
318 cr_token_set_import_sym (CRToken * a_this)
319 {
320 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
322 cr_token_clear (a_this);
324 a_this->type = IMPORT_SYM_TK;
326 return CR_OK;
327 }
329 enum CRStatus
330 cr_token_set_page_sym (CRToken * a_this)
331 {
332 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
334 cr_token_clear (a_this);
336 a_this->type = PAGE_SYM_TK;
338 return CR_OK;
339 }
341 enum CRStatus
342 cr_token_set_media_sym (CRToken * a_this)
343 {
344 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
346 cr_token_clear (a_this);
348 a_this->type = MEDIA_SYM_TK;
350 return CR_OK;
351 }
353 enum CRStatus
354 cr_token_set_font_face_sym (CRToken * a_this)
355 {
356 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
358 cr_token_clear (a_this);
359 a_this->type = FONT_FACE_SYM_TK;
361 return CR_OK;
362 }
364 enum CRStatus
365 cr_token_set_charset_sym (CRToken * a_this)
366 {
367 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
369 cr_token_clear (a_this);
370 a_this->type = CHARSET_SYM_TK;
372 return CR_OK;
373 }
375 enum CRStatus
376 cr_token_set_atkeyword (CRToken * a_this, CRString * a_atname)
377 {
378 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
380 cr_token_clear (a_this);
381 a_this->type = ATKEYWORD_TK;
382 a_this->u.str = a_atname;
383 return CR_OK;
384 }
386 enum CRStatus
387 cr_token_set_important_sym (CRToken * a_this)
388 {
389 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
390 cr_token_clear (a_this);
391 a_this->type = IMPORTANT_SYM_TK;
392 return CR_OK;
393 }
395 enum CRStatus
396 cr_token_set_ems (CRToken * a_this, CRNum * a_num)
397 {
398 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
399 cr_token_clear (a_this);
400 a_this->type = EMS_TK;
401 a_this->u.num = a_num;
402 return CR_OK;
403 }
405 enum CRStatus
406 cr_token_set_exs (CRToken * a_this, CRNum * a_num)
407 {
408 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
409 cr_token_clear (a_this);
410 a_this->type = EXS_TK;
411 a_this->u.num = a_num;
412 return CR_OK;
413 }
415 enum CRStatus
416 cr_token_set_length (CRToken * a_this, CRNum * a_num,
417 enum CRTokenExtraType a_et)
418 {
419 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
421 cr_token_clear (a_this);
423 a_this->type = LENGTH_TK;
424 a_this->extra_type = a_et;
425 a_this->u.num = a_num;
427 return CR_OK;
428 }
430 enum CRStatus
431 cr_token_set_angle (CRToken * a_this, CRNum * a_num,
432 enum CRTokenExtraType a_et)
433 {
434 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
436 cr_token_clear (a_this);
438 a_this->type = ANGLE_TK;
439 a_this->extra_type = a_et;
440 a_this->u.num = a_num;
442 return CR_OK;
443 }
445 enum CRStatus
446 cr_token_set_time (CRToken * a_this, CRNum * a_num,
447 enum CRTokenExtraType a_et)
448 {
449 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
451 cr_token_clear (a_this);
453 a_this->type = TIME_TK;
454 a_this->extra_type = a_et;
455 a_this->u.num = a_num;
457 return CR_OK;
458 }
460 enum CRStatus
461 cr_token_set_freq (CRToken * a_this, CRNum * a_num,
462 enum CRTokenExtraType a_et)
463 {
464 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
466 cr_token_clear (a_this);
468 a_this->type = FREQ_TK;
469 a_this->extra_type = a_et;
470 a_this->u.num = a_num;
472 return CR_OK;
473 }
475 enum CRStatus
476 cr_token_set_dimen (CRToken * a_this, CRNum * a_num,
477 CRString * a_dim)
478 {
479 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
480 cr_token_clear (a_this);
481 a_this->type = DIMEN_TK;
482 a_this->u.num = a_num;
483 a_this->dimen = a_dim;
484 return CR_OK;
486 }
488 enum CRStatus
489 cr_token_set_percentage (CRToken * a_this, CRNum * a_num)
490 {
491 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
493 cr_token_clear (a_this);
495 a_this->type = PERCENTAGE_TK;
496 a_this->u.num = a_num;
498 return CR_OK;
499 }
501 enum CRStatus
502 cr_token_set_number (CRToken * a_this, CRNum * a_num)
503 {
504 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
506 cr_token_clear (a_this);
508 a_this->type = NUMBER_TK;
509 a_this->u.num = a_num;
510 return CR_OK;
511 }
513 enum CRStatus
514 cr_token_set_uri (CRToken * a_this, CRString * a_uri)
515 {
516 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
518 cr_token_clear (a_this);
520 a_this->type = URI_TK;
521 a_this->u.str = a_uri;
523 return CR_OK;
524 }
526 enum CRStatus
527 cr_token_set_delim (CRToken * a_this, guint32 a_char)
528 {
529 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
531 cr_token_clear (a_this);
533 a_this->type = DELIM_TK;
534 a_this->u.unichar = a_char;
536 return CR_OK;
537 }
539 enum CRStatus
540 cr_token_set_semicolon (CRToken * a_this)
541 {
542 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
544 cr_token_clear (a_this);
546 a_this->type = SEMICOLON_TK;
548 return CR_OK;
549 }
551 enum CRStatus
552 cr_token_set_cbo (CRToken * a_this)
553 {
554 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
556 cr_token_clear (a_this);
558 a_this->type = CBO_TK;
560 return CR_OK;
561 }
563 enum CRStatus
564 cr_token_set_cbc (CRToken * a_this)
565 {
566 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
568 cr_token_clear (a_this);
570 a_this->type = CBC_TK;
572 return CR_OK;
573 }
575 enum CRStatus
576 cr_token_set_po (CRToken * a_this)
577 {
578 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
580 cr_token_clear (a_this);
582 a_this->type = PO_TK;
584 return CR_OK;
585 }
587 enum CRStatus
588 cr_token_set_pc (CRToken * a_this)
589 {
590 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
592 cr_token_clear (a_this);
594 a_this->type = PC_TK;
596 return CR_OK;
597 }
599 enum CRStatus
600 cr_token_set_bo (CRToken * a_this)
601 {
602 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
604 cr_token_clear (a_this);
606 a_this->type = BO_TK;
608 return CR_OK;
609 }
611 enum CRStatus
612 cr_token_set_bc (CRToken * a_this)
613 {
614 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
616 cr_token_clear (a_this);
618 a_this->type = BC_TK;
620 return CR_OK;
621 }
623 /**
624 *The destructor of the #CRToken class.
625 *@param a_this the current instance of #CRToken.
626 */
627 void
628 cr_token_destroy (CRToken * a_this)
629 {
630 g_return_if_fail (a_this);
632 cr_token_clear (a_this);
634 g_free (a_this);
635 }