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

Generated on Sat Mar 20 02:38:42 2004 for Libcroco by doxygen 1.3.5