1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2
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 */
23
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"
31
32 /*
33 *TODO: write a CRToken::to_string() method.
34 */
35
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
cr_token_clear(CRToken * a_this)42 cr_token_clear (CRToken * a_this)
43 {
44 g_return_if_fail (a_this);
45
46 switch (a_this->type) {
47 case S_TK:
48 case CDO_TK:
49 case CDC_TK:
50 case INCLUDES_TK:
51 case DASHMATCH_TK:
52 case PAGE_SYM_TK:
53 case MEDIA_SYM_TK:
54 case FONT_FACE_SYM_TK:
55 case CHARSET_SYM_TK:
56 case IMPORT_SYM_TK:
57 case IMPORTANT_SYM_TK:
58 case SEMICOLON_TK:
59 case NO_TK:
60 case DELIM_TK:
61 case CBO_TK:
62 case CBC_TK:
63 case BO_TK:
64 case BC_TK:
65 break;
66
67 case STRING_TK:
68 case IDENT_TK:
69 case HASH_TK:
70 case URI_TK:
71 case FUNCTION_TK:
72 case COMMENT_TK:
73 case ATKEYWORD_TK:
74 if (a_this->u.str) {
75 cr_string_destroy (a_this->u.str);
76 a_this->u.str = NULL;
77 }
78 break;
79
80 case EMS_TK:
81 case EXS_TK:
82 case LENGTH_TK:
83 case ANGLE_TK:
84 case TIME_TK:
85 case FREQ_TK:
86 case PERCENTAGE_TK:
87 case NUMBER_TK:
88 case PO_TK:
89 case PC_TK:
90 if (a_this->u.num) {
91 cr_num_destroy (a_this->u.num);
92 a_this->u.num = NULL;
93 }
94 break;
95
96 case DIMEN_TK:
97 if (a_this->u.num) {
98 cr_num_destroy (a_this->u.num);
99 a_this->u.num = NULL;
100 }
101
102 if (a_this->dimen) {
103 cr_string_destroy (a_this->dimen);
104 a_this->dimen = NULL;
105 }
106
107 break;
108
109 case RGB_TK:
110 if (a_this->u.rgb) {
111 cr_rgb_destroy (a_this->u.rgb) ;
112 a_this->u.rgb = NULL ;
113 }
114 break ;
115
116 case UNICODERANGE_TK:
117 /*not supported yet. */
118 break;
119
120 default:
121 cr_utils_trace_info ("I don't know how to clear this token\n") ;
122 break;
123 }
124
125 a_this->type = NO_TK;
126 }
127
128 /**
129 *Default constructor of
130 *the #CRToken class.
131 *@return the newly built instance of #CRToken.
132 */
133 CRToken *
cr_token_new(void)134 cr_token_new (void)
135 {
136 CRToken *result = NULL;
137
138 result = (CRToken *) g_try_malloc (sizeof (CRToken));
139
140 if (result == NULL) {
141 cr_utils_trace_info ("Out of memory");
142 return NULL;
143 }
144
145 memset (result, 0, sizeof (CRToken));
146
147 return result;
148 }
149
150 /**
151 *Sets the type of curren instance of
152 *#CRToken to 'S_TK' (S in the css2 spec)
153 *@param a_this the current instance of #CRToken.
154 *@return CR_OK upon successful completion, an error
155 *code otherwise.
156 */
157 enum CRStatus
cr_token_set_s(CRToken * a_this)158 cr_token_set_s (CRToken * a_this)
159 {
160 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
161
162 cr_token_clear (a_this);
163
164 a_this->type = S_TK;
165
166 return CR_OK;
167 }
168
169 /**
170 *Sets the type of the current instance of
171 *#CRToken to 'CDO_TK' (CDO as said by the css2 spec)
172 *@param a_this the current instance of #CRToken.
173 *@return CR_OK upon successful completion, an error
174 *code otherwise.
175 */
176 enum CRStatus
cr_token_set_cdo(CRToken * a_this)177 cr_token_set_cdo (CRToken * a_this)
178 {
179 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
180
181 cr_token_clear (a_this);
182
183 a_this->type = CDO_TK;
184
185 return CR_OK;
186 }
187
188 /**
189 *Sets the type of the current token to
190 *CDC_TK (CDC as said by the css2 spec).
191 *@param a_this the current instance of #CRToken.
192 *@return CR_OK upon successful completion, an error
193 *code otherwise.
194 */
195 enum CRStatus
cr_token_set_cdc(CRToken * a_this)196 cr_token_set_cdc (CRToken * a_this)
197 {
198 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
199
200 cr_token_clear (a_this);
201
202 a_this->type = CDC_TK;
203
204 return CR_OK;
205 }
206
207 /**
208 *Sets the type of the current instance of
209 *#CRToken to INCLUDES_TK (INCLUDES as said by the css2 spec).
210 *@param a_this the current instance of #CRToken.
211 *@return CR_OK upon successful completion, an error
212 *code otherwise.
213 */
214 enum CRStatus
cr_token_set_includes(CRToken * a_this)215 cr_token_set_includes (CRToken * a_this)
216 {
217 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
218
219 cr_token_clear (a_this);
220
221 a_this->type = INCLUDES_TK;
222
223 return CR_OK;
224 }
225
226 /**
227 *Sets the type of the current instance of
228 *#CRToken to DASHMATCH_TK (DASHMATCH as said by the css2 spec).
229 *@param a_this the current instance of #CRToken.
230 *@return CR_OK upon successful completion, an error
231 *code otherwise.
232 */
233 enum CRStatus
cr_token_set_dashmatch(CRToken * a_this)234 cr_token_set_dashmatch (CRToken * a_this)
235 {
236 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
237
238 cr_token_clear (a_this);
239
240 a_this->type = DASHMATCH_TK;
241
242 return CR_OK;
243 }
244
245 enum CRStatus
cr_token_set_comment(CRToken * a_this,CRString * a_str)246 cr_token_set_comment (CRToken * a_this, CRString * a_str)
247 {
248 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
249
250 cr_token_clear (a_this);
251 a_this->type = COMMENT_TK;
252 a_this->u.str = a_str ;
253 return CR_OK;
254 }
255
256 enum CRStatus
cr_token_set_string(CRToken * a_this,CRString * a_str)257 cr_token_set_string (CRToken * a_this, CRString * a_str)
258 {
259 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
260
261 cr_token_clear (a_this);
262
263 a_this->type = STRING_TK;
264
265 a_this->u.str = a_str ;
266
267 return CR_OK;
268 }
269
270 enum CRStatus
cr_token_set_ident(CRToken * a_this,CRString * a_ident)271 cr_token_set_ident (CRToken * a_this, CRString * a_ident)
272 {
273 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
274
275 cr_token_clear (a_this);
276 a_this->type = IDENT_TK;
277 a_this->u.str = a_ident;
278 return CR_OK;
279 }
280
281
282 enum CRStatus
cr_token_set_function(CRToken * a_this,CRString * a_fun_name)283 cr_token_set_function (CRToken * a_this, CRString * a_fun_name)
284 {
285 g_return_val_if_fail (a_this,
286 CR_BAD_PARAM_ERROR);
287
288 cr_token_clear (a_this);
289 a_this->type = FUNCTION_TK;
290 a_this->u.str = a_fun_name;
291 return CR_OK;
292 }
293
294 enum CRStatus
cr_token_set_hash(CRToken * a_this,CRString * a_hash)295 cr_token_set_hash (CRToken * a_this, CRString * a_hash)
296 {
297 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
298
299 cr_token_clear (a_this);
300 a_this->type = HASH_TK;
301 a_this->u.str = a_hash;
302
303 return CR_OK;
304 }
305
306 enum CRStatus
cr_token_set_rgb(CRToken * a_this,CRRgb * a_rgb)307 cr_token_set_rgb (CRToken * a_this, CRRgb * a_rgb)
308 {
309 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
310
311 cr_token_clear (a_this);
312 a_this->type = RGB_TK;
313 a_this->u.rgb = a_rgb;
314
315 return CR_OK;
316 }
317
318 enum CRStatus
cr_token_set_import_sym(CRToken * a_this)319 cr_token_set_import_sym (CRToken * a_this)
320 {
321 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
322
323 cr_token_clear (a_this);
324
325 a_this->type = IMPORT_SYM_TK;
326
327 return CR_OK;
328 }
329
330 enum CRStatus
cr_token_set_page_sym(CRToken * a_this)331 cr_token_set_page_sym (CRToken * a_this)
332 {
333 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
334
335 cr_token_clear (a_this);
336
337 a_this->type = PAGE_SYM_TK;
338
339 return CR_OK;
340 }
341
342 enum CRStatus
cr_token_set_media_sym(CRToken * a_this)343 cr_token_set_media_sym (CRToken * a_this)
344 {
345 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
346
347 cr_token_clear (a_this);
348
349 a_this->type = MEDIA_SYM_TK;
350
351 return CR_OK;
352 }
353
354 enum CRStatus
cr_token_set_font_face_sym(CRToken * a_this)355 cr_token_set_font_face_sym (CRToken * a_this)
356 {
357 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
358
359 cr_token_clear (a_this);
360 a_this->type = FONT_FACE_SYM_TK;
361
362 return CR_OK;
363 }
364
365 enum CRStatus
cr_token_set_charset_sym(CRToken * a_this)366 cr_token_set_charset_sym (CRToken * a_this)
367 {
368 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
369
370 cr_token_clear (a_this);
371 a_this->type = CHARSET_SYM_TK;
372
373 return CR_OK;
374 }
375
376 enum CRStatus
cr_token_set_atkeyword(CRToken * a_this,CRString * a_atname)377 cr_token_set_atkeyword (CRToken * a_this, CRString * a_atname)
378 {
379 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
380
381 cr_token_clear (a_this);
382 a_this->type = ATKEYWORD_TK;
383 a_this->u.str = a_atname;
384 return CR_OK;
385 }
386
387 enum CRStatus
cr_token_set_important_sym(CRToken * a_this)388 cr_token_set_important_sym (CRToken * a_this)
389 {
390 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
391 cr_token_clear (a_this);
392 a_this->type = IMPORTANT_SYM_TK;
393 return CR_OK;
394 }
395
396 enum CRStatus
cr_token_set_ems(CRToken * a_this,CRNum * a_num)397 cr_token_set_ems (CRToken * a_this, CRNum * a_num)
398 {
399 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
400 cr_token_clear (a_this);
401 a_this->type = EMS_TK;
402 a_this->u.num = a_num;
403 return CR_OK;
404 }
405
406 enum CRStatus
cr_token_set_exs(CRToken * a_this,CRNum * a_num)407 cr_token_set_exs (CRToken * a_this, CRNum * a_num)
408 {
409 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
410 cr_token_clear (a_this);
411 a_this->type = EXS_TK;
412 a_this->u.num = a_num;
413 return CR_OK;
414 }
415
416 enum CRStatus
cr_token_set_length(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)417 cr_token_set_length (CRToken * a_this, CRNum * a_num,
418 enum CRTokenExtraType a_et)
419 {
420 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
421
422 cr_token_clear (a_this);
423
424 a_this->type = LENGTH_TK;
425 a_this->extra_type = a_et;
426 a_this->u.num = a_num;
427
428 return CR_OK;
429 }
430
431 enum CRStatus
cr_token_set_angle(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)432 cr_token_set_angle (CRToken * a_this, CRNum * a_num,
433 enum CRTokenExtraType a_et)
434 {
435 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
436
437 cr_token_clear (a_this);
438
439 a_this->type = ANGLE_TK;
440 a_this->extra_type = a_et;
441 a_this->u.num = a_num;
442
443 return CR_OK;
444 }
445
446 enum CRStatus
cr_token_set_time(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)447 cr_token_set_time (CRToken * a_this, CRNum * a_num,
448 enum CRTokenExtraType a_et)
449 {
450 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
451
452 cr_token_clear (a_this);
453
454 a_this->type = TIME_TK;
455 a_this->extra_type = a_et;
456 a_this->u.num = a_num;
457
458 return CR_OK;
459 }
460
461 enum CRStatus
cr_token_set_freq(CRToken * a_this,CRNum * a_num,enum CRTokenExtraType a_et)462 cr_token_set_freq (CRToken * a_this, CRNum * a_num,
463 enum CRTokenExtraType a_et)
464 {
465 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
466
467 cr_token_clear (a_this);
468
469 a_this->type = FREQ_TK;
470 a_this->extra_type = a_et;
471 a_this->u.num = a_num;
472
473 return CR_OK;
474 }
475
476 enum CRStatus
cr_token_set_dimen(CRToken * a_this,CRNum * a_num,CRString * a_dim)477 cr_token_set_dimen (CRToken * a_this, CRNum * a_num,
478 CRString * a_dim)
479 {
480 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
481 cr_token_clear (a_this);
482 a_this->type = DIMEN_TK;
483 a_this->u.num = a_num;
484 a_this->dimen = a_dim;
485 return CR_OK;
486
487 }
488
489 enum CRStatus
cr_token_set_percentage(CRToken * a_this,CRNum * a_num)490 cr_token_set_percentage (CRToken * a_this, CRNum * a_num)
491 {
492 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
493
494 cr_token_clear (a_this);
495
496 a_this->type = PERCENTAGE_TK;
497 a_this->u.num = a_num;
498
499 return CR_OK;
500 }
501
502 enum CRStatus
cr_token_set_number(CRToken * a_this,CRNum * a_num)503 cr_token_set_number (CRToken * a_this, CRNum * a_num)
504 {
505 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
506
507 cr_token_clear (a_this);
508
509 a_this->type = NUMBER_TK;
510 a_this->u.num = a_num;
511 return CR_OK;
512 }
513
514 enum CRStatus
cr_token_set_uri(CRToken * a_this,CRString * a_uri)515 cr_token_set_uri (CRToken * a_this, CRString * a_uri)
516 {
517 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
518
519 cr_token_clear (a_this);
520
521 a_this->type = URI_TK;
522 a_this->u.str = a_uri;
523
524 return CR_OK;
525 }
526
527 enum CRStatus
cr_token_set_delim(CRToken * a_this,guint32 a_char)528 cr_token_set_delim (CRToken * a_this, guint32 a_char)
529 {
530 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
531
532 cr_token_clear (a_this);
533
534 a_this->type = DELIM_TK;
535 a_this->u.unichar = a_char;
536
537 return CR_OK;
538 }
539
540 enum CRStatus
cr_token_set_semicolon(CRToken * a_this)541 cr_token_set_semicolon (CRToken * a_this)
542 {
543 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
544
545 cr_token_clear (a_this);
546
547 a_this->type = SEMICOLON_TK;
548
549 return CR_OK;
550 }
551
552 enum CRStatus
cr_token_set_cbo(CRToken * a_this)553 cr_token_set_cbo (CRToken * a_this)
554 {
555 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
556
557 cr_token_clear (a_this);
558
559 a_this->type = CBO_TK;
560
561 return CR_OK;
562 }
563
564 enum CRStatus
cr_token_set_cbc(CRToken * a_this)565 cr_token_set_cbc (CRToken * a_this)
566 {
567 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
568
569 cr_token_clear (a_this);
570
571 a_this->type = CBC_TK;
572
573 return CR_OK;
574 }
575
576 enum CRStatus
cr_token_set_po(CRToken * a_this)577 cr_token_set_po (CRToken * a_this)
578 {
579 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
580
581 cr_token_clear (a_this);
582
583 a_this->type = PO_TK;
584
585 return CR_OK;
586 }
587
588 enum CRStatus
cr_token_set_pc(CRToken * a_this)589 cr_token_set_pc (CRToken * a_this)
590 {
591 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
592
593 cr_token_clear (a_this);
594
595 a_this->type = PC_TK;
596
597 return CR_OK;
598 }
599
600 enum CRStatus
cr_token_set_bo(CRToken * a_this)601 cr_token_set_bo (CRToken * a_this)
602 {
603 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
604
605 cr_token_clear (a_this);
606
607 a_this->type = BO_TK;
608
609 return CR_OK;
610 }
611
612 enum CRStatus
cr_token_set_bc(CRToken * a_this)613 cr_token_set_bc (CRToken * a_this)
614 {
615 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
616
617 cr_token_clear (a_this);
618
619 a_this->type = BC_TK;
620
621 return CR_OK;
622 }
623
624 /**
625 *The destructor of the #CRToken class.
626 *@param a_this the current instance of #CRToken.
627 */
628 void
cr_token_destroy(CRToken * a_this)629 cr_token_destroy (CRToken * a_this)
630 {
631 g_return_if_fail (a_this);
632
633 cr_token_clear (a_this);
634
635 g_free (a_this);
636 }
637