Libcroco
cr-token.c
Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00018  * USA
00019  * 
00020  * Author: Dodji Seketeli
00021  * see COPYRIGHTS file for copyright information.
00022  */
00023 
00024 /**
00025  *@file
00026  *The definition of the #CRToken class.
00027  *Abstracts a css2 token.
00028  */
00029 #include <string.h>
00030 #include "cr-token.h"
00031 
00032 /*
00033  *TODO: write a CRToken::to_string() method.
00034  */
00035 
00036 /**
00037  *Frees the attributes of the current instance
00038  *of #CRtoken.
00039  *@param a_this the current instance of #CRToken.
00040  */
00041 static void
00042 cr_token_clear (CRToken * a_this)
00043 {
00044         g_return_if_fail (a_this);
00045 
00046         switch (a_this->type) {
00047         case S_TK:
00048         case CDO_TK:
00049         case CDC_TK:
00050         case INCLUDES_TK:
00051         case DASHMATCH_TK:
00052         case PAGE_SYM_TK:
00053         case MEDIA_SYM_TK:
00054         case FONT_FACE_SYM_TK:
00055         case CHARSET_SYM_TK:
00056         case IMPORT_SYM_TK:
00057         case IMPORTANT_SYM_TK:
00058         case SEMICOLON_TK:
00059         case NO_TK:
00060         case DELIM_TK:
00061         case CBO_TK:
00062         case CBC_TK:
00063         case BO_TK:
00064         case BC_TK:
00065                 break;
00066 
00067         case STRING_TK:
00068         case IDENT_TK:
00069         case HASH_TK:
00070         case URI_TK:
00071         case FUNCTION_TK:
00072         case COMMENT_TK:
00073         case ATKEYWORD_TK:
00074                 if (a_this->u.str) {
00075                         cr_string_destroy (a_this->u.str);
00076                         a_this->u.str = NULL;
00077                 }
00078                 break;
00079 
00080         case EMS_TK:
00081         case EXS_TK:
00082         case LENGTH_TK:
00083         case ANGLE_TK:
00084         case TIME_TK:
00085         case FREQ_TK:
00086         case PERCENTAGE_TK:
00087         case NUMBER_TK:
00088         case PO_TK:
00089         case PC_TK:
00090                 if (a_this->u.num) {
00091                         cr_num_destroy (a_this->u.num);
00092                         a_this->u.num = NULL;
00093                 }
00094                 break;
00095 
00096         case DIMEN_TK:
00097                 if (a_this->u.num) {
00098                         cr_num_destroy (a_this->u.num);
00099                         a_this->u.num = NULL;
00100                 }
00101 
00102                 if (a_this->dimen) {
00103                         cr_string_destroy (a_this->dimen);
00104                         a_this->dimen = NULL;
00105                 }
00106 
00107                 break;
00108 
00109         case RGB_TK:
00110                 if (a_this->u.rgb) {
00111                         cr_rgb_destroy (a_this->u.rgb) ;
00112                         a_this->u.rgb = NULL ;
00113                 }
00114                 break ;
00115 
00116         case UNICODERANGE_TK:
00117                 /*not supported yet. */
00118                 break;
00119 
00120         default:
00121                 cr_utils_trace_info ("I don't know how to clear this token\n") ;
00122                 break;
00123         }
00124 
00125         a_this->type = NO_TK;
00126 }
00127 
00128 /**
00129  *Default constructor of
00130  *the #CRToken class.
00131  *@return the newly built instance of #CRToken.
00132  */
00133 CRToken *
00134 cr_token_new (void)
00135 {
00136         CRToken *result = NULL;
00137 
00138         result = g_try_malloc (sizeof (CRToken));
00139 
00140         if (result == NULL) {
00141                 cr_utils_trace_info ("Out of memory");
00142                 return NULL;
00143         }
00144 
00145         memset (result, 0, sizeof (CRToken));
00146 
00147         return result;
00148 }
00149 
00150 /**
00151  *Sets the type of curren instance of
00152  *#CRToken to 'S_TK' (S in the css2 spec)
00153  *@param a_this the current instance of #CRToken.
00154  *@return CR_OK upon successfull completion, an error
00155  *code otherwise.
00156  */
00157 enum CRStatus
00158 cr_token_set_s (CRToken * a_this)
00159 {
00160         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00161 
00162         cr_token_clear (a_this);
00163 
00164         a_this->type = S_TK;
00165 
00166         return CR_OK;
00167 }
00168 
00169 /**
00170  *Sets the type of the current instance of
00171  *#CRToken to 'CDO_TK' (CDO as said by the css2 spec)
00172  *@param a_this the current instance of #CRToken.
00173  *@return CR_OK upon successfull completion, an error
00174  *code otherwise.
00175  */
00176 enum CRStatus
00177 cr_token_set_cdo (CRToken * a_this)
00178 {
00179         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00180 
00181         cr_token_clear (a_this);
00182 
00183         a_this->type = CDO_TK;
00184 
00185         return CR_OK;
00186 }
00187 
00188 /**
00189  *Sets the type of the current token to
00190  *CDC_TK (CDC as said by the css2 spec).
00191  *@param a_this the current instance of #CRToken.
00192  *@return CR_OK upon successfull completion, an error
00193  *code otherwise.
00194  */
00195 enum CRStatus
00196 cr_token_set_cdc (CRToken * a_this)
00197 {
00198         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00199 
00200         cr_token_clear (a_this);
00201 
00202         a_this->type = CDC_TK;
00203 
00204         return CR_OK;
00205 }
00206 
00207 /**
00208  *Sets the type of the current instance of
00209  *#CRToken to INCLUDES_TK (INCLUDES as said by the css2 spec).
00210  *@param a_this the current instance of #CRToken.
00211  *@return CR_OK upon successfull completion, an error
00212  *code otherwise.
00213  */
00214 enum CRStatus
00215 cr_token_set_includes (CRToken * a_this)
00216 {
00217         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00218 
00219         cr_token_clear (a_this);
00220 
00221         a_this->type = INCLUDES_TK;
00222 
00223         return CR_OK;
00224 }
00225 
00226 /**
00227  *Sets the type of the current instance of
00228  *#CRToken to DASHMATCH_TK (DASHMATCH as said by the css2 spec).
00229  *@param a_this the current instance of #CRToken.
00230  *@return CR_OK upon successfull completion, an error
00231  *code otherwise.
00232  */
00233 enum CRStatus
00234 cr_token_set_dashmatch (CRToken * a_this)
00235 {
00236         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00237 
00238         cr_token_clear (a_this);
00239 
00240         a_this->type = DASHMATCH_TK;
00241 
00242         return CR_OK;
00243 }
00244 
00245 enum CRStatus
00246 cr_token_set_comment (CRToken * a_this, CRString * a_str)
00247 {
00248         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00249 
00250         cr_token_clear (a_this);
00251         a_this->type = COMMENT_TK;
00252         a_this->u.str = a_str ;
00253         return CR_OK;
00254 }
00255 
00256 enum CRStatus
00257 cr_token_set_string (CRToken * a_this, CRString * a_str)
00258 {
00259         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00260 
00261         cr_token_clear (a_this);
00262 
00263         a_this->type = STRING_TK;
00264 
00265         a_this->u.str = a_str ;
00266 
00267         return CR_OK;
00268 }
00269 
00270 enum CRStatus
00271 cr_token_set_ident (CRToken * a_this, CRString * a_ident)
00272 {
00273         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00274 
00275         cr_token_clear (a_this);
00276         a_this->type = IDENT_TK;
00277         a_this->u.str = a_ident;
00278         return CR_OK;
00279 }
00280 
00281 
00282 enum CRStatus
00283 cr_token_set_function (CRToken * a_this, CRString * a_fun_name)
00284 {
00285         g_return_val_if_fail (a_this,
00286                               CR_BAD_PARAM_ERROR);
00287 
00288         cr_token_clear (a_this);
00289         a_this->type = FUNCTION_TK;
00290         a_this->u.str  = a_fun_name;
00291         return CR_OK;
00292 }
00293 
00294 enum CRStatus
00295 cr_token_set_hash (CRToken * a_this, CRString * a_hash)
00296 {
00297         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00298 
00299         cr_token_clear (a_this);
00300         a_this->type = HASH_TK;
00301         a_this->u.str = a_hash;
00302 
00303         return CR_OK;
00304 }
00305 
00306 enum CRStatus
00307 cr_token_set_rgb (CRToken * a_this, CRRgb * a_rgb)
00308 {
00309         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00310 
00311         cr_token_clear (a_this);
00312         a_this->type = RGB_TK;
00313         a_this->u.rgb = a_rgb;
00314 
00315         return CR_OK;
00316 }
00317 
00318 enum CRStatus
00319 cr_token_set_import_sym (CRToken * a_this)
00320 {
00321         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00322 
00323         cr_token_clear (a_this);
00324 
00325         a_this->type = IMPORT_SYM_TK;
00326 
00327         return CR_OK;
00328 }
00329 
00330 enum CRStatus
00331 cr_token_set_page_sym (CRToken * a_this)
00332 {
00333         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00334 
00335         cr_token_clear (a_this);
00336 
00337         a_this->type = PAGE_SYM_TK;
00338 
00339         return CR_OK;
00340 }
00341 
00342 enum CRStatus
00343 cr_token_set_media_sym (CRToken * a_this)
00344 {
00345         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00346 
00347         cr_token_clear (a_this);
00348 
00349         a_this->type = MEDIA_SYM_TK;
00350 
00351         return CR_OK;
00352 }
00353 
00354 enum CRStatus
00355 cr_token_set_font_face_sym (CRToken * a_this)
00356 {
00357         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00358 
00359         cr_token_clear (a_this);
00360         a_this->type = FONT_FACE_SYM_TK;
00361 
00362         return CR_OK;
00363 }
00364 
00365 enum CRStatus
00366 cr_token_set_charset_sym (CRToken * a_this)
00367 {
00368         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00369 
00370         cr_token_clear (a_this);
00371         a_this->type = CHARSET_SYM_TK;
00372 
00373         return CR_OK;
00374 }
00375 
00376 enum CRStatus
00377 cr_token_set_atkeyword (CRToken * a_this, CRString * a_atname)
00378 {
00379         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00380 
00381         cr_token_clear (a_this);
00382         a_this->type = ATKEYWORD_TK;
00383         a_this->u.str = a_atname;
00384         return CR_OK;
00385 }
00386 
00387 enum CRStatus
00388 cr_token_set_important_sym (CRToken * a_this)
00389 {
00390         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00391         cr_token_clear (a_this);
00392         a_this->type = IMPORTANT_SYM_TK;
00393         return CR_OK;
00394 }
00395 
00396 enum CRStatus
00397 cr_token_set_ems (CRToken * a_this, CRNum * a_num)
00398 {
00399         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00400         cr_token_clear (a_this);
00401         a_this->type = EMS_TK;
00402         a_this->u.num = a_num;
00403         return CR_OK;
00404 }
00405 
00406 enum CRStatus
00407 cr_token_set_exs (CRToken * a_this, CRNum * a_num)
00408 {
00409         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00410         cr_token_clear (a_this);
00411         a_this->type = EXS_TK;
00412         a_this->u.num = a_num;
00413         return CR_OK;
00414 }
00415 
00416 enum CRStatus
00417 cr_token_set_length (CRToken * a_this, CRNum * a_num,
00418                      enum CRTokenExtraType a_et)
00419 {
00420         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00421 
00422         cr_token_clear (a_this);
00423 
00424         a_this->type = LENGTH_TK;
00425         a_this->extra_type = a_et;
00426         a_this->u.num = a_num;
00427 
00428         return CR_OK;
00429 }
00430 
00431 enum CRStatus
00432 cr_token_set_angle (CRToken * a_this, CRNum * a_num,
00433                     enum CRTokenExtraType a_et)
00434 {
00435         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00436 
00437         cr_token_clear (a_this);
00438 
00439         a_this->type = ANGLE_TK;
00440         a_this->extra_type = a_et;
00441         a_this->u.num = a_num;
00442 
00443         return CR_OK;
00444 }
00445 
00446 enum CRStatus
00447 cr_token_set_time (CRToken * a_this, CRNum * a_num,
00448                    enum CRTokenExtraType a_et)
00449 {
00450         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00451 
00452         cr_token_clear (a_this);
00453 
00454         a_this->type = TIME_TK;
00455         a_this->extra_type = a_et;
00456         a_this->u.num = a_num;
00457 
00458         return CR_OK;
00459 }
00460 
00461 enum CRStatus
00462 cr_token_set_freq (CRToken * a_this, CRNum * a_num,
00463                    enum CRTokenExtraType a_et)
00464 {
00465         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00466 
00467         cr_token_clear (a_this);
00468 
00469         a_this->type = FREQ_TK;
00470         a_this->extra_type = a_et;
00471         a_this->u.num = a_num;
00472 
00473         return CR_OK;
00474 }
00475 
00476 enum CRStatus
00477 cr_token_set_dimen (CRToken * a_this, CRNum * a_num, 
00478                     CRString * a_dim)
00479 {
00480         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00481         cr_token_clear (a_this);
00482         a_this->type = DIMEN_TK;
00483         a_this->u.num = a_num;
00484         a_this->dimen = a_dim;
00485         return CR_OK;
00486 
00487 }
00488 
00489 enum CRStatus
00490 cr_token_set_percentage (CRToken * a_this, CRNum * a_num)
00491 {
00492         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00493 
00494         cr_token_clear (a_this);
00495 
00496         a_this->type = PERCENTAGE_TK;
00497         a_this->u.num = a_num;
00498 
00499         return CR_OK;
00500 }
00501 
00502 enum CRStatus
00503 cr_token_set_number (CRToken * a_this, CRNum * a_num)
00504 {
00505         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00506 
00507         cr_token_clear (a_this);
00508 
00509         a_this->type = NUMBER_TK;
00510         a_this->u.num = a_num;
00511         return CR_OK;
00512 }
00513 
00514 enum CRStatus
00515 cr_token_set_uri (CRToken * a_this, CRString * a_uri)
00516 {
00517         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00518 
00519         cr_token_clear (a_this);
00520 
00521         a_this->type = URI_TK;
00522         a_this->u.str = a_uri;
00523 
00524         return CR_OK;
00525 }
00526 
00527 enum CRStatus
00528 cr_token_set_delim (CRToken * a_this, guint32 a_char)
00529 {
00530         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00531 
00532         cr_token_clear (a_this);
00533 
00534         a_this->type = DELIM_TK;
00535         a_this->u.unichar = a_char;
00536 
00537         return CR_OK;
00538 }
00539 
00540 enum CRStatus
00541 cr_token_set_semicolon (CRToken * a_this)
00542 {
00543         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00544 
00545         cr_token_clear (a_this);
00546 
00547         a_this->type = SEMICOLON_TK;
00548 
00549         return CR_OK;
00550 }
00551 
00552 enum CRStatus
00553 cr_token_set_cbo (CRToken * a_this)
00554 {
00555         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00556 
00557         cr_token_clear (a_this);
00558 
00559         a_this->type = CBO_TK;
00560 
00561         return CR_OK;
00562 }
00563 
00564 enum CRStatus
00565 cr_token_set_cbc (CRToken * a_this)
00566 {
00567         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00568 
00569         cr_token_clear (a_this);
00570 
00571         a_this->type = CBC_TK;
00572 
00573         return CR_OK;
00574 }
00575 
00576 enum CRStatus
00577 cr_token_set_po (CRToken * a_this)
00578 {
00579         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00580 
00581         cr_token_clear (a_this);
00582 
00583         a_this->type = PO_TK;
00584 
00585         return CR_OK;
00586 }
00587 
00588 enum CRStatus
00589 cr_token_set_pc (CRToken * a_this)
00590 {
00591         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00592 
00593         cr_token_clear (a_this);
00594 
00595         a_this->type = PC_TK;
00596 
00597         return CR_OK;
00598 }
00599 
00600 enum CRStatus
00601 cr_token_set_bo (CRToken * a_this)
00602 {
00603         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00604 
00605         cr_token_clear (a_this);
00606 
00607         a_this->type = BO_TK;
00608 
00609         return CR_OK;
00610 }
00611 
00612 enum CRStatus
00613 cr_token_set_bc (CRToken * a_this)
00614 {
00615         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00616 
00617         cr_token_clear (a_this);
00618 
00619         a_this->type = BC_TK;
00620 
00621         return CR_OK;
00622 }
00623 
00624 /**
00625  *The destructor of the #CRToken class.
00626  *@param a_this the current instance of #CRToken.
00627  */
00628 void
00629 cr_token_destroy (CRToken * a_this)
00630 {
00631         g_return_if_fail (a_this);
00632 
00633         cr_token_clear (a_this);
00634 
00635         g_free (a_this);
00636 }