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

Generated on Thu Mar 9 19:19:09 2006 for Libcroco by  doxygen 1.4.6