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