cr-num.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  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of version 2.1 of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00019  * USA
00020  *
00021  * Author: Dodji Seketeli
00022  * See COPYRIGHTS file for copyrights information.
00023  */
00024 
00025 /**
00026  *@CRNum:
00027  *
00028  *The definition
00029  *of the #CRNum class.
00030  */
00031 
00032 #include "cr-num.h"
00033 #include "string.h"
00034 
00035 /**
00036  * cr_num_new:
00037  *
00038  *#CRNum.
00039  *
00040  *Returns the newly built instance of
00041  *#CRNum.
00042  */
00043 CRNum *
00044 cr_num_new (void)
00045 {
00046         CRNum *result = NULL;
00047 
00048         result = g_try_malloc (sizeof (CRNum));
00049 
00050         if (result == NULL) {
00051                 cr_utils_trace_info ("Out of memory");
00052                 return NULL;
00053         }
00054 
00055         memset (result, 0, sizeof (CRNum));
00056 
00057         return result;
00058 }
00059 
00060 /**
00061  * cr_num_new_with_val:
00062  * @a_val: the numerical value of the number.
00063  * @a_type: the type of number.
00064  * 
00065  * A constructor of #CRNum.
00066  *
00067  * Returns the newly built instance of #CRNum or
00068  * NULL if an error arises.
00069  */
00070 CRNum *
00071 cr_num_new_with_val (gdouble a_val, enum CRNumType a_type)
00072 {
00073         CRNum *result = NULL;
00074 
00075         result = cr_num_new ();
00076 
00077         g_return_val_if_fail (result, NULL);
00078 
00079         result->val = a_val;
00080         result->type = a_type;
00081 
00082         return result;
00083 }
00084 
00085 /**
00086  * cr_num_to_string:
00087  *@a_this: the current instance of #CRNum.
00088  *
00089  *Returns the newly built string representation
00090  *of the current instance of #CRNum. The returned
00091  *string is NULL terminated. The caller *must*
00092  *free the returned string.
00093  */
00094 guchar *
00095 cr_num_to_string (CRNum * a_this)
00096 {
00097         gdouble test_val = 0.0;
00098 
00099         guchar *tmp_char1 = NULL,
00100                 *tmp_char2 = NULL,
00101                 *result = NULL;
00102 
00103         g_return_val_if_fail (a_this, NULL);
00104 
00105         test_val = a_this->val - (glong) a_this->val;
00106 
00107         if (!test_val) {
00108                 tmp_char1 = g_strdup_printf ("%ld", (glong) a_this->val);
00109         } else {
00110                 tmp_char1 = g_strdup_printf ("%.3f", a_this->val);
00111         }
00112 
00113         g_return_val_if_fail (tmp_char1, NULL);
00114 
00115         switch (a_this->type) {
00116         case NUM_LENGTH_EM:
00117                 tmp_char2 = (guchar *) "em";
00118                 break;
00119 
00120         case NUM_LENGTH_EX:
00121                 tmp_char2 = (guchar *) "ex";
00122                 break;
00123 
00124         case NUM_LENGTH_PX:
00125                 tmp_char2 = (guchar *) "px";
00126                 break;
00127 
00128         case NUM_LENGTH_IN:
00129                 tmp_char2 = (guchar *) "in";
00130                 break;
00131 
00132         case NUM_LENGTH_CM:
00133                 tmp_char2 = (guchar *) "cm";
00134                 break;
00135 
00136         case NUM_LENGTH_MM:
00137                 tmp_char2 = (guchar *) "mm";
00138                 break;
00139 
00140         case NUM_LENGTH_PT:
00141                 tmp_char2 = (guchar *) "pt";
00142                 break;
00143 
00144         case NUM_LENGTH_PC:
00145                 tmp_char2 = (guchar *) "pc";
00146                 break;
00147 
00148         case NUM_ANGLE_DEG:
00149                 tmp_char2 = (guchar *) "deg";
00150                 break;
00151 
00152         case NUM_ANGLE_RAD:
00153                 tmp_char2 = (guchar *) "rad";
00154                 break;
00155 
00156         case NUM_ANGLE_GRAD:
00157                 tmp_char2 = (guchar *) "grad";
00158                 break;
00159 
00160         case NUM_TIME_MS:
00161                 tmp_char2 = (guchar *) "ms";
00162                 break;
00163 
00164         case NUM_TIME_S:
00165                 tmp_char2 = (guchar *) "s";
00166                 break;
00167 
00168         case NUM_FREQ_HZ:
00169                 tmp_char2 = (guchar *) "Hz";
00170                 break;
00171 
00172         case NUM_FREQ_KHZ:
00173                 tmp_char2 = (guchar *) "KHz";
00174                 break;
00175 
00176         case NUM_PERCENTAGE:
00177                 tmp_char2 = (guchar *) "%";
00178                 break;
00179         case NUM_INHERIT:
00180                 tmp_char2 = (guchar *) "inherit";
00181                 break ;
00182         case NUM_AUTO:
00183                 tmp_char2 = (guchar *) "auto";
00184                 break ;
00185         case NUM_GENERIC:
00186                 tmp_char2 = NULL ;
00187                 break ;
00188         default:
00189                 tmp_char2 = (guchar *) "unknown";
00190                 break;
00191         }
00192 
00193         if (tmp_char2) {
00194                 result = g_strconcat (tmp_char1, tmp_char2, NULL);
00195                 g_free (tmp_char1);
00196         } else {
00197                 result = tmp_char1;
00198         }
00199 
00200         return result;
00201 }
00202 
00203 /**
00204  * cr_num_copy:
00205  *@a_src: the instance of #CRNum to copy.
00206  *Must be non NULL.
00207  *@a_dest: the destination of the copy.
00208  *Must be non NULL
00209  *
00210  *Copies an instance of #CRNum.
00211  *
00212  *Returns CR_OK upon successful completion, an
00213  *error code otherwise.
00214  */
00215 enum CRStatus
00216 cr_num_copy (CRNum * a_dest, CRNum * a_src)
00217 {
00218         g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR);
00219 
00220         memcpy (a_dest, a_src, sizeof (CRNum));
00221 
00222         return CR_OK;
00223 }
00224 
00225 /**
00226  * cr_num_dup:
00227  *@a_this: the instance of #CRNum to duplicate.
00228  *
00229  *Duplicates an instance of #CRNum
00230  *
00231  *Returns the newly created (duplicated) instance of #CRNum.
00232  *Must be freed by cr_num_destroy().
00233  */
00234 CRNum *
00235 cr_num_dup (CRNum * a_this)
00236 {
00237         CRNum *result = NULL;
00238         enum CRStatus status = CR_OK;
00239 
00240         g_return_val_if_fail (a_this, NULL);
00241 
00242         result = cr_num_new ();
00243         g_return_val_if_fail (result, NULL);
00244 
00245         status = cr_num_copy (result, a_this);
00246         g_return_val_if_fail (status == CR_OK, NULL);
00247 
00248         return result;
00249 }
00250 
00251 /**
00252  * cr_num_set:
00253  *Sets an instance of #CRNum.
00254  *@a_this: the current instance of #CRNum to be set.
00255  *@a_val: the new numerical value to be hold by the current
00256  *instance of #CRNum
00257  *@a_type: the new type of #CRNum.
00258  *
00259  * Returns CR_OK upon succesful completion, an error code otherwise.
00260  */
00261 enum CRStatus
00262 cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type)
00263 {
00264         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00265 
00266         a_this->val = a_val;
00267         a_this->type = a_type;
00268 
00269         return CR_OK;
00270 }
00271 
00272 /**
00273  * cr_num_is_fixed_length:
00274  * @a_this: the current instance of #CRNum .
00275  *
00276  *Tests if the current instance of #CRNum is a fixed
00277  *length value or not. Typically a fixed length value
00278  *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC.
00279  *See the definition of #CRNumType to see what we mean.
00280  *
00281  *Returns TRUE if the instance of #CRNum is a fixed length number,
00282  *FALSE otherwise.
00283  */
00284 gboolean
00285 cr_num_is_fixed_length (CRNum * a_this)
00286 {
00287         gboolean result = FALSE;
00288 
00289         g_return_val_if_fail (a_this, FALSE);
00290 
00291         if (a_this->type >= NUM_LENGTH_EM 
00292             && a_this->type <= NUM_LENGTH_PC) {
00293                 result = TRUE ;
00294         }
00295         return result ;
00296 }
00297 
00298 /**
00299  * cr_num_destroy:
00300  *@a_this: the this pointer of
00301  *the current instance of #CRNum.
00302  *
00303  *The destructor of #CRNum.
00304  */
00305 void
00306 cr_num_destroy (CRNum * a_this)
00307 {
00308         g_return_if_fail (a_this);
00309 
00310         g_free (a_this);
00311 }

Generated on Wed Mar 15 18:23:54 2006 for Libcroco by  doxygen 1.4.6