-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
970 lines (768 loc) · 24.5 KB
/
Copy pathparser.c
File metadata and controls
970 lines (768 loc) · 24.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
#include "parser.h"
#include <assert.h>
#include <complex.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifdef MP_PREC
#include <mpfr.h>
#include <mpc.h>
#endif
/* Minimum/maximum possible complex values */
const complex CMPLX_MIN = -(DBL_MAX) - DBL_MAX * I;
const complex CMPLX_MAX = DBL_MAX + DBL_MAX * I;
/* Minimum/maximum possible long double complex values */
const long double complex LCMPLX_MIN = -(LDBL_MAX) - LDBL_MAX * I;
const long double complex LCMPLX_MAX = LDBL_MAX + LDBL_MAX * I;
/* Symbol to denote the imaginary unit (case-insensitive) */
static const char IMAGINARY_UNIT = 'i';
static int parseMemoryUnit(char *str, char **endptr);
static int parseSign(char *c, char **endptr);
static ComplexPt parseImaginaryUnit(char *c, char **endptr);
#ifdef MP_PREC
static mpfr_rnd_t getReMPFRRound(mpc_rnd_t rnd);
static mpfr_rnd_t getImMPFRRound(mpc_rnd_t rnd);
#endif
/* Convert string to unsigned long and handle errors */
ParseErr stringToULong(unsigned long *x, char *nptr, unsigned long min, unsigned long max, char **endptr, int base)
{
char sign;
*endptr = nptr;
if ((base < 2 && base != 0) || base > 36)
return PARSE_EBASE;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
sign = **endptr;
nptr = *endptr;
errno = 0;
*x = strtoul(nptr, endptr, base);
/* Conversion check */
if (*endptr == nptr || errno == EINVAL)
return PARSE_EERR;
/* Range checks */
if (errno == ERANGE)
return PARSE_ERANGE;
else if (*x < min)
return PARSE_EMIN;
else if (*x > max)
return PARSE_EMAX;
else if (sign == '-' && *x != 0)
return PARSE_EMIN;
/* If more characters in string */
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/* Convert string to uintmax_t and handle errors */
ParseErr stringToUIntMax(uintmax_t *x, char *nptr, uintmax_t min, uintmax_t max, char **endptr, int base)
{
char sign;
*endptr = nptr;
if ((base < 2 && base != 0) || base > 36)
return PARSE_EBASE;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
sign = **endptr;
nptr = *endptr;
errno = 0;
*x = strtoumax(nptr, endptr, base);
/* Conversion check */
if (*endptr == nptr || errno == EINVAL)
return PARSE_EERR;
/* Range checks */
if (errno == ERANGE)
return PARSE_ERANGE;
else if (*x < min)
return PARSE_EMIN;
else if (*x > max)
return PARSE_EMAX;
else if (sign == '-' && *x != 0)
return PARSE_EMIN;
/* If more characters in string */
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/* Convert string to double and handle errors */
ParseErr stringToDouble(double *x, char *nptr, double min, double max, char **endptr)
{
errno = 0;
*x = strtod(nptr, endptr);
/* Conversion check */
if (*endptr == nptr)
return PARSE_EERR;
/* Range checks */
if (errno == ERANGE)
return PARSE_ERANGE;
else if (*x < min)
return PARSE_EMIN;
else if (*x > max)
return PARSE_EMAX;
/* If more characters in string */
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/* Convert string to long double and handle errors */
ParseErr stringToDoubleL(long double *x, char *nptr, long double min, long double max, char **endptr)
{
errno = 0;
*x = strtold(nptr, endptr);
/* Conversion check */
if (*endptr == nptr)
return PARSE_EERR;
/* Range checks */
if (errno == ERANGE)
return PARSE_ERANGE;
else if (*x < min)
return PARSE_EMIN;
else if (*x > max)
return PARSE_EMAX;
/* If more characters in string */
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a string as an imaginary or real double
*
* Where:
* - The format is that of a `double` type - meaning a decimal, additional
* exponent part, and hexadecimal sequence are all valid inputs
* - Whitespace will be stripped
* - The operator can be '+' or '-'
* - It can be preceded by an optional '+' or '-' sign
* - An imaginary number must be followed by the imaginary unit
*/
ParseErr stringToComplexPart(complex *z, char *nptr, complex min, complex max, char **endptr, ComplexPt *type)
{
double x;
int sign;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
/*
* Manually parsing the sign enables detection of a complex unit lacking in
* a coefficient but having a '+'/'-' sign
*/
sign = parseSign(*endptr, endptr);
if (!sign)
sign = 1;
/*
* Because the sign has been manually parsed, error on a second sign, which
* strtod() will not detect
*/
if (parseSign(*endptr, endptr))
return PARSE_EFORM;
parseError = stringToDouble(&x, *endptr, -(DBL_MAX), DBL_MAX, endptr);
if (parseError == PARSE_EERR)
{
if (toupper(**endptr) != toupper(IMAGINARY_UNIT))
return PARSE_EFORM;
/* Failed conversion must be an imaginary unit without coefficient */
x = 1.0;
}
else if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
return parseError;
}
x *= sign;
*type = parseImaginaryUnit(*endptr, endptr);
switch(*type)
{
case COMPLEX_REAL:
if (x < creal(min))
return PARSE_EMIN;
else if (x > creal(max))
return PARSE_EMAX;
*z = x + cimag(*z) * I;
break;
case COMPLEX_IMAGINARY:
if (x < cimag(min))
return PARSE_EMIN;
else if (x > cimag(max))
return PARSE_EMAX;
*z = creal(*z) + x * I;
break;
default:
return PARSE_EERR;
}
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a string as an imaginary or real long double
*
* Where:
* - The format is that of a `double` type - meaning a decimal, additional
* exponent part, and hexadecimal sequence are all valid inputs
* - Whitespace will be stripped
* - The operator can be '+' or '-'
* - It can be preceded by an optional '+' or '-' sign
* - An imaginary number must be followed by the imaginary unit
*/
ParseErr stringToComplexPartL(long double complex *z, char *nptr, long double complex min, long double complex max,
char **endptr, ComplexPt *type)
{
long double x;
int sign;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
/*
* Manually parsing the sign enables detection of a complex unit lacking in
* a coefficient but having a '+'/'-' sign
*/
sign = parseSign(*endptr, endptr);
if (!sign)
sign = 1;
/*
* Because the sign has been manually parsed, error on a second sign, which
* strtod() will not detect
*/
if (parseSign(*endptr, endptr))
return PARSE_EFORM;
parseError = stringToDoubleL(&x, *endptr, -(LDBL_MAX), LDBL_MAX, endptr);
if (parseError == PARSE_EERR)
{
if (toupper(**endptr) != toupper(IMAGINARY_UNIT))
return PARSE_EFORM;
/* Failed conversion must be an imaginary unit without coefficient */
x = 1.0L;
}
else if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
return parseError;
}
x *= sign;
*type = parseImaginaryUnit(*endptr, endptr);
parseError = (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
switch(*type)
{
case COMPLEX_REAL:
if (x < creall(min) || x > creall(max))
return PARSE_ERANGE;
*z = x + cimagl(*z) * I;
return parseError;
case COMPLEX_IMAGINARY:
if (x < cimagl(min) || x > cimagl(max))
return PARSE_ERANGE;
*z = creall(*z) + x * I;
return parseError;
default:
return PARSE_EERR;
}
}
/*
* Parse a complex number string into a complex variable
*
* Input must be of the form:
* "a + bi" or
* "bi + a"
*
* Where each part, `a` and `bi`, is parsed according to stringToImaginary():
* - The operator can be '+' or '-'
* - `a` and `bi` can be preceded by an optional '+' or '-' sign (independant
* of the expression's operator)
* - There cannot be multiple real or imaginary parts (e.g. "a + b + ci" is
* invalid)
* - Either parts can be omitted - the missing part will be interpreted as 0.0
*/
ParseErr stringToComplex(complex *z, char *nptr, complex min, complex max, char **endptr)
{
ComplexPt firstType, secondType;
char *partEndptr;
int operator;
complex secondZPart;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
*z = 0.0 + 0.0 * I;
/* Get first operand in complex number */
parseError = stringToComplexPart(z, *endptr, min, max, endptr, &firstType);
if (parseError == PARSE_SUCCESS)
return PARSE_SUCCESS;
else if (parseError != PARSE_EEND)
return parseError;
/*
* Record the end of the first part. Any future parse errors should set
* *endptr back to this and return PARSE_EEND, hence telling the user only
* the first part was parsed
*/
partEndptr = *endptr;
/* Get operator between the two parts */
operator = parseSign(*endptr, endptr);
if (!operator)
{
*endptr = partEndptr;
return PARSE_EEND;
}
/* Get second operand in complex number */
parseError = stringToComplexPart(&secondZPart, *endptr, min, max, endptr, &secondType);
if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
*endptr = partEndptr;
return PARSE_EEND;
}
if (firstType == secondType)
{
*endptr = partEndptr;
return PARSE_EEND;
}
/* Set correct part of *z, dependent on the first parsed part's type */
switch (secondType)
{
case COMPLEX_REAL:
*z = operator * creal(secondZPart) + cimag(*z) * I;
break;
case COMPLEX_IMAGINARY:
*z = creal(*z) + operator * cimag(secondZPart) * I;
break;
default:
*endptr = partEndptr;
return PARSE_EEND;
}
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a complex number string into a long double complex variable
*
* Input must be of the form:
* "a + bi" or
* "bi + a"
*
* Where each part, `a` and `bi`, is parsed according to stringToImaginary():
* - The operator can be '+' or '-'
* - `a` and `bi` can be preceded by an optional '+' or '-' sign (independant
* of the expression's operator)
* - There cannot be multiple real or imaginary parts (e.g. "a + b + ci" is
* invalid)
* - Either parts can be omitted - the missing part will be interpreted as 0.0
*/
ParseErr stringToComplexL(long double complex *z, char *nptr, long double complex min, long double complex max,
char **endptr)
{
ComplexPt firstType, secondType;
char *partEndptr;
int operator;
long double complex secondZPart;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
*z = 0.0L + 0.0L * I;
/* Get first operand in complex number */
parseError = stringToComplexPartL(z, *endptr, min, max, endptr, &firstType);
if (parseError == PARSE_SUCCESS)
return PARSE_SUCCESS;
else if (parseError != PARSE_EEND)
return parseError;
/*
* Record the end of the first part. Any future parse errors should set
* *endptr back to this and return PARSE_EEND, hence telling the user only
* the first part was parsed
*/
partEndptr = *endptr;
/* Get operator between the two parts */
operator = parseSign(*endptr, endptr);
if (!operator)
{
*endptr = partEndptr;
return PARSE_EEND;
}
/* Get second operand in complex number */
parseError = stringToComplexPartL(&secondZPart, *endptr, min, max, endptr, &secondType);
if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
*endptr = partEndptr;
return PARSE_EEND;
}
if (firstType == secondType)
{
*endptr = partEndptr;
return PARSE_EEND;
}
/* Set correct part of *z, dependent on the first parsed part's type */
switch (secondType)
{
case COMPLEX_REAL:
*z = operator * creall(secondZPart) + cimagl(*z) * I;
break;
case COMPLEX_IMAGINARY:
*z = creall(*z) + operator * cimagl(secondZPart) * I;
break;
default:
*endptr = partEndptr;
return PARSE_EEND;
}
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a positive double with optional memory unit suffix (if omitted,
* magnitude will be that of the magnitude argument) into a size_t value
*/
ParseErr stringToMemory(size_t *bytes, char *nptr, size_t min, size_t max, char **endptr, int magnitude)
{
double x;
int unitPrefix;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
parseError = stringToDouble(&x, *endptr, 0.0, DBL_MAX, endptr);
if (parseError == PARSE_SUCCESS)
{
unitPrefix = magnitude;
}
else if (parseError == PARSE_EEND)
{
nptr = *endptr;
unitPrefix = parseMemoryUnit(nptr, endptr);
if (unitPrefix < 0)
{
*endptr = nptr;
unitPrefix = magnitude;
}
}
else
{
return parseError;
}
x *= pow(10.0, unitPrefix);
if (x < 0.0 || x > SIZE_MAX)
return PARSE_ERANGE;
*bytes = (size_t) x;
if (*bytes < min)
return PARSE_EMIN;
else if (*bytes > max)
return PARSE_EMAX;
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
#ifdef MP_PREC
/* Convert string to MPFR floating-point and handle errors */
ParseErr stringToMPFR(mpfr_t x, char *nptr, mpfr_t min, mpfr_t max, char **endptr, int base, mpfr_rnd_t rnd)
{
mpfr_flags_t mpfrErr;
if ((base < 2 && base != 0) || base > 62)
return PARSE_EBASE;
mpfr_clear_flags();
mpfr_strtofr(x, nptr, endptr, base, rnd);
/* Inexactness is not considered an error */
mpfr_clear_inexflag();
mpfrErr = mpfr_flags_save();
if (mpfrErr || *endptr == nptr)
{
if (mpfrErr & MPFR_FLAGS_UNDERFLOW
|| mpfrErr & MPFR_FLAGS_OVERFLOW
|| mpfrErr & MPFR_FLAGS_ERANGE)
{
return PARSE_ERANGE;
}
return PARSE_EERR;
}
/* If user supplied minimum and/or maximum */
if (min && mpfr_cmp(x, min) < 0)
return PARSE_EMIN;
if (max && mpfr_cmp(x, max) > 0)
return PARSE_EMAX;
/* If more characters in string */
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a string as an imaginary or real MPFR floating-point
*
* Where:
* - The format is that of an `mpc_t` type - meaning a decimal, additional
* exponent part, and hexadecimal sequence are all valid inputs
* - Whitespace will be stripped
* - The operator can be '+' or '-'
* - It can be preceded by an optional '+' or '-' sign
* - An imaginary number must be followed by the imaginary unit
*/
ParseErr stringToComplexPartMPC(mpc_t z, char *nptr, mpc_t min, mpc_t max, char **endptr,
int base, mpfr_prec_t prec, mpc_rnd_t rnd, ComplexPt *type)
{
mpfr_t x;
int sign;
ParseErr parseError;
char *tmpptr;
mpfr_rnd_t mpfrRnd;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
/*
* Manually parsing the sign enables detection of a complex unit lacking in
* a coefficient but having a '+'/'-' sign
*/
sign = parseSign(*endptr, endptr);
if (!sign)
sign = 1;
/*
* Because the sign has been manually parsed, error on a second sign, which
* gmp_sscanf() will not detect
*/
if (parseSign(*endptr, endptr))
return PARSE_EFORM;
mpfr_init2(x, prec);
/* Do a dummy read of the number to apply correct rounding mode */
tmpptr = *endptr;
stringToMPFR(x, *endptr, NULL, NULL, endptr, base, MPFR_RNDN);
if (parseImaginaryUnit(*endptr, endptr) == COMPLEX_IMAGINARY)
mpfrRnd = getImMPFRRound(rnd);
else
mpfrRnd = getReMPFRRound(rnd);
if (mpfrRnd == MPFR_RNDA)
{
mpfr_clear(x);
return PARSE_EERR;
}
*endptr = tmpptr;
parseError = stringToMPFR(x, *endptr, NULL, NULL, endptr, base, mpfrRnd);
if (parseError == PARSE_EERR || parseError == PARSE_EFORM)
{
if (toupper(**endptr) != toupper(IMAGINARY_UNIT))
{
mpfr_clear(x);
return PARSE_EFORM;
}
/* Failed conversion must be an imaginary unit without coefficient */
mpfr_set_d(x, 1.0, mpfrRnd);
}
else if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
mpfr_clear(x);
return parseError;
}
if (sign == -1)
mpfr_neg(x, x, mpfrRnd);
*type = parseImaginaryUnit(*endptr, endptr);
switch(*type)
{
case COMPLEX_REAL:
if (min && mpfr_cmp(x, mpc_realref(min)) < 0)
{
mpfr_clear(x);
return PARSE_EMIN;
}
else if (max && mpfr_cmp(x, mpc_realref(max)) > 0)
{
mpfr_clear(x);
return PARSE_EMAX;
}
mpc_set_fr_fr(z, x, mpc_imagref(z), rnd);
break;
case COMPLEX_IMAGINARY:
if (min && mpfr_cmp(x, mpc_imagref(min)) < 0)
{
mpfr_clear(x);
return PARSE_EMIN;
}
else if (max && mpfr_cmp(x, mpc_imagref(max)) > 0)
{
mpfr_clear(x);
return PARSE_EMAX;
}
mpc_set_fr_fr(z, mpc_realref(z), x, rnd);
break;
default:
mpfr_clear(x);
return PARSE_EERR;
}
mpfr_clear(x);
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
/*
* Parse a complex number string into an MPC complex variable
*
* Input must be of the form:
* "a + bi" or
* "bi + a"
*
* Where each part, `a` and `bi`, is parsed according to stringToImaginary():
* - The operator can be '+' or '-'
* - `a` and `bi` can be preceded by an optional '+' or '-' sign (independant
* of the expression's operator)
* - There cannot be multiple real or imaginary parts (e.g. "a + b + ci" is
* invalid)
* - Either parts can be omitted - the missing part will be interpreted as 0.0
*/
ParseErr stringToComplexMPC(mpc_t z, char *nptr, mpc_t min, mpc_t max, char **endptr,
int base, mpfr_prec_t prec, mpc_rnd_t rnd)
{
ComplexPt firstType, secondType;
char *partEndptr;
int operator;
mpc_t secondZPart;
ParseErr parseError;
*endptr = nptr;
/* Get pointer to start of number */
while (isspace(**endptr))
++(*endptr);
mpc_set_d_d(z, 0.0, 0.0, rnd);
/* Get first operand in complex number */
parseError = stringToComplexPartMPC(z, *endptr, min, max, endptr, base, prec, rnd, &firstType);
if (parseError == PARSE_SUCCESS)
return PARSE_SUCCESS;
else if (parseError != PARSE_EEND)
return parseError;
/*
* Record the end of the first part. Any future parse errors should set
* *endptr back to this and return PARSE_EEND, hence telling the user only
* the first part was parsed
*/
partEndptr = *endptr;
/* Get operator between the two parts */
operator = parseSign(*endptr, endptr);
if (!operator)
{
*endptr = partEndptr;
return PARSE_EEND;
}
mpc_init2(secondZPart, prec);
/* Get second operand in complex number */
parseError = stringToComplexPartMPC(secondZPart, *endptr, min, max, endptr, base, prec, rnd, &secondType);
if (parseError != PARSE_SUCCESS && parseError != PARSE_EEND)
{
*endptr = partEndptr;
mpc_clear(secondZPart);
return PARSE_EEND;
}
if (firstType == secondType)
{
*endptr = partEndptr;
mpc_clear(secondZPart);
return PARSE_EEND;
}
if (operator == -1)
mpc_neg(secondZPart, secondZPart, rnd);
/* Set correct part of z, dependent on the first parsed part's type */
switch (secondType)
{
case COMPLEX_REAL:
mpc_set_fr_fr(z, mpc_realref(secondZPart), mpc_imagref(z), rnd);
break;
case COMPLEX_IMAGINARY:
mpc_set_fr_fr(z, mpc_realref(z), mpc_imagref(secondZPart), rnd);
break;
default:
*endptr = partEndptr;
mpc_clear(secondZPart);
return PARSE_EEND;
}
mpc_clear(secondZPart);
return (**endptr == '\0') ? PARSE_SUCCESS : PARSE_EEND;
}
#endif
/*
* Strip src of non-graphical characters then copy a maximum of n characters
* (including the null terminator) into dest and return the length of dest
*/
size_t strncpyGraph(char *dest, const char *src, size_t n)
{
size_t j = 0;
for (size_t i = 0; src[i] != '\0' && j < n - 1; ++i)
{
if (isgraph(src[i]))
dest[j++] = src[i];
}
dest[j] = '\0';
/* Length of dest */
return j;
}
static int parseMemoryUnit(char *str, char **endptr)
{
const char BYTE_UNIT = 'B';
const char BYTE_PREFIXES[] = {'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
unsigned int magnitude = 0;
*endptr = str;
/* Get pointer to start of unit */
while (isspace(**endptr))
++(*endptr);
for (unsigned int i = 0; i < sizeof(BYTE_PREFIXES) / sizeof(char); ++i)
{
if (toupper(**endptr) == toupper(BYTE_PREFIXES[i]))
{
magnitude = (i + 1) * 3;
if (magnitude > INT_MAX)
return -1;
++(*endptr);
break;
}
}
if (toupper(**endptr) != toupper(BYTE_UNIT))
return -1;
++(*endptr);
/* Cast is safe due to previous check */
return (int) magnitude;
}
/* Parse the sign of a number */
static int parseSign(char *c, char **endptr)
{
*endptr = c;
/* Get pointer to sign */
while (isspace(**endptr))
++(*endptr);
switch (**endptr)
{
case '+':
++(*endptr);
return 1;
case '-':
++(*endptr);
return -1;
default:
return 0;
}
}
/* Parse the imaginary unit, or lack thereof */
static ComplexPt parseImaginaryUnit(char *c, char **endptr)
{
*endptr = c;
/* Get pointer to start of imaginary unit */
while (isspace(**endptr))
++(*endptr);
if (toupper(**endptr) != toupper(IMAGINARY_UNIT))
return COMPLEX_REAL;
++(*endptr);
return COMPLEX_IMAGINARY;
}
#ifdef MP_PREC
/* Get real rounding mode from MPC mode */
static mpfr_rnd_t getReMPFRRound(mpc_rnd_t rnd)
{
if (rnd == MPC_RNDNN || rnd == MPC_RNDNZ || rnd == MPC_RNDNU || rnd == MPC_RNDND)
return MPFR_RNDN;
else if (rnd == MPC_RNDZN || rnd == MPC_RNDZZ || rnd == MPC_RNDZU || rnd == MPC_RNDZD)
return MPFR_RNDZ;
else if (rnd == MPC_RNDUN || rnd == MPC_RNDUZ || rnd == MPC_RNDUU || rnd == MPC_RNDUD)
return MPFR_RNDU;
else if (rnd == MPC_RNDDN || rnd == MPC_RNDDZ || rnd == MPC_RNDDU || rnd == MPC_RNDDD)
return MPFR_RNDD;
/* Return unused (in MPC) MPFR rounding mode on error */
return MPFR_RNDA;
}
/* Get imaginary rounding mode from MPC mode */
static mpfr_rnd_t getImMPFRRound(mpc_rnd_t rnd)
{
if (rnd == MPC_RNDNN || rnd == MPC_RNDZN || rnd == MPC_RNDUN || rnd == MPC_RNDDN)
return MPFR_RNDN;
else if (rnd == MPC_RNDNZ || rnd == MPC_RNDZZ || rnd == MPC_RNDUZ || rnd == MPC_RNDDZ)
return MPFR_RNDZ;
else if (rnd == MPC_RNDNU || rnd == MPC_RNDZU || rnd == MPC_RNDUU || rnd == MPC_RNDDU)
return MPFR_RNDU;
else if (rnd == MPC_RNDND || rnd == MPC_RNDZD || rnd == MPC_RNDUD || rnd == MPC_RNDDD)
return MPFR_RNDD;
/* Return unused (in MPC) MPFR rounding mode on error */
return MPFR_RNDA;
}
#endif