Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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  *@file
00027  *The definition
00028  *of the #CRNum class.
00029  */
00030 
00031 #include "cr-num.h"
00032 #include "string.h"
00033 
00034 /**
00035  *The default constructor of
00036  *#CRNum.
00037  *@return the newly built instance of
00038  *#CRNum.
00039  */
00040 CRNum *
00041 cr_num_new (void)
00042 {
00043         CRNum *result = NULL;
00044 
00045         result = g_try_malloc (sizeof (CRNum));
00046 
00047         if (result == NULL) {
00048                 cr_utils_trace_info ("Out of memory");
00049                 return NULL;
00050         }
00051 
00052         memset (result, 0, sizeof (CRNum));
00053 
00054         return result;
00055 }
00056 
00057 /**
00058  *A constructor of #CRNum.
00059  *@param a_is_natural indicates whether the intance of #CRNum is 
00060  *a natural number or not.
00061  *@param a_integer_part the integer part of the instance 
00062  *of #CRNum
00063  *@param a_decimal_part in case the instance of #CRNum
00064  *natural number (but a decimal one) this parameter
00065  *is the decimal part of the instance of #CRNum.
00066  *@return the newly built instance of #CRNum or
00067  *NULL if an error arises.
00068  */
00069 CRNum *
00070 cr_num_new_with_val (gdouble a_val, enum CRNumType a_type)
00071 {
00072         CRNum *result = NULL;
00073 
00074         result = cr_num_new ();
00075 
00076         g_return_val_if_fail (result, NULL);
00077 
00078         result->val = a_val;
00079         result->type = a_type;
00080 
00081         return result;
00082 }
00083 
00084 /**
00085  *Returns the string representation of the
00086  *current instance of #CRNum.
00087  *@param a_this the current instance of #CRNum.
00088  *@return the newly built string representation
00089  *of the current instance of #CRNum. The returned
00090  *string is NULL terminated. The caller *must*
00091  *free the returned string.
00092  */
00093 guchar *
00094 cr_num_to_string (CRNum * a_this)
00095 {
00096         gdouble test_val = 0.0;
00097 
00098         guchar *tmp_char1 = NULL,
00099                 *tmp_char2 = NULL,
00100                 *result = NULL;
00101 
00102         g_return_val_if_fail (a_this, NULL);
00103 
00104         test_val = a_this->val - (glong) a_this->val;
00105 
00106         if (!test_val) {
00107                 tmp_char1 = g_strdup_printf ("%ld", (glong) a_this->val);
00108         } else {
00109                 tmp_char1 = g_strdup_printf ("%.3f", a_this->val);
00110         }
00111 
00112         g_return_val_if_fail (tmp_char1, NULL);
00113 
00114         switch (a_this->type) {
00115         case NUM_LENGTH_EM:
00116                 tmp_char2 = (guchar *) "em";
00117                 break;
00118 
00119         case NUM_LENGTH_EX:
00120                 tmp_char2 = (guchar *) "ex";
00121                 break;
00122 
00123         case NUM_LENGTH_PX:
00124                 tmp_char2 = (guchar *) "px";
00125                 break;
00126 
00127         case NUM_LENGTH_IN:
00128                 tmp_char2 = (guchar *) "in";
00129                 break;
00130 
00131         case NUM_LENGTH_CM:
00132                 tmp_char2 = (guchar *) "cm";
00133                 break;
00134 
00135         case NUM_LENGTH_MM:
00136                 tmp_char2 = (guchar *) "mm";
00137                 break;
00138 
00139         case NUM_LENGTH_PT:
00140                 tmp_char2 = (guchar *) "pt";
00141                 break;
00142 
00143         case NUM_LENGTH_PC:
00144                 tmp_char2 = (guchar *) "pc";
00145                 break;
00146 
00147         case NUM_ANGLE_DEG:
00148                 tmp_char2 = (guchar *) "deg";
00149                 break;
00150 
00151         case NUM_ANGLE_RAD:
00152                 tmp_char2 = (guchar *) "rad";
00153                 break;
00154 
00155         case NUM_ANGLE_GRAD:
00156                 tmp_char2 = (guchar *) "grad";
00157                 break;
00158 
00159         case NUM_TIME_MS:
00160                 tmp_char2 = (guchar *) "ms";
00161                 break;
00162 
00163         case NUM_TIME_S:
00164                 tmp_char2 = (guchar *) "s";
00165                 break;
00166 
00167         case NUM_FREQ_HZ:
00168                 tmp_char2 = (guchar *) "Hz";
00169                 break;
00170 
00171         case NUM_FREQ_KHZ:
00172                 tmp_char2 = (guchar *) "KHz";
00173                 break;
00174 
00175         case NUM_PERCENTAGE:
00176                 tmp_char2 = (guchar *) "%";
00177                 break;
00178         case NUM_INHERIT:
00179                 tmp_char2 = (guchar *) "inherit";
00180                 break ;
00181         case NUM_AUTO:
00182                 tmp_char2 = (guchar *) "auto";
00183                 break ;
00184         case NUM_GENERIC:
00185                 tmp_char2 = NULL ;
00186                 break ;
00187         default:
00188                 tmp_char2 = (guchar *) "unknown";
00189                 break;
00190         }
00191 
00192         if (tmp_char2) {
00193                 result = g_strconcat (tmp_char1, tmp_char2, NULL);
00194                 g_free (tmp_char1);
00195         } else {
00196                 result = tmp_char1;
00197         }
00198 
00199         return result;
00200 }
00201 
00202 /**
00203  *Copies an instance of #CRNum.
00204  *@param a_src the instance of #CRNum to copy.
00205  *Must be non NULL.
00206  *@param a_dst the destination of the copy.
00207  *Must be non NULL
00208  *@return CR_OK upon successful completion, an
00209  *error code otherwise.
00210  */
00211 enum CRStatus
00212 cr_num_copy (CRNum * a_dest, CRNum * a_src)
00213 {
00214         g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR);
00215 
00216         memcpy (a_dest, a_src, sizeof (CRNum));
00217 
00218         return CR_OK;
00219 }
00220 
00221 /**
00222  *Duplicates an instance of #CRNum
00223  *@param a_this the instance of #CRNum to duplicate.
00224  *@return the newly created (duplicated) instance of #CRNum.
00225  *Must be freed by cr_num_destroy().
00226  */
00227 CRNum *
00228 cr_num_dup (CRNum * a_this)
00229 {
00230         CRNum *result = NULL;
00231         enum CRStatus status = CR_OK;
00232 
00233         g_return_val_if_fail (a_this, NULL);
00234 
00235         result = cr_num_new ();
00236         g_return_val_if_fail (result, NULL);
00237 
00238         status = cr_num_copy (result, a_this);
00239         g_return_val_if_fail (status == CR_OK, NULL);
00240 
00241         return result;
00242 }
00243 
00244 /**
00245  *Sets an instance of #CRNum.
00246  *@param a_this the current instance of #CRNum to be set.
00247  *@param a_val the new numerical value to be hold by the current
00248  *instance of #CRNum
00249  *@param a_type the new type of #CRNum.
00250  */
00251 enum CRStatus
00252 cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type)
00253 {
00254         g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00255 
00256         a_this->val = a_val;
00257         a_this->type = a_type;
00258 
00259         return CR_OK;
00260 }
00261 
00262 /**
00263  *Tests if the current instance of #CRNum is a fixed
00264  *length value or not. Typically a fixed length value
00265  *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC.
00266  *See the definition of #CRNumType to see what we mean.
00267  *@return TRUE if the instance of #CRNum is a fixed length number,
00268  *FALSE otherwise.
00269  */
00270 gboolean
00271 cr_num_is_fixed_length (CRNum * a_this)
00272 {
00273         gboolean result = FALSE;
00274 
00275         g_return_val_if_fail (a_this, FALSE);
00276 
00277         if (a_this->type >= NUM_LENGTH_EM 
00278             && a_this->type <= NUM_LENGTH_PC) {
00279                 result = TRUE ;
00280         }
00281         return result ;
00282 }
00283 
00284 /**
00285  *The destructor of #CRNum.
00286  *@param a_this the this pointer of
00287  *the current instance of #CRNum.
00288  */
00289 void
00290 cr_num_destroy (CRNum * a_this)
00291 {
00292         g_return_if_fail (a_this);
00293 
00294         g_free (a_this);
00295 }

Generated on Fri Oct 29 08:29:11 2004 for Libcroco by  doxygen 1.3.9.1