-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheml.c
1282 lines (1103 loc) · 38.1 KB
/
eml.c
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "eml.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// #define EML_PARSER_VERSION "0.0.0"
// #define DEBUG
// The maximum length a user-input string may be (excluding sentinel)
#define MAX_NAME_LENGTH 128
#define MAX_VERSION_STRING_LENGTH 12
#define MAX_WEIGHT_UNIT_STRING_LENGTH 3
#define MAX_FORMATTED_EML_STRING_LENGTH 12
#define true 1
#define false 0
#define right 1
#define left 0
/*
* Parser Parameters:
* version - the version in the eml header
* weightUnit - the weight abbreviation in the eml header
*/
static char version[MAX_VERSION_STRING_LENGTH + 1];
static char weightUnit[MAX_WEIGHT_UNIT_STRING_LENGTH + 1];
/*
* Global Parser Vars:
* emlString - The eml to be parsed
* emlstringlen - The length of the emlString (excluding sentinel)
* current_position - The index of emlString the parser is currently on
*/
static char *emlString;
static int emlstringlen;
static int current_postition;
static int parse_header(eml_result *result);
static void validate_header_t(eml_header_t *h);
static int parse_string(char **result);
static int parse_header_t(eml_header_t **tht);
static int parse_super_t(eml_super_t **tsupt);
static int parse_single_t(eml_single_t **tst);
static int applying_reps_type(eml_reps *r, uint32_t t);
static int flush(eml_single_t *tst, uint32_t *vcount, eml_kind_flag kind, eml_modifier_flag mod, eml_number *buf, uint32_t *dcount);
static void move_to_asymmetric(eml_single_t *tst, bool side);
static int upgrade_to_asymmetric(eml_single_t *tst);
static int upgrade_to_standard_varied(eml_single_t *tst);
static int upgrade_to_standard(eml_single_t *tst);
static void format_eml_number(eml_number *e, char *f);
static void print_standard_k(eml_standard_k *k);
static void print_standard_varied_k(eml_standard_varied_k *k);
static void print_single_t(eml_single_t *s);
static void print_super_t(eml_super_t *s);
static void print_emlobj(eml_obj *e);
static void free_single_t(eml_single_t *s);
static void free_super_t(eml_super_t *s);
static void free_emlobj(eml_obj *e);
/*
* parse: Entry point for parsing eml. Starts at '{', ends at (emlstringlen - 1)
*/
int parse(char *eml_string, eml_result **result) {
emlString = eml_string;
emlstringlen = strlen(eml_string);
current_postition = 0;
version[0] = 0;
weightUnit[0] = 0;
*result = malloc(sizeof(eml_result));
if (*result == NULL) {
return allocation_error;
}
(*result)->header = NULL;
(*result)->objs = NULL;
eml_obj *obj_tail = NULL;
#ifdef DEBUG
printf("EML String: %s, length: %i\n", eml_string, emlstringlen);
#endif
eml_super_t *tsupt = NULL;
eml_single_t *tst = NULL;
int error = 0;
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current) {
case (int)'{': // Give control to parse_header()
parse_header(*result);
if (version[0] == '\0') {
error = missing_version;
goto bail;
}
if (weightUnit[0] == '\0') {
error = missing_weight_unit;
goto bail;
}
#ifdef DEBUG
printf("parsed version: %s, parsed weight: %s\n", version, weightUnit);
printf("-------------------------\n");
#endif
break;
case (int)'s': // Give control to parse_super_t()
if ((error = parse_super_t(&tsupt))) {
goto bail;
}
eml_obj *temp_super = malloc(sizeof(eml_obj));
if (temp_super == NULL) {
error = allocation_error;
goto bail;
}
temp_super->type = super;
temp_super->data = tsupt;
temp_super->next = NULL;
if ((*result)->objs == NULL) {
(*result)->objs = temp_super;
} else {
obj_tail->next = temp_super;
}
obj_tail = temp_super;
break;
case (int)'c': // Give control to parse_super_t()
if ((error = parse_super_t(&tsupt))) {
goto bail;
}
eml_obj *temp_circuit = malloc(sizeof(eml_obj));
if (temp_circuit == NULL) {
error = allocation_error;
goto bail;
}
temp_circuit->type = circuit;
temp_circuit->data = tsupt;
temp_circuit->next = NULL;
if ((*result)->objs == NULL) {
(*result)->objs = temp_circuit;
} else {
obj_tail->next = temp_circuit;
}
obj_tail = temp_circuit;
break;
case (int)'\"': // Give control to parse_single_t()
if ((error = parse_single_t(&tst))) {
goto bail;
}
eml_obj *temp_single = malloc(sizeof(eml_obj));
if (temp_single == NULL) {
error = allocation_error;
goto bail;
}
temp_single->type = single;
temp_single->data = tst;
temp_single->next = NULL;
if ((*result)->objs == NULL) {
(*result)->objs = temp_single;
} else {
obj_tail->next = temp_single;
}
obj_tail = temp_single;
break;
case (int)';':
++current_postition;
break;
default:
error = unexpected_error;
goto bail;
}
}
return no_error;
bail:
if (tst != NULL) {
free_single_t(tst);
}
if (tsupt != NULL) {
free_super_t(tsupt);
}
free_result(*result);
return error;
}
/*
* parse_header: Parses header section or exits. Starts on "{" of header, ends on char succeeding "}"
*/
static int parse_header(eml_result *result) {
eml_header_t *tht = NULL;
int error = no_error;
if (emlString[current_postition++] != (int)'{') {
error = missing_header_start_char;
return error;
}
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current){
case (int)'}': // Release control & inc
++current_postition;
return no_error;
case (int)',':
++current_postition;
break;
case (int)'\"':
if ((error = parse_header_t(&tht))) {
return error;
}
validate_header_t(tht);
tht->next = result->header;
result->header = tht;
break;
default:
return unexpected_error;
}
}
error = unexpected_error;
return error;
}
/*
* parse_header_t: Returns an eml_header_t or exits. Starts on '"', ends on ',' or '}'.
*/
static int parse_header_t(eml_header_t **tht) {
*tht = malloc(sizeof(eml_header_t));
if (*tht == NULL) {
return allocation_error;
}
bool pv = false; // Toggle between parameter & value
int error = no_error;
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current) {
case (int)'}': // Release control
return no_error;
case (int)',': // Release control
return no_error;
case (int)':':
pv = true;
++current_postition;
break;
case (int)'\"':
if (pv == false) {
if ((error = parse_string(&(*tht)->parameter))) {
goto bail;
}
}
else {
if ((error = parse_string(&(*tht)->value))) {
goto bail;
}
}
break;
default:
error = unexpected_error;
goto bail;
}
}
error = unexpected_error;
bail:
if (*tht != NULL) {
if ((*tht)->parameter != NULL) {
free((*tht)->parameter);
}
if ((*tht)->value != NULL) {
free((*tht)->value);
}
free(*tht);
}
return error;
}
/*
* parse_super_t: Returns an eml_super_t or exits. Starts on 's', ends succeeding ')'.
*/
static int parse_super_t(eml_super_t **tsupt) {
*tsupt = malloc(sizeof(eml_super_t));
if (tsupt == NULL) {
return allocation_error;
}
eml_single_t *tst = NULL;
eml_super_member_t *set_tail = NULL;
int error = no_error;
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current) {
case (int)'(':
++current_postition;
break;
case (int)'\"':
if ((error = parse_single_t(&tst))) {
return error;
}
eml_super_member_t *temp = malloc(sizeof(eml_super_member_t));
temp->single = tst;
temp->next = NULL;
if ((*tsupt)->sets == NULL) {
(*tsupt)->sets = temp;
} else {
set_tail->next = temp;
}
(*tsupt)->count++;
set_tail = temp;
break;
case (int)')':
++current_postition;
return no_error;
default: // skip {'u', 'p', 'e', 'r'} and {'i', 'r', 'c', 'u', 'i', 't'}
++current_postition;
break;
}
}
error = unexpected_error;
return error;
}
/*
* parse_single_t: Returns an eml_single_t or exits. Starts on '"', ends succeeding ';'
*/
static int parse_single_t(eml_single_t **tst) {
*tst = malloc(sizeof(eml_single_t));
if (tst == NULL) {
return allocation_error;
}
// Initialize eml_single_t
(*tst)->name = NULL;
(*tst)->no_work = NULL;
(*tst)->standard_work = NULL;
(*tst)->standard_varied_work = NULL;
(*tst)->asymmetric_work = NULL;
int error = no_error;
// #define BAIL(e) { error = e; goto bail;}
if ((error = parse_string(&(*tst)->name))) {
goto bail;
}
eml_kind_flag kind = none;
eml_modifier_flag modifier = no_mod;
eml_number buffer_int = 0; // Rolling eml_number
uint32_t dcount = 0; // If >0, writing to (dcount * 10)'ths place, >2 error
uint32_t vcount = 0; // Index of standard_varied_k.vReps[]
uint32_t temp; // Used for building eml_number in `default`
if (emlString[current_postition++] != (int)':') {
error = name_work_separator_error;
goto bail;
}
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current) {
case (int)'\"':
++current_postition;
break;
case (int)':':
// Upgrade to asymetric_k
// Write value/modifier. If kind == standard_varied_work, write as macro.
if ((error = flush(*tst, NULL, kind, modifier, &buffer_int, &dcount))) {
goto bail;
}
modifier = no_mod;
kind = none;
// Upgrade existing eml_single_t to asymmetric
if ((error = upgrade_to_asymmetric(*tst))) {
goto bail;
}
++current_postition;
break;
case (int)'x':
// Allocate standard_work & set sets.
if ((error = upgrade_to_standard(*tst))) {
goto bail;
}
(*tst)->standard_work->sets = buffer_int;
buffer_int = 0;
kind = standard;
++current_postition;
break;
case (int)'(':
// Allocate standard_varied_work & dealloc/transition standard_work
if ((error = upgrade_to_standard_varied(*tst))) {
goto bail;
}
vcount = 0;
kind = standard_varied;
++current_postition;
break;
case (int)',':
if (vcount > (*tst)->standard_varied_work->sets) {
error = extra_variable_reps_error;
goto bail;
}
// Write reps/(internal)modifiers
if ((error = flush(*tst, &vcount, kind, modifier, &buffer_int, &dcount))) {
goto bail;
}
vcount++;
modifier = no_mod;
++current_postition;
break;
case (int)')':
if (vcount > (*tst)->standard_varied_work->sets) {
error = extra_variable_reps_error;
goto bail;
}
// Write reps/(internal)modifiers
if ((error = flush(*tst, &vcount, kind, modifier, &buffer_int, &dcount))) {
goto bail;
}
vcount++;
modifier = no_mod;
if (vcount < (*tst)->standard_varied_work->sets) {
error = missing_variable_reps_error;
goto bail;
}
++current_postition;
break;
case (int)'F':
switch (kind) {
case none:
error = none_work_to_failure_error;
goto bail;
case standard:
if ((error = applying_reps_type(&(*tst)->standard_work->reps, unmodifiedFailure))) {
goto bail;
}
break;
case standard_varied:
// Apply 'toFailure' to internal Reps
if (vcount < (*tst)->standard_varied_work->sets) {
if ((error = applying_reps_type(&(*tst)->standard_varied_work->vReps[vcount], unmodifiedFailure))) {
goto bail;
}
}
else {
error = to_failure_used_as_macro_error;
goto bail;
}
break;
}
++current_postition;
break;
case (int)'T':
switch (kind) {
case none:
error = modifier_on_none_work_error;
goto bail;
case standard:
if ((error = applying_reps_type(&(*tst)->standard_work->reps, unmodifiedTime))) {
goto bail;
}
break;
case standard_varied:
// Apply 'isTime' to internal Reps
if (vcount < (*tst)->standard_varied_work->sets) {
if ((error = applying_reps_type(&(*tst)->standard_varied_work->vReps[vcount], unmodifiedTime))) {
goto bail;
}
}
else {
error = time_macro_error;
goto bail;
}
break;
}
++current_postition;
break;
case (int)'@':
switch (kind) {
case none:
error = modifier_on_none_work_error;
goto bail;
case standard:
(*tst)->standard_work->reps.value = buffer_int;
break;
case standard_varied:
// Apply weight modifier to internal Reps (EX: 3x(5@120,...,...))
if (vcount < (*tst)->standard_varied_work->sets) {
(*tst)->standard_varied_work->vReps[vcount].value = buffer_int;
}
break;
}
buffer_int = 0;
dcount = 0;
modifier = weight_mod;
++current_postition;
break;
case (int)'%':
switch (kind) {
case none:
error = modifier_on_none_work_error;
goto bail;
case standard:
(*tst)->standard_work->reps.value = buffer_int;
break;
case standard_varied:
// Apply weight modifier to internal Reps (EX: 3x(5%120,...,...))
if (vcount < (*tst)->standard_varied_work->sets) {
(*tst)->standard_varied_work->vReps[vcount].value = buffer_int;
}
break;
}
buffer_int = 0;
dcount = 0;
modifier = rpe_mod;
++current_postition;
break;
case (int)'.':
if (kind == none) {
error = fractional_sets_error;
goto bail;
}
if (dcount) {
error = multiple_radix_points_error;
goto bail;
}
if (modifier == no_mod) {
error = fractional_none_modifier_value_error;
goto bail;
}
// Set H bit, potential overflow handled in `default`
buffer_int = buffer_int * 100U | eml_number_H;
++dcount;
++current_postition;
break;
case (int)';':
// Write value/modifier. If kind == standard_varied_work, write as macro.
if ((error = flush(*tst, NULL, kind, modifier, &buffer_int, &dcount))) {
goto bail;
}
if ((*tst)->asymmetric_work != NULL) {
move_to_asymmetric(*tst, right);
}
++current_postition;
return no_error; // Give control back
default:
switch (dcount) {
case 0: // Before radix
temp = buffer_int * 10U + (unsigned int)current - '0';
if (temp > 21474836U) {
error = integral_overflow_error;
goto bail;
}
buffer_int = temp;
break;
case 1: // 10ths place
temp = buffer_int + ((unsigned int)current - '0') * 10U;
if ((temp & eml_number_mask) > 2147483647U) {
error = fp_overflow_error;
goto bail;
}
buffer_int = temp;
dcount++;
break;
case 2: // 100ths place
temp = buffer_int + ((unsigned int)current - '0');
if ((temp & eml_number_mask) > 2147483647U) {
error = fp_overflow_error;
goto bail;
}
buffer_int = temp;
dcount++;
break;
default:
error = too_many_fp_digits;
goto bail;
}
++current_postition;
break;
}
}
error = unexpected_error;
bail:
return error;
}
/*
* validate_header_t: Checks and adds parser configuration from eml_header_t
*/
static void validate_header_t(eml_header_t *h) {
if (version[0] == '\0' && strcmp(h->parameter, "version") == 0) {
strncpy(version, h->value, MAX_VERSION_STRING_LENGTH);
version[MAX_VERSION_STRING_LENGTH] = '\0';
} else if (weightUnit[0] == '\0' && strcmp(h->parameter, "weight") == 0) {
strncpy(weightUnit, h->value, MAX_WEIGHT_UNIT_STRING_LENGTH);
weightUnit[MAX_WEIGHT_UNIT_STRING_LENGTH] = '\0';
}
}
/*
* parse_string: Returns a string (char*) or exits. Starts on '"', ends succeeding the next '"'
*/
static int parse_string(char **result) {
static char strbuf[MAX_NAME_LENGTH + 1];
uint32_t strindex = 0;
++current_postition; // skip '"'
while (current_postition < emlstringlen) {
char current = emlString[current_postition];
switch (current) {
case (int)'\"':
++current_postition;
if (strindex == 0) {
return empty_string_error;
}
*result = malloc(strindex + 1);
if (*result == NULL) {
return allocation_error;
}
for(int i = 0; i < strindex; i++) {
(*result)[i] = strbuf[i];
}
(*result)[strindex] = '\0';
return no_error;
default:
if (strindex > 127) {
free(*result);
return string_length_error;
}
strbuf[strindex++] = current;
++current_postition;
break;
}
}
return no_error;
}
/*
* applying_reps_type: Transitions REPS type to new type
*/
static int applying_reps_type(eml_reps *r, uint32_t t) {
switch (r->type) {
case unmodified:
switch (t) {
case unmodifiedFailure:
r->type = unmodifiedFailure;
break;
case unmodifiedTime:
r->type = unmodifiedTime;
break;
case weight:
r->type = weight;
break;
case rpe:
r->type = rpe;
break;
default:
return bad_reps_type_transition;
}
break;
case unmodifiedFailure:
switch (t) {
case unmodifiedTime:
r->type = unmodifiedTimeFailure;
break;
case weight:
r->type = weightFailure;
break;
case rpe:
return rpe_to_failure;
default:
return bad_reps_type_transition;
}
break;
case unmodifiedTime:
switch (t) {
case unmodifiedFailure:
r->type = unmodifiedTimeFailure;
break;
case weight:
r->type = timeWeight;
break;
case rpe:
r->type = timeRPE;
break;
default:
return bad_reps_type_transition;
}
break;
case unmodifiedTimeFailure:
switch (t) {
case weight:
r->type = timeWeightFaliure;
break;
case rpe:
return rpe_to_failure;
default:
return bad_reps_type_transition;
}
break;
default:
return bad_reps_type_transition;
}
return no_error;
}
/*
* flush: Writes buf to the appropriate field in eml_single_t. If there is none work, flush will malloc tst->no_work.
* If `vcount` is NULL, modifier will be applied as a macro. Resets `buf` and `dcount`.
*/
static int flush(eml_single_t *tst, uint32_t *vcount, eml_kind_flag kind, eml_modifier_flag mod, eml_number *buf, uint32_t *dcount) {
int error = no_error;
if (*dcount == 1) {
return missing_digit_following_radix_error;
}
switch (kind) {
case none:
switch (mod) {
case no_mod:
tst->no_work = malloc(sizeof(bool));
if (tst->no_work == NULL) {
return allocation_error;
}
break;
default:
return modifier_on_none_work_error;
}
break;
case standard:
switch (mod) {
case no_mod:
tst->standard_work->reps.value = *buf;
break;
case weight_mod:
if ((error = applying_reps_type(&tst->standard_work->reps, weight))) {
return error;
}
tst->standard_work->reps.modifier.weight = *buf;
break;
case rpe_mod:
if ((error = applying_reps_type(&tst->standard_work->reps, rpe))) {
return error;
}
tst->standard_work->reps.modifier.rpe = *buf;
break;
}
break;
case standard_varied:
if (vcount == NULL) { // MACRO MODIFIER
switch (mod) {
case no_mod:
break;
case weight_mod:
for (int i = 0; i < tst->standard_varied_work->sets; i++) {
// Only apply weight macro to reps which do not have a modifier
switch (tst->standard_varied_work->vReps[i].type) {
case unmodified:
case unmodifiedFailure:
case unmodifiedTime:
case unmodifiedTimeFailure:
if ((error = applying_reps_type(&tst->standard_varied_work->vReps[i], weight))) {
return error;
}
tst->standard_varied_work->vReps[i].modifier.weight = *buf;
break;
default:
break;
}
}
break;
case rpe_mod:
for (int i = 0; i < tst->standard_varied_work->sets; i++) {
// Only apply RPE macro to reps which do not have a modifier.
switch (tst->standard_varied_work->vReps[i].type) {
case unmodified:
case unmodifiedTime:
if ((error = applying_reps_type(&tst->standard_varied_work->vReps[i], rpe))) {
return error;
}
tst->standard_varied_work->vReps[i].modifier.rpe = *buf; // add validation
break;
case unmodifiedFailure:
case unmodifiedTimeFailure:
// Applying RPE as a macro to a failure set is an error
return rpe_to_failure;
default:
break;
}
}
break;
}
} else { // INNER-MODIFIER
switch (mod) {
case no_mod:
tst->standard_varied_work->vReps[*vcount].value = *buf;
break;
case weight_mod:
if ((error = applying_reps_type(&tst->standard_varied_work->vReps[*vcount], weight))) {
return error;
}
tst->standard_varied_work->vReps[*vcount].modifier.weight = *buf;
break;
case rpe_mod:
if ((error = applying_reps_type(&tst->standard_varied_work->vReps[*vcount], rpe))) {
return error;
}
tst->standard_varied_work->vReps[*vcount].modifier.rpe = *buf;
break;
}
}
break;
}
*buf = 0;
*dcount = 0;
return no_error;
}
/*
* move_to_asymmetric: Moves tst->(no_work | standard_work | standard_varied_work) kind to the left or right side of tst->asymmetric_work.
*/
static void move_to_asymmetric(eml_single_t *tst, bool side) {
if (tst->no_work != NULL) {
if (side) {
tst->asymmetric_work->right_none_k = tst->no_work;
} else {
tst->asymmetric_work->left_none_k = tst->no_work;
}
tst->no_work = NULL;
}
else if (tst->standard_work != NULL) {
if (side) {
tst->asymmetric_work->right_standard_k = tst->standard_work;
} else {
tst->asymmetric_work->left_standard_k = tst->standard_work;
}
tst->standard_work = NULL;
}
else if (tst->standard_varied_work != NULL) {
if (side) {
tst->asymmetric_work->right_standard_varied_k = tst->standard_varied_work;
} else {
tst->asymmetric_work->left_standard_varied_k = tst->standard_varied_work;
}
tst->standard_varied_work = NULL;
}
}
/*
* upgrade_to_asymmetric: Allocates tst->asymmetric_work & moves existing tst->(no_work | standard_work | standard_varied_work) kind to left side.
*/
static int upgrade_to_asymmetric(eml_single_t *tst) {
tst->asymmetric_work = malloc(sizeof(eml_asymmetric_k));
if (tst->asymmetric_work == NULL) {
return allocation_error;
}
tst->asymmetric_work->left_none_k = NULL;
tst->asymmetric_work->left_standard_k = NULL;
tst->asymmetric_work->left_standard_varied_k = NULL;
tst->asymmetric_work->right_none_k = NULL;
tst->asymmetric_work->right_standard_k = NULL;
tst->asymmetric_work->right_standard_varied_k = NULL;
move_to_asymmetric(tst, left);
return no_error;
}
/*
* upgrade_to_standard_varied: Allocates tst->standard_varied_work & migrates tst->standard_work.
*/
static int upgrade_to_standard_varied(eml_single_t *tst) {
tst->standard_varied_work = malloc(sizeof(eml_reps) * tst->standard_work->sets + sizeof(eml_number));
if (tst->standard_varied_work == NULL) {
return allocation_error;
}
tst->standard_varied_work->sets = tst->standard_work->sets;
// standard_varied_kind defaults
for (int i = 0; i < tst->standard_varied_work->sets; i++) {
tst->standard_varied_work->vReps[i].type = unmodified;
}
free(tst->standard_work);
tst->standard_work = NULL;
return no_error;
}
/*
* upgrade_to_standard: Allocates tst->standard_work.
*/
static int upgrade_to_standard(eml_single_t *tst) {
tst->standard_work = malloc(sizeof(eml_standard_k));
if (tst->standard_work == NULL) {
return allocation_error;
}
tst->standard_work->reps.type = unmodified;
return no_error;
}
/*
* format_eml_number: Returns a temporary formatted eml_number string which is valid until next call or exits.
*/
static void format_eml_number(eml_number *e, char *f) {
if (*e & eml_number_H) {
uint32_t masked = (*e & eml_number_mask);