-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptvalues.c
More file actions
942 lines (892 loc) · 29.3 KB
/
optvalues.c
File metadata and controls
942 lines (892 loc) · 29.3 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
#include "optparser.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#define container(pointer, ptype, field) ((ptype)((char*)(pointer) - (char*)&((ptype)0)->field))
#define cast(type, self) container(self, type, _base)
// option value user {{{
struct opt_value_user
{
struct option_value _base;
void* context;
void* default_value;
void (*default_store)(void*, void*);
char const* default_display;
char const* (*user_validator)(char const*, void*);
char const* (*user_parser)(char const*, void*, void*);
void (*context_free)(void*);
};
#define value_user_cast(self) cast(struct opt_value_user*, self)
static char const* value_user_validate(OptionValue self, char const* arg)
{
struct opt_value_user* u_self = value_user_cast(self);
if (u_self->user_validator )
return u_self->user_validator(arg, u_self->context);
return NULL;
}
static char const* value_user_parse(OptionValue self, char const* arg)
{
assert( self->pointer );
struct opt_value_user* u_self = value_user_cast(self);
if ( u_self->user_parser )
return u_self->user_parser(arg, self->pointer, u_self->context);
return NULL;
}
static void value_user_free(OptionValue self)
{
struct opt_value_user* u_self = value_user_cast(self);
if ( u_self->context_free && u_self->context )
u_self->context_free(u_self->context);
free(u_self);
}
static void value_user_store_default(OptionValue self)
{
struct opt_value_user* u_self = value_user_cast(self);
if ( self->has_default && u_self->default_store )
u_self->default_store(u_self->default_value, self->pointer);
}
static char const* value_user_display_default(OptionValue self)
{
char const* s = value_user_cast(self)->default_display;
if ( s == NULL )
return "object";
return s;
}
struct option_value_ops g_value_user_ops = {
.validate = &value_user_validate,
.parse = &value_user_parse,
.store_default = &value_user_store_default,
.display_default= &value_user_display_default,
.free = value_user_free
};
static OptValueUserBuilderInterface ubi_set_required();
static OptValueUserBuilderInterface ubi_set_context(void* context);
static OptValueUserBuilderInterface ubi_set_validator(char const* (*user_validator)(char const*, void*));
static OptValueUserBuilderInterface ubi_set_parser(char const* (*user_parser)(char const*, void*, void*));
static OptValueUserBuilderInterface ubi_set_default_value(void* value);
static OptValueUserBuilderInterface ubi_set_free(void (*on_free)(void*));
static OptValueUserBuilderInterface ubi_set_default_store(void (*store)(void*, void*));
static OptValueUserBuilderInterface ubi_set_default_display(char const*);
struct opt_value_user_builder_interface g_user_builder = {
._data = NULL,
.required = &ubi_set_required,
.validator = &ubi_set_validator,
.parser = &ubi_set_parser,
.default_value = &ubi_set_default_value,
.free = &ubi_set_free,
.context = &ubi_set_context,
.default_store = &ubi_set_default_store,
.default_display = & ubi_set_default_display,
};
OptValueUserBuilderInterface opt_arg(void* pointer)
{
struct opt_value_user* value = (struct opt_value_user*)malloc(sizeof(*value));
bzero(value, sizeof *value);
value->_base.pointer = pointer;
value->_base.ops = &g_value_user_ops;
g_user_builder._data = &value->_base;
return &g_user_builder;
}
#define this_user_value() container(g_user_builder._data, struct opt_value_user*, _base)
static OptValueUserBuilderInterface ubi_set_required()
{
g_user_builder._data->required = 1;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_validator(char const* (*user_validator)(char const*, void*))
{
this_user_value()->user_validator = user_validator;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_parser(char const* (*user_parser)(char const*, void*, void*))
{
this_user_value()->user_parser = user_parser;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_default_value(void* value)
{
this_user_value()->default_value = value;
this_user_value()->_base.has_default = 1;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_default_display(char const* s)
{
this_user_value()->default_display = s;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_free(void (*on_free)(void*))
{
this_user_value()->context_free = on_free;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_context(void* context)
{
this_user_value()->context = context;
return &g_user_builder;
}
static OptValueUserBuilderInterface ubi_set_default_store(void (*store)(void*, void*))
{
this_user_value()->default_store = store;
return &g_user_builder;
}
// }}}
// option value int{{{
struct opt_value_int
{
struct option_value _base;
int default_int;
int base;
void* context;
char const* (*int_validator)(int, void*);
void (*context_free)(void*);
};
#define value_int_cast(self) cast(struct opt_value_int*, self)
static char const* value_int_validate(OptionValue self, char const* arg)
{
char* endptr;
struct opt_value_int* i_self = value_int_cast(self);
int n = strtol(arg, &endptr, i_self->base);
if ( *endptr == '\0')
{
if( i_self->int_validator ) return i_self->int_validator(n, i_self->context);
return NULL;
}
return "not a int";
}
static char const* value_int_parse(OptionValue self, char const* arg)
{
assert( self->pointer );
int n = strtol(arg, NULL, value_int_cast(self)->base);
*(int*)(self->pointer) = n;
return NULL;
}
static void value_int_free(OptionValue self)
{
struct opt_value_int* int_self = value_int_cast(self);
if ( int_self->context_free && int_self->context)
int_self->context_free(int_self->context);
free(int_self);
}
static void value_int_store_default(OptionValue self)
{
assert(self->pointer);
*(int*)self->pointer = value_int_cast(self)->default_int;
}
char const* value_int_display_default(OptionValue self)
{
static char str[32];
snprintf(str, sizeof str, "%d",value_int_cast(self)->default_int);
return str;
}
struct option_value_ops g_value_int_ops = {
.validate = &value_int_validate,
.parse = &value_int_parse,
.store_default = &value_int_store_default,
.display_default = &value_int_display_default,
.free = &value_int_free
};
static OptIntBuilderInterface int_set_required();
static OptIntBuilderInterface int_set_validator(char const* (*user_validator)(int n, void*));
static OptIntBuilderInterface int_set_default_value(int def);
static OptIntBuilderInterface int_set_base(int b);
static OptIntBuilderInterface int_set_context(void* context);
static OptIntBuilderInterface int_set_context_free(void (*)(void*));
struct opt_int_builder_interface g_int_builder = {
._data = NULL,
.required = &int_set_required,
.validator = &int_set_validator,
.default_value = &int_set_default_value,
.base = &int_set_base,
.context = &int_set_context,
.free = &int_set_context_free,
};
#define this_int_value() value_int_cast(g_int_builder._data)
OptIntBuilderInterface opt_int(int* p)
{
struct opt_value_int* value = (struct opt_value_int*)malloc(sizeof(*value));
bzero(value, sizeof(*value));
value->base = 10;
value->_base.ops = &g_value_int_ops;
value->_base.pointer = p;
g_int_builder._data = &value->_base;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_required()
{
this_int_value()->_base.required = 1;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_validator(char const* (*user_validator)(int, void*))
{
this_int_value()->int_validator = user_validator;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_default_value(int n)
{
this_int_value()->default_int = n;
this_int_value()->_base.has_default = 1;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_base(int b)
{
this_int_value()->base = b;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_context(void* context)
{
this_int_value()->context = context;
return &g_int_builder;
}
static OptIntBuilderInterface int_set_context_free(void (*free)(void*))
{
this_int_value()->context_free = free;
return &g_int_builder;
}
// }}}
// option value double {{{
struct opt_value_double
{
struct option_value _base;
double default_double;
void* context;
char const* (*validator)(double, void*);
void (*context_free)(void*);
};
#define value_double_cast(self) cast(struct opt_value_double*, self)
static char const* value_double_validate(OptionValue self, char const* arg)
{
char* endptr;
double d = strtod(arg, &endptr);
struct opt_value_double* d_self = value_double_cast(self);
if ( *endptr == '\0')
{
if (d_self->validator) return d_self->validator(d, d_self->context);
return NULL;
}
return "not a double";
}
static char const* value_double_parse(OptionValue self, char const* arg)
{
if ( self->pointer )
{
double d = strtod(arg, NULL);
*(double*)self->pointer = d;
}
return NULL;
}
static void value_double_store_default(OptionValue self)
{
struct opt_value_double* d_self = value_double_cast(self);
*(double*)self->pointer = d_self->default_double;
}
static char const* value_double_display_default(OptionValue self)
{
static char str[32];
snprintf(str, sizeof str, "%g", value_double_cast(self)->default_double);
return str;
}
static void value_double_free(OptionValue self)
{
struct opt_value_double* d_self = value_double_cast(self);
if ( d_self->context_free && d_self->context )
d_self->context_free(d_self->context);
free(d_self);
}
struct option_value_ops g_value_double_ops = {
.validate = &value_double_validate,
.parse = &value_double_parse,
.store_default = &value_double_store_default,
.display_default = &value_double_display_default,
.free = &value_double_free,
};
static OptDoubleBuilderInterface double_set_required();
static OptDoubleBuilderInterface double_set_validator(char const* (*user_validator)(double, void*));
static OptDoubleBuilderInterface double_set_default_value(double d);
static OptDoubleBuilderInterface double_set_context(void*);
static OptDoubleBuilderInterface double_set_context_free(void(*free)(void*));
struct opt_double_builder_interface g_double_builder = {
._data = NULL,
.required = &double_set_required,
.validator = &double_set_validator,
.default_value = &double_set_default_value,
.context = &double_set_context,
.free = &double_set_context_free,
};
#define this_double_value() value_double_cast(g_double_builder._data)
OptDoubleBuilderInterface opt_double(double* pointer)
{
struct opt_value_double* val = (struct opt_value_double*)malloc(sizeof(*val));
bzero(val, sizeof(*val));
val->_base.pointer = pointer;
val->_base.ops = &g_value_double_ops;
g_double_builder._data = & val->_base;
return &g_double_builder;
}
static OptDoubleBuilderInterface double_set_required()
{
this_double_value()->_base.required = 1;
return &g_double_builder;
}
static OptDoubleBuilderInterface double_set_validator(char const* (*user_validator)(double, void*))
{
this_double_value()->validator = user_validator;
return &g_double_builder;
}
static OptDoubleBuilderInterface double_set_default_value(double d)
{
this_double_value()->default_double = d;
this_double_value()->_base.has_default = 1;
return &g_double_builder;
}
static OptDoubleBuilderInterface double_set_context(void* context)
{
this_double_value()->context = context;
return &g_double_builder;
}
static OptDoubleBuilderInterface double_set_context_free(void(*free)(void*))
{
this_double_value()->context_free = free;
return &g_double_builder;
}
//}}}
// option value string {{{
struct opt_value_string
{
struct option_value _base;
char const* default_string;
};
#define value_string_cast(self) cast(struct opt_value_string*, self)
static char const* value_string_parse( OptionValue self, char const* args )
{
assert( self->pointer );
*(char const **)self->pointer = args;
return NULL;
}
static void value_string_store_default( OptionValue self )
{
struct opt_value_string* s_self = value_string_cast(self);
assert( self->pointer );
*(char const**)self->pointer = s_self->default_string;
}
static void value_string_free(OptionValue self)
{
free( value_string_cast(self) );
}
static char const* value_string_display_default(OptionValue self)
{
return value_string_cast(self)->default_string;
}
struct option_value_ops g_value_string_ops = {
.validate = NULL,
.parse = &value_string_parse,
.store_default = &value_string_store_default,
.display_default = &value_string_display_default,
.free = &value_string_free
};
static OptStringBuilderInterface string_required();
static OptStringBuilderInterface string_default_value(char const*);
struct opt_string_builder_interface g_string_builder = {
._data = NULL,
.required = &string_required,
.default_value = &string_default_value,
};
#define this_string_value() value_string_cast(g_string_builder._data)
OptStringBuilderInterface opt_string(char const** p)
{
struct opt_value_string* svalue = (struct opt_value_string*)malloc(sizeof * svalue);
bzero(svalue, sizeof*svalue);
svalue->_base.pointer = p;
svalue->_base.ops = & g_value_string_ops;
g_string_builder._data = & svalue->_base;
return &g_string_builder;
}
static OptStringBuilderInterface string_required()
{
this_string_value()->_base.required = 1;
return &g_string_builder;
}
static OptStringBuilderInterface string_default_value(char const* p)
{
this_string_value()->default_string = p;
return &g_string_builder;
}
///}}}
// option value ints {{{
struct opt_value_ints
{
struct option_value _base;
int max_output_n;
int* output_n;
int base;
int const* default_ints;
int default_n;
char const* dlms;
char const* (*validator)(int const*, int, void*);
void* context;
void (*context_free)(void*);
};
#define value_ints_cast(self) cast(struct opt_value_ints*, self)
char const* value_ints_validate(OptionValue self, char const* arg)
{
struct opt_value_ints* is_self = value_ints_cast(self);
int * arr = (int*)malloc(sizeof(int)*is_self->max_output_n);
char* buf = strdup(arg);
static char err_buf[100];
int i;
char* p;
char const* err_msg = NULL;
for(p = strtok(buf, is_self->dlms),i = 0; p; p = strtok(NULL, is_self->dlms), ++i)
{
if ( i == is_self->max_output_n)
{
err_msg = "too much int";
break;
}
char * endptr;
int a = strtol(p, &endptr, is_self->base);
if (*endptr != '\0')
{
snprintf(err_buf, sizeof err_buf, "[%d]:%s is not a int", i, p);
err_msg = err_buf;
break;
}
arr[i] = a;
}
if ( ! err_msg && is_self->validator)
{
err_msg = is_self->validator(arr, i, is_self->context);
}
free(buf);
free(arr);
return err_msg;
}
char const* value_ints_parse(OptionValue self, char const* arg)
{
struct opt_value_ints* is_self = value_ints_cast(self);
char * buf = strdup(arg);
int i ;
char* p;
for(p = strtok(buf, is_self->dlms),i = 0; p ; p = strtok(NULL, is_self->dlms), ++i)
{
int a = strtol(p, NULL, is_self->base);
((int*)self->pointer)[i] = a;
}
*is_self->output_n = i;
free(buf);
return NULL;
}
static void value_ints_store_default(OptionValue self)
{
struct opt_value_ints* is_self = value_ints_cast(self);
int n;
if ( is_self->output_n == NULL ) n = is_self->default_n;
else n = is_self->max_output_n < is_self->default_n ?is_self->max_output_n:is_self->default_n;
memcpy(self->pointer, is_self->default_ints, n * sizeof n);
if ( is_self->output_n ) *is_self->output_n = n;
}
static char const* value_ints_display_default(OptionValue self)
{
static char str[200];
struct opt_value_ints* is_self = value_ints_cast(self);
char dlm = *is_self->dlms;
int n = is_self->default_n;
int const* arr = is_self->default_ints;
int total = sizeof str;
int writed = 0;
for(int i = 0; i < n; ++i)
{
writed += snprintf(str + writed, total - writed, "%d", arr[i]);
if (i != n-1)
writed += snprintf(str + writed, total - writed, "%c", dlm);
}
return str;
}
static void value_ints_free(OptionValue self)
{
struct opt_value_ints* is_self = value_ints_cast(self);
if ( is_self->context && is_self->context_free )
is_self->context_free(is_self->context);
free(is_self);
}
struct option_value_ops g_value_ints_ops = {
.validate = & value_ints_validate,
.parse = & value_ints_parse,
.store_default = & value_ints_store_default,
.display_default = & value_ints_display_default,
.free = & value_ints_free
};
static OptIntsBuilderInterface ints_set_required();
static OptIntsBuilderInterface ints_set_default_value(int const*, int);
static OptIntsBuilderInterface ints_set_delimiters(char const*);
static OptIntsBuilderInterface ints_set_validator(char const* (*)(int const*, int , void*));
static OptIntsBuilderInterface ints_set_context(void*);
static OptIntsBuilderInterface ints_set_context_free(void(*)(void*));
static OptIntsBuilderInterface ints_set_base(int n);
struct opt_ints_builder_interface g_ints_builder = {
._data = NULL,
.required = &ints_set_required,
.default_value = &ints_set_default_value,
.delimiters = &ints_set_delimiters,
.validator = &ints_set_validator,
.context = &ints_set_context,
.free = &ints_set_context_free,
.base = &ints_set_base,
};
#define this_ints_value() cast(struct opt_value_ints*, g_ints_builder._data)
OptIntsBuilderInterface opt_ints(int * pointer, int* n)
{
struct opt_value_ints * ints = (struct opt_value_ints*)malloc(sizeof *ints);
bzero(ints, sizeof *ints);
ints->_base.pointer = pointer;
assert(n);
ints->max_output_n = *n;
ints->output_n = n;
ints->dlms = ",";
ints->_base.ops = &g_value_ints_ops;
g_ints_builder._data = &ints->_base;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_required()
{
this_ints_value()->_base.required = 1;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_default_value(int const* arr, int n)
{
this_ints_value()->default_n = n;
this_ints_value()->default_ints = arr;
this_ints_value()->_base.has_default = 1;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_delimiters(char const* dlms)
{
this_ints_value()->dlms = dlms;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_validator(char const* (validator)(int const*, int, void*))
{
this_ints_value()->validator = validator;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_context(void* context)
{
this_ints_value()->context = context;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_context_free(void(*on_free)(void*))
{
this_ints_value()->context_free = on_free;
return &g_ints_builder;
}
static OptIntsBuilderInterface ints_set_base(int n)
{
this_ints_value()->base = n;
return &g_ints_builder;
}
// }}}
// option value doubles{{{
struct opt_value_doubles
{
struct option_value _base;
int max_output_n;
int* output_n;
double const* default_doubles;
int default_n;
char const* dlms;
char const* (*validator)(double const*, int, void*);
void* context;
void (*context_free)(void*);
};
#define value_doubles_cast(self) cast(struct opt_value_doubles*, self)
char const* value_doubles_validate(OptionValue self, char const* arg)
{
struct opt_value_doubles* is_self = value_doubles_cast(self);
double * arr = (double*)malloc(sizeof(double)*is_self->max_output_n);
char* buf = strdup(arg);
static char err_buf[100];
int i;
char* p;
char const* err_msg = NULL;
for(p = strtok(buf, is_self->dlms),i = 0; p; p = strtok(NULL, is_self->dlms), ++i)
{
if ( i == is_self->max_output_n)
{
err_msg = "too much double";
break;
}
char * endptr;
double a = strtod(p, &endptr);//strtol(p, &endptr, is_self->base);
if (*endptr != '\0')
{
snprintf(err_buf, sizeof err_buf, "[%d]:%s is not a double", i, p);
err_msg = err_buf;
break;
}
arr[i] = a;
}
if ( ! err_msg && is_self->validator)
{
err_msg = is_self->validator(arr, i, is_self->context);
}
free(buf);
free(arr);
return err_msg;
}
char const* value_doubles_parse(OptionValue self, char const* arg)
{
struct opt_value_doubles* is_self = value_doubles_cast(self);
char * buf = strdup(arg);
int i ;
char* p;
for(p = strtok(buf, is_self->dlms),i = 0; p ; p = strtok(NULL, is_self->dlms), ++i)
{
double a = strtod(p, NULL);//strtol(p, NULL, is_self->base);
((double*)self->pointer)[i] = a;
}
*is_self->output_n = i;
free(buf);
return NULL;
}
static void value_doubles_store_default(OptionValue self)
{
struct opt_value_doubles* is_self = value_doubles_cast(self);
int n;
if ( is_self->output_n == NULL ) n = is_self->default_n;
else n = is_self->max_output_n < is_self->default_n ?is_self->max_output_n:is_self->default_n;
memcpy(self->pointer, is_self->default_doubles, n * sizeof(double));
if ( is_self->output_n ) *is_self->output_n = n;
}
static char const* value_doubles_display_default(OptionValue self)
{
static char str[200];
struct opt_value_doubles* is_self = value_doubles_cast(self);
char dlm = *is_self->dlms;
int n = is_self->default_n;
double const* arr = is_self->default_doubles;
int total = sizeof str;
int writed = 0;
for(int i = 0; i < n; ++i)
{
writed += snprintf(str + writed, total - writed, "%g", arr[i]);
if (i != n-1)
writed += snprintf(str + writed, total - writed, "%c", dlm);
}
return str;
}
static void value_doubles_free(OptionValue self)
{
struct opt_value_doubles* is_self = value_doubles_cast(self);
if ( is_self->context && is_self->context_free )
is_self->context_free(is_self->context);
free(is_self);
}
struct option_value_ops g_value_doubles_ops = {
.validate = & value_doubles_validate,
.parse = & value_doubles_parse,
.store_default = & value_doubles_store_default,
.display_default = & value_doubles_display_default,
.free = & value_doubles_free
};
static OptDoublesBuilderInterface doubles_set_required();
static OptDoublesBuilderInterface doubles_set_default_value(double const*, int);
static OptDoublesBuilderInterface doubles_set_delimiters(char const*);
static OptDoublesBuilderInterface doubles_set_validator(char const* (*)(double const*, int , void*));
static OptDoublesBuilderInterface doubles_set_context(void*);
static OptDoublesBuilderInterface doubles_set_context_free(void(*)(void*));
struct opt_doubles_builder_interface g_doubles_builder = {
._data = NULL,
.required = &doubles_set_required,
.default_value = &doubles_set_default_value,
.delimiters = &doubles_set_delimiters,
.validator = &doubles_set_validator,
.context = &doubles_set_context,
.free = &doubles_set_context_free,
};
#define this_doubles_value() cast(struct opt_value_doubles*, g_doubles_builder._data)
OptDoublesBuilderInterface opt_doubles(double * pointer, int* n)
{
struct opt_value_doubles * doubles = (struct opt_value_doubles*)malloc(sizeof *doubles);
bzero(doubles, sizeof *doubles);
doubles->_base.pointer = pointer;
assert(n);
doubles->max_output_n = *n;
doubles->output_n = n;
doubles->dlms = ",";
doubles->_base.ops = &g_value_doubles_ops;
g_doubles_builder._data = &doubles->_base;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_required()
{
this_doubles_value()->_base.required = 1;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_default_value(double const* arr, int n)
{
this_doubles_value()->default_n = n;
this_doubles_value()->default_doubles = arr;
this_doubles_value()->_base.has_default = 1;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_delimiters(char const* dlms)
{
this_doubles_value()->dlms = dlms;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_validator(char const* (validator)(double const*, int, void*))
{
this_doubles_value()->validator = validator;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_context(void* context)
{
this_doubles_value()->context = context;
return &g_doubles_builder;
}
static OptDoublesBuilderInterface doubles_set_context_free(void(*on_free)(void*))
{
this_doubles_value()->context_free = on_free;
return &g_doubles_builder;
}
// }}}
// option strings {{{
struct opt_value_strings
{
struct option_value _base;
int max_output_n;
int* output_n;
char const** default_strings;
int default_n;
char const* dlms;
char* buf;
};
#define value_strings_cast(self) cast(struct opt_value_strings*, self)
char const* value_strings_validate(OptionValue self, char const* arg)
{
char* buf = strdup(arg);
struct opt_value_strings* ss_self = value_strings_cast(self);
int n = ss_self->max_output_n;
int i = 0;
for(char* p = strtok(buf, ss_self->dlms); p ; p =strtok(NULL, ss_self->dlms))
{
++i;
}
char const* err_msg = NULL;
if (i > n)
{
err_msg = "too much strings";
}
free(buf);
return err_msg;
}
char const* value_strings_parse(OptionValue self, char const* arg)
{
struct opt_value_strings* ss_self = value_strings_cast(self);
if (ss_self->buf) free(ss_self->buf);
if ( self->pointer )
{
ss_self->buf = strdup(arg);
char const** output = (char const**)self->pointer;
int i = 0;
for(char* p = strtok(ss_self->buf, ss_self->dlms); p; p = strtok(NULL, ss_self->dlms))
{
output[i++] = p;
}
*(ss_self->output_n) = i;
}
return NULL;
}
static void value_strings_store_default(OptionValue self)
{
struct opt_value_strings* ss_self = value_strings_cast(self);
int n = ss_self->max_output_n < ss_self->default_n ?ss_self->max_output_n:ss_self->default_n;
memcpy(self->pointer, ss_self->default_strings, n * sizeof(char const*));
*(ss_self->output_n) = n;
}
static char const* value_strings_display_default(OptionValue self)
{
static char str[200];
struct opt_value_strings* ss_self = value_strings_cast(self);
char dlm = *ss_self->dlms;
int n = ss_self->default_n;
char const** arr = ss_self->default_strings;
int total = sizeof str;
int writed = 0;
for(int i = 0; i < n; ++i)
{
writed += snprintf(str + writed, total - writed, "%s", arr[i]);
if (i != n-1)
writed += snprintf(str + writed, total - writed, "%c", dlm);
}
return str;
}
static void value_strings_free(OptionValue self)
{
struct opt_value_strings* ss_self = value_strings_cast(self);
if ( ss_self->buf ) free(ss_self->buf);
if ( ss_self->default_strings ) free((ss_self->default_strings));
}
struct option_value_ops g_value_strings_ops = {
.validate = & value_strings_validate,
.parse = & value_strings_parse,
.store_default = & value_strings_store_default,
.display_default = & value_strings_display_default,
.free = & value_strings_free
};
static OptStringsBuilderInterface strings_set_required();
static OptStringsBuilderInterface strings_set_default_value(char const* arg, ...);
static OptStringsBuilderInterface strings_set_delimiters(char const*);
struct opt_strings_builder_interface g_strings_builder = {
._data = NULL,
.required = &strings_set_required,
.default_value = &strings_set_default_value,
.delimiters = &strings_set_delimiters,
};
#define this_strings_value() cast(struct opt_value_strings*, g_strings_builder._data)
OptStringsBuilderInterface opt_strings(char const** pointer, int* n)
{
assert(n);
struct opt_value_strings * strings= (struct opt_value_strings*)malloc(sizeof *strings);
bzero(strings, sizeof *strings);
strings->_base.pointer = pointer;
strings->max_output_n = *n;
strings->output_n = n;
strings->dlms = ",";
strings->_base.ops = &g_value_strings_ops;
g_strings_builder._data = &strings->_base;
return &g_strings_builder;
}
static OptStringsBuilderInterface strings_set_required()
{
this_strings_value()->_base.required = 1;
return &g_strings_builder;
}
static OptStringsBuilderInterface strings_set_default_value(char const* arg, ...)
{
assert(arg);
int n = 1;
va_list parg;
char const* str;
for(va_start(parg, arg), str = va_arg(parg, char const*); str; str = va_arg(parg, char const*))
{
++n;
}
this_strings_value()->default_n = n;
this_strings_value()->default_strings = (char const**)malloc(n * sizeof(char const*));
this_strings_value()->_base.has_default = 1;
va_start(parg, arg);
str = arg;
int i = 0;
for(str = arg, va_start(parg, arg); str; str = va_arg(parg, char const*), ++i)
{
this_strings_value()->default_strings[i] = str;
}
return &g_strings_builder;
}
static OptStringsBuilderInterface strings_set_delimiters(char const* dlms)
{
this_strings_value()->dlms = dlms;
return &g_strings_builder;
}
//}}}