PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/cssed-0.4.0/libcroco/parser/cr-num.c

#
C | 295 lines | 153 code | 53 blank | 89 comment | 11 complexity | bd4e42edbd9d741915219efc102ab7a4 MD5 | raw file
  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 *@file
 27 *The definition
 28 *of the #CRNum class.
 29 */
 30
 31#include "cr-num.h"
 32#include "string.h"
 33
 34/**
 35 *The default constructor of
 36 *#CRNum.
 37 *@return the newly built instance of
 38 *#CRNum.
 39 */
 40CRNum *
 41cr_num_new (void)
 42{
 43        CRNum *result = NULL;
 44
 45        result = g_try_malloc (sizeof (CRNum));
 46
 47        if (result == NULL) {
 48                cr_utils_trace_info ("Out of memory");
 49                return NULL;
 50        }
 51
 52        memset (result, 0, sizeof (CRNum));
 53
 54        return result;
 55}
 56
 57/**
 58 *A constructor of #CRNum.
 59 *@param a_is_natural indicates whether the intance of #CRNum is 
 60 *a natural number or not.
 61 *@param a_integer_part the integer part of the instance 
 62 *of #CRNum
 63 *@param a_decimal_part in case the instance of #CRNum
 64 *natural number (but a decimal one) this parameter
 65 *is the decimal part of the instance of #CRNum.
 66 *@return the newly built instance of #CRNum or
 67 *NULL if an error arises.
 68 */
 69CRNum *
 70cr_num_new_with_val (gdouble a_val, enum CRNumType a_type)
 71{
 72        CRNum *result = NULL;
 73
 74        result = cr_num_new ();
 75
 76        g_return_val_if_fail (result, NULL);
 77
 78        result->val = a_val;
 79        result->type = a_type;
 80
 81        return result;
 82}
 83
 84/**
 85 *Returns the string representation of the
 86 *current instance of #CRNum.
 87 *@param a_this the current instance of #CRNum.
 88 *@return the newly built string representation
 89 *of the current instance of #CRNum. The returned
 90 *string is NULL terminated. The caller *must*
 91 *free the returned string.
 92 */
 93guchar *
 94cr_num_to_string (CRNum * a_this)
 95{
 96        gdouble test_val = 0.0;
 97
 98        guchar *tmp_char1 = NULL,
 99                *tmp_char2 = NULL,
100                *result = NULL;
101
102        g_return_val_if_fail (a_this, NULL);
103
104        test_val = a_this->val - (glong) a_this->val;
105
106        if (!test_val) {
107                tmp_char1 = g_strdup_printf ("%ld", (glong) a_this->val);
108        } else {
109                tmp_char1 = g_strdup_printf ("%.3f", a_this->val);
110        }
111
112        g_return_val_if_fail (tmp_char1, NULL);
113
114        switch (a_this->type) {
115        case NUM_LENGTH_EM:
116                tmp_char2 = (guchar *) "em";
117                break;
118
119        case NUM_LENGTH_EX:
120                tmp_char2 = (guchar *) "ex";
121                break;
122
123        case NUM_LENGTH_PX:
124                tmp_char2 = (guchar *) "px";
125                break;
126
127        case NUM_LENGTH_IN:
128                tmp_char2 = (guchar *) "in";
129                break;
130
131        case NUM_LENGTH_CM:
132                tmp_char2 = (guchar *) "cm";
133                break;
134
135        case NUM_LENGTH_MM:
136                tmp_char2 = (guchar *) "mm";
137                break;
138
139        case NUM_LENGTH_PT:
140                tmp_char2 = (guchar *) "pt";
141                break;
142
143        case NUM_LENGTH_PC:
144                tmp_char2 = (guchar *) "pc";
145                break;
146
147        case NUM_ANGLE_DEG:
148                tmp_char2 = (guchar *) "deg";
149                break;
150
151        case NUM_ANGLE_RAD:
152                tmp_char2 = (guchar *) "rad";
153                break;
154
155        case NUM_ANGLE_GRAD:
156                tmp_char2 = (guchar *) "grad";
157                break;
158
159        case NUM_TIME_MS:
160                tmp_char2 = (guchar *) "ms";
161                break;
162
163        case NUM_TIME_S:
164                tmp_char2 = (guchar *) "s";
165                break;
166
167        case NUM_FREQ_HZ:
168                tmp_char2 = (guchar *) "Hz";
169                break;
170
171        case NUM_FREQ_KHZ:
172                tmp_char2 = (guchar *) "KHz";
173                break;
174
175        case NUM_PERCENTAGE:
176                tmp_char2 = (guchar *) "%";
177                break;
178        case NUM_INHERIT:
179                tmp_char2 = (guchar *) "inherit";
180                break ;
181        case NUM_AUTO:
182                tmp_char2 = (guchar *) "auto";
183                break ;
184        case NUM_GENERIC:
185                tmp_char2 = NULL ;
186                break ;
187        default:
188                tmp_char2 = (guchar *) "unknown";
189                break;
190        }
191
192        if (tmp_char2) {
193                result = g_strconcat (tmp_char1, tmp_char2, NULL);
194                g_free (tmp_char1);
195        } else {
196                result = tmp_char1;
197        }
198
199        return result;
200}
201
202/**
203 *Copies an instance of #CRNum.
204 *@param a_src the instance of #CRNum to copy.
205 *Must be non NULL.
206 *@param a_dst the destination of the copy.
207 *Must be non NULL
208 *@return CR_OK upon successful completion, an
209 *error code otherwise.
210 */
211enum CRStatus
212cr_num_copy (CRNum * a_dest, CRNum * a_src)
213{
214        g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR);
215
216        memcpy (a_dest, a_src, sizeof (CRNum));
217
218        return CR_OK;
219}
220
221/**
222 *Duplicates an instance of #CRNum
223 *@param a_this the instance of #CRNum to duplicate.
224 *@return the newly created (duplicated) instance of #CRNum.
225 *Must be freed by cr_num_destroy().
226 */
227CRNum *
228cr_num_dup (CRNum * a_this)
229{
230        CRNum *result = NULL;
231        enum CRStatus status = CR_OK;
232
233        g_return_val_if_fail (a_this, NULL);
234
235        result = cr_num_new ();
236        g_return_val_if_fail (result, NULL);
237
238        status = cr_num_copy (result, a_this);
239        g_return_val_if_fail (status == CR_OK, NULL);
240
241        return result;
242}
243
244/**
245 *Sets an instance of #CRNum.
246 *@param a_this the current instance of #CRNum to be set.
247 *@param a_val the new numerical value to be hold by the current
248 *instance of #CRNum
249 *@param a_type the new type of #CRNum.
250 */
251enum CRStatus
252cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type)
253{
254        g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
255
256        a_this->val = a_val;
257        a_this->type = a_type;
258
259        return CR_OK;
260}
261
262/**
263 *Tests if the current instance of #CRNum is a fixed
264 *length value or not. Typically a fixed length value
265 *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC.
266 *See the definition of #CRNumType to see what we mean.
267 *@return TRUE if the instance of #CRNum is a fixed length number,
268 *FALSE otherwise.
269 */
270gboolean
271cr_num_is_fixed_length (CRNum * a_this)
272{
273        gboolean result = FALSE;
274
275        g_return_val_if_fail (a_this, FALSE);
276
277        if (a_this->type >= NUM_LENGTH_EM 
278            && a_this->type <= NUM_LENGTH_PC) {
279                result = TRUE ;
280        }
281        return result ;
282}
283
284/**
285 *The destructor of #CRNum.
286 *@param a_this the this pointer of
287 *the current instance of #CRNum.
288 */
289void
290cr_num_destroy (CRNum * a_this)
291{
292        g_return_if_fail (a_this);
293
294        g_free (a_this);
295}