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

Generated on Tue Apr 27 13:46:35 2004 for Libcroco by doxygen 1.3.6