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

Generated on Wed Oct 1 01:36:45 2003 for Libcroco by doxygen 1.3.3