@@ -35,6 +35,21 @@ typedef enum {
35
35
JIMP_NUMBER ,
36
36
} Jimp_Token ;
37
37
38
+ typedef enum {
39
+ JIMP_FLOATING ,
40
+ JIMP_INTEGER
41
+ } JimpNumberType ;
42
+
43
+ union JimpNumberValue {
44
+ double f ;
45
+ long long i ;
46
+ };
47
+
48
+ typedef struct {
49
+ JimpNumberType type ;
50
+ union JimpNumberValue value ;
51
+ } JimpNumber ;
52
+
38
53
typedef struct {
39
54
const char * file_path ;
40
55
const char * start ;
@@ -47,7 +62,9 @@ typedef struct {
47
62
char * string ;
48
63
size_t string_count ;
49
64
size_t string_capacity ;
50
- double number ;
65
+ JimpNumber number ;
66
+ double floating ;
67
+ long long integer ;
51
68
bool boolean ;
52
69
} Jimp ;
53
70
@@ -63,6 +80,14 @@ bool jimp_boolean(Jimp *jimp);
63
80
/// Any consequent calls to the jimp_* functions may invalidate jimp->number.
64
81
bool jimp_number (Jimp * jimp );
65
82
83
+ /// If succeeds puts the freshly parsed number into jimp->number and jimp->floating.
84
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->number and jimp->floating.
85
+ bool jimp_float (Jimp * jimp );
86
+
87
+ /// If succeeds puts the freshly parsed number into jimp->number and jimp->integer.
88
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->number and jimp->integer.
89
+ bool jimp_integer (Jimp * jimp );
90
+
66
91
/// If succeeds puts the freshly parsed string into jimp->string as a NULL-terminated string.
67
92
/// Any consequent calls to the jimp_* functions may invalidate jimp->string.
68
93
/// strdup it if you don't wanna lose it (memory management is on you at that point).
@@ -175,8 +200,16 @@ static bool jimp__get_token(Jimp *jimp)
175
200
}
176
201
177
202
char * endptr = NULL ;
178
- jimp -> number = strtod (jimp -> point , & endptr ); // TODO: This implies that jimp->end is a valid address and *jimp->end == 0
203
+ jimp -> number .value .i = strtoull (jimp -> point , & endptr , 0 ); // TODO: This implies that jimp->end is a valid address and *jimp->end == 0
204
+ if (jimp -> point != endptr && * endptr != '.' ) {
205
+ jimp -> number .type = JIMP_INTEGER ;
206
+ jimp -> point = endptr ;
207
+ jimp -> token = JIMP_NUMBER ;
208
+ return true;
209
+ }
210
+ jimp -> number .value .f = strtod (jimp -> point , & endptr );
179
211
if (jimp -> point != endptr ) {
212
+ jimp -> number .type = JIMP_FLOATING ;
180
213
jimp -> point = endptr ;
181
214
jimp -> token = JIMP_NUMBER ;
182
215
return true;
@@ -370,6 +403,20 @@ bool jimp_number(Jimp *jimp)
370
403
return jimp__get_and_expect_token (jimp , JIMP_NUMBER );
371
404
}
372
405
406
+ bool jimp_float (Jimp * jimp )
407
+ {
408
+ if (!jimp__get_and_expect_token (jimp , JIMP_NUMBER )) return false;
409
+ jimp -> floating = jimp -> number .type == JIMP_FLOATING ? jimp -> number .value .f : jimp -> number .value .i ;
410
+ return true;
411
+ }
412
+
413
+ bool jimp_integer (Jimp * jimp )
414
+ {
415
+ if (!jimp__get_and_expect_token (jimp , JIMP_NUMBER ) || jimp -> number .type != JIMP_INTEGER ) return false;
416
+ jimp -> integer = jimp -> number .value .i ;
417
+ return true;
418
+ }
419
+
373
420
static bool jimp__get_and_expect_token (Jimp * jimp , Jimp_Token token )
374
421
{
375
422
if (!jimp__get_token (jimp )) return false;
0 commit comments