summaryrefslogtreecommitdiffstats
path: root/src/st/croco/cr-num.c
blob: ba17285b2f722edb198ca384d4b953c5556b6a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */

/*
 * This file is part of The Croco Library
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Author: Dodji Seketeli
 * See COPYRIGHTS file for copyrights information.
 */

/**
 *@CRNum:
 *
 *The definition
 *of the #CRNum class.
 */

#include "cr-num.h"
#include "string.h"

/**
 * cr_num_new:
 *
 *#CRNum.
 *
 *Returns the newly built instance of
 *#CRNum.
 */
CRNum *
cr_num_new (void)
{
        CRNum *result = NULL;

        result = g_try_malloc (sizeof (CRNum));

        if (result == NULL) {
                cr_utils_trace_info ("Out of memory");
                return NULL;
        }

        memset (result, 0, sizeof (CRNum));

        return result;
}

/**
 * cr_num_new_with_val:
 * @a_val: the numerical value of the number.
 * @a_type: the type of number.
 * 
 * A constructor of #CRNum.
 *
 * Returns the newly built instance of #CRNum or
 * NULL if an error arises.
 */
CRNum *
cr_num_new_with_val (gdouble a_val, enum CRNumType a_type)
{
        CRNum *result = NULL;

        result = cr_num_new ();

        g_return_val_if_fail (result, NULL);

        result->val = a_val;
        result->type = a_type;

        return result;
}

/**
 * cr_num_to_string:
 *@a_this: the current instance of #CRNum.
 *
 *Returns the newly built string representation
 *of the current instance of #CRNum. The returned
 *string is NULL terminated. The caller *must*
 *free the returned string.
 */
guchar *
cr_num_to_string (CRNum const * a_this)
{
        gdouble test_val = 0.0;

        guchar *tmp_char1 = NULL,
                *tmp_char2 = NULL,
                *result = NULL;

        g_return_val_if_fail (a_this, NULL);

        test_val = a_this->val - (glong) a_this->val;

        if (!test_val) {
                tmp_char1 = (guchar *) g_strdup_printf ("%ld", (glong) a_this->val);
        } else {
                tmp_char1 = (guchar *) g_new0 (char, G_ASCII_DTOSTR_BUF_SIZE + 1);
                if (tmp_char1 != NULL)
                        g_ascii_dtostr ((gchar *) tmp_char1, G_ASCII_DTOSTR_BUF_SIZE, a_this->val);
        }

        g_return_val_if_fail (tmp_char1, NULL);

        switch (a_this->type) {
        case NUM_LENGTH_EM:
                tmp_char2 = (guchar *) "em";
                break;

        case NUM_LENGTH_EX:
                tmp_char2 = (guchar *) "ex";
                break;

        case NUM_LENGTH_PX:
                tmp_char2 = (guchar *) "px";
                break;

        case NUM_LENGTH_IN:
                tmp_char2 = (guchar *) "in";
                break;

        case NUM_LENGTH_CM:
                tmp_char2 = (guchar *) "cm";
                break;

        case NUM_LENGTH_MM:
                tmp_char2 = (guchar *) "mm";
                break;

        case NUM_LENGTH_PT:
                tmp_char2 = (guchar *) "pt";
                break;

        case NUM_LENGTH_PC:
                tmp_char2 = (guchar *) "pc";
                break;

        case NUM_ANGLE_DEG:
                tmp_char2 = (guchar *) "deg";
                break;

        case NUM_ANGLE_RAD:
                tmp_char2 = (guchar *) "rad";
                break;

        case NUM_ANGLE_GRAD:
                tmp_char2 = (guchar *) "grad";
                break;

        case NUM_TIME_MS:
                tmp_char2 = (guchar *) "ms";
                break;

        case NUM_TIME_S:
                tmp_char2 = (guchar *) "s";
                break;

        case NUM_FREQ_HZ:
                tmp_char2 = (guchar *) "Hz";
                break;

        case NUM_FREQ_KHZ:
                tmp_char2 = (guchar *) "KHz";
                break;

        case NUM_PERCENTAGE:
                tmp_char2 = (guchar *) "%";
                break;
        case NUM_INHERIT:
                tmp_char2 = (guchar *) "inherit";
                break ;
        case NUM_AUTO:
                tmp_char2 = (guchar *) "auto";
                break ;
        case NUM_GENERIC:
                tmp_char2 = NULL ;
                break ;
        default:
                tmp_char2 = (guchar *) "unknown";
                break;
        }

        if (tmp_char2) {
                result = (guchar *)  g_strconcat ((gchar *) tmp_char1, tmp_char2, NULL);
                g_free (tmp_char1);
        } else {
                result = tmp_char1;
        }

        return result;
}

/**
 * cr_num_copy:
 *@a_src: the instance of #CRNum to copy.
 *Must be non NULL.
 *@a_dest: the destination of the copy.
 *Must be non NULL
 *
 *Copies an instance of #CRNum.
 *
 *Returns CR_OK upon successful completion, an
 *error code otherwise.
 */
enum CRStatus
cr_num_copy (CRNum * a_dest, CRNum const * a_src)
{
        g_return_val_if_fail (a_dest && a_src, CR_BAD_PARAM_ERROR);

        memcpy (a_dest, a_src, sizeof (CRNum));

        return CR_OK;
}

/**
 * cr_num_dup:
 *@a_this: the instance of #CRNum to duplicate.
 *
 *Duplicates an instance of #CRNum
 *
 *Returns the newly created (duplicated) instance of #CRNum.
 *Must be freed by cr_num_destroy().
 */
CRNum *
cr_num_dup (CRNum const * a_this)
{
        CRNum *result = NULL;
        enum CRStatus status = CR_OK;

        g_return_val_if_fail (a_this, NULL);

        result = cr_num_new ();
        g_return_val_if_fail (result, NULL);

        status = cr_num_copy (result, a_this);
        g_return_val_if_fail (status == CR_OK, NULL);

        return result;
}

/**
 * cr_num_set:
 *Sets an instance of #CRNum.
 *@a_this: the current instance of #CRNum to be set.
 *@a_val: the new numerical value to be hold by the current
 *instance of #CRNum
 *@a_type: the new type of #CRNum.
 *
 * Returns CR_OK upon successful completion, an error code otherwise.
 */
enum CRStatus
cr_num_set (CRNum * a_this, gdouble a_val, enum CRNumType a_type)
{
        g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);

        a_this->val = a_val;
        a_this->type = a_type;

        return CR_OK;
}

/**
 * cr_num_is_fixed_length:
 * @a_this: the current instance of #CRNum .
 *
 *Tests if the current instance of #CRNum is a fixed
 *length value or not. Typically a fixed length value
 *is anything from NUM_LENGTH_EM to NUM_LENGTH_PC.
 *See the definition of #CRNumType to see what we mean.
 *
 *Returns TRUE if the instance of #CRNum is a fixed length number,
 *FALSE otherwise.
 */
gboolean
cr_num_is_fixed_length (CRNum const * a_this)
{
        gboolean result = FALSE;

        g_return_val_if_fail (a_this, FALSE);

        if (a_this->type >= NUM_LENGTH_EM 
            && a_this->type <= NUM_LENGTH_PC) {
                result = TRUE ;
        }
        return result ;
}

/**
 * cr_num_destroy:
 *@a_this: the this pointer of
 *the current instance of #CRNum.
 *
 *The destructor of #CRNum.
 */
void
cr_num_destroy (CRNum * a_this)
{
        g_return_if_fail (a_this);

        g_free (a_this);
}