1/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */ 2 3/* 4 * This file is part of The Croco Library 5 * 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of version 2.1 of the GNU Lesser General Public 9 * License as published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * USA 20 * 21 * Author: Dodji Seketeli 22 * See COPYRIGHTS file for copyrights information. 23 */ 24 25/** 26 *@CRNum: 27 * 28 *The definition 29 *of the #CRNum class. 30 */ 31 32#include <config.h> 33#include "cr-num.h" 34#include "string.h" 35 36/** 37 * cr_num_new: 38 * 39 *#CRNum. 40 * 41 *Returns the newly built instance of 42 *#CRNum. 43 */ 44CRNum * 45cr_num_new (void) 46{ 47 CRNum *result = NULL; 48 49 result = g_try_malloc (sizeof (CRNum)); 50 51 if (result == NULL) { 52 cr_utils_trace_info ("Out of memory"); 53 return NULL; 54 } 55 56 memset (result, 0, sizeof (CRNum)); 57 58 return result; 59} 60 61/** 62 * cr_num_new_with_val: 63 * @a_val: the numerical value of the number. 64 * @a_type: the type of number. 65 * 66 * A constructor of #CRNum. 67 * 68 * Returns the newly built instance of #CRNum or 69 * NULL if an error arises. 70 */ 71CRNum * 72cr_num_new_with_val (gdouble a_val, enum CRNumType a_type) 73{ 74 CRNum *result = NULL; 75 76 result = cr_num_new (); 77 78 g_return_val_if_fail (result, NULL); 79 80 result->val = a_val; 81 result->type = a_type; 82 83 return result; 84} 85 86/** 87 * cr_num_to_string: 88 *@a_this: the current instance of #CRNum. 89 * 90 *Returns the newly built string representation 91 *of the current instance of #CRNum. The returned 92 *string is NULL terminated. The caller *must* 93 *free the returned string. 94 */ 95guchar * 96cr_num_to_string (CRNum * a_this) 97{ 98 gdouble test_val = 0.0; 99 100 guchar *tmp_char1 = NULL, 101 *tmp_char2 = NULL, 102 *result = NULL; 103 104 g_return_val_if_fail (a_this, NULL); 105 106 test_val = a_this->val - (glong) a_this->val; 107 108 if (!test_val) { 109 tmp_char1 = g_strdup_printf ("%ld", (glong) a_this->val); 110 } else { 111 tmp_char1 = g_strdup_printf ("%.3f", a_this->val); 112 } 113 114 g_return_val_if_fail (tmp_char1, NULL); 115 116 switch (a_this->type) { 117 case NUM_LENGTH_EM: 118 tmp_char2 = (guchar *) "em"; 119 break; 120 121 case NUM_LENGTH_EX: 122 tmp_char2 = (guchar *) "ex"; 123 break; 124 125 case NUM_LENGTH_PX: 126 tmp_char2 = (guchar *) "px"; 127 break; 128 129 case NUM_LENGTH_IN: 130 tmp_char2 = (guchar *) "in"; 131 break; 132 133 case NUM_LENGTH_CM: 134 tmp_char2 = (guchar *) "cm"; 135 break; 136 137 case NUM_LENGTH_MM: 138 tmp_char2 = (guchar *) "mm"; 139 break; 140 141 case NUM_LENGTH_PT: 142 tmp_char2 = (guchar *) "pt"; 143 break; 144 145 case NUM_LENGTH_PC: 146 tmp_char2 = (guchar *) "pc"; 147 break; 148 149 case NUM_ANGLE_DEG: 150 tmp_char2 = (guchar *) "deg"; 151 break; 152 153 case NUM_ANGLE_RAD: 154 tmp_char2 = (guchar *) "rad"; 155 break; 156 157 case NUM_ANGLE_GRAD: 158 tmp_char2 = (guchar *) "grad"; 159 break; 160 161 case NUM_TIME_MS: 162 tmp_char2 = (guchar *) "ms"; 163 break; 164 165 case NUM_TIME_S: 166 tmp_char2 = (guchar *) "s"; 167 break; 168 169 case NUM_FREQ_HZ: 170 tmp_char2 = (guchar *) "Hz"; 171 break; 172 173 case NUM_FREQ_KHZ: 174 tmp_char2 = (guchar *) "KHz"; 175 break; 176 177 case NUM_PERCENTAGE: 178 tmp_char2 = (guchar *) "%"; 179 break; 180 case NUM_INHERIT: 181 tmp_char2 = (guchar *) "inherit"; 182 break ; 183 case NUM_AUTO: 184 tmp_char2 = (guchar *) "auto"; 185 break ; 186 case NUM_GENERIC: 187 tmp_char2 = NULL ; 188 break ; 189 default: 190 tmp_char2 = (guchar *) "unknown"; 191 break; 192 } 193 194 if (tmp_char2) { 195 result = g_strconcat (tmp_char1, tmp_char2, NULL); 196 g_free (tmp_char1); 197 } else { 198 result = tmp_char1; 199 } 200 201 return result; 202} 203 204/** 205 * cr_num_copy: 206 *@a_src: the instance of #CRNum to copy. 207 *Must be non NULL. 208 *@a_dest: the destination of the copy. 209 *Must be non NULL 210 * 211 *Copies an instance of #CRNum. 212 * 213 *Returns CR_OK upon successful completion, an 214 *error code otherwise. 215 */ 216enum CRStatus 217cr_num_copy (CRNum * a_dest, CRNum * a_src) 218{ 219 g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR); 220 221 memcpy (a_dest, a_src, sizeof (CRNum)); 222 223 return CR_OK; 224} 225 226/** 227 * cr_num_dup: 228 *@a_this: the instance of #CRNum to duplicate. 229 * 230 *Duplicates an instance of #CRNum 231 * 232 *Returns the newly created (duplicated) instance of #CRNum. 233 *Must be freed by cr_num_destroy(). 234 */ 235CRNum * 236cr_num_dup (CRNum * a_this) 237{ 238 CRNum *result = NULL; 239 enum CRStatus status = CR_OK; 240 241 g_return_val_if_fail (a_this, NULL); 242 243 result = cr_num_new (); 244 g_return_val_if_fail (result, NULL); 245 246 status = cr_num_copy (result, a_this); 247 g_return_val_if_fail (status == CR_OK, NULL); 248 249 return result; 250} 251 252/** 253 * cr_num_set: 254 *Sets an instance of #CRNum. 255 *@a_this: the current instance of #CRNum to be set. 256 *@a_val: the new numerical value to be hold by the current 257 *instance of #CRNum 258 *@a_type: the new type of #CRNum. 259 * 260 * Returns CR_OK upon succesful completion, an error code otherwise. 261 */ 262enum CRStatus 263cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type) 264{ 265 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR); 266 267 a_this->val = a_val; 268 a_this->type = a_type; 269 270 return CR_OK; 271} 272 273/** 274 * cr_num_is_fixed_length: 275 * @a_this: the current instance of #CRNum . 276 * 277 *Tests if the current instance of #CRNum is a fixed 278 *length value or not. Typically a fixed length value 279 *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC. 280 *See the definition of #CRNumType to see what we mean. 281 * 282 *Returns TRUE if the instance of #CRNum is a fixed length number, 283 *FALSE otherwise. 284 */ 285gboolean 286cr_num_is_fixed_length (CRNum * a_this) 287{ 288 gboolean result = FALSE; 289 290 g_return_val_if_fail (a_this, FALSE); 291 292 if (a_this->type >= NUM_LENGTH_EM 293 && a_this->type <= NUM_LENGTH_PC) { 294 result = TRUE ; 295 } 296 return result ; 297} 298 299/** 300 * cr_num_destroy: 301 *@a_this: the this pointer of 302 *the current instance of #CRNum. 303 * 304 *The destructor of #CRNum. 305 */ 306void 307cr_num_destroy (CRNum * a_this) 308{ 309 g_return_if_fail (a_this); 310 311 g_free (a_this); 312} 313