-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuvec.c
1208 lines (1100 loc) · 29.5 KB
/
uvec.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
/* uvec.c -
* Collected Unix Vector routines
*/
/** ** Copyright *********************************************************** **
** **
** Copyright 2001,2009 by R.K.Owen,Ph.D. **
** last known email: [email protected] **
** see LICENSE.LGPL, which must be provided, for details **
** **
** ************************************************************************ **/
static char Id[]="@(#)$Id$";
static void *UseId[] = { &UseId, Id };
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include "modules_def.h"
#ifdef HAVE_STRMALLOC
# include "strmalloc.h"
#endif
char uvec_TAG[5] = "UVEC";
/* uvec internal macros */
#define UVECNEXTLENGTH(x) (((x)*3)/2)
#define UVECPREVLENGTH(x) (((x)*2)/3)
/* ====================================================================== */
/* === uvec_strfns.c === */
/* uvec.c -
* uvec_set_def_strfns
* register the set of string functions to use by default.
*
* uvec_get_def_strfns
* query as to which set of string functions are being used
* returns a str_fns *
*
* uvec_set_strfns
* register the set of string functions to use for the object
* overrides the current set ... don't do this if the object is already
* fully defined.
*
* uvec_get_strfns
* query as to which set of string functions
* are being used for the object.
* returns a str_fns *
*/
/* wrappers for the StdC string functions
*/
static char *stdc_nmalloc (char const *str, size_t n) {
char *out;
/* fprintf(stderr, "stdc malloc\n"); */
if((out = module_calloc((n+1),sizeof(char))))
(void) strncpy(out, str, n);
return out;
}
static int stdc_free (char **str) {
/* fprintf(stderr, "stdc free\n"); */
null_free((void **) str);
return 0;
}
uvec_str stdc_str_fns = {
UVEC_STDC,
stdc_nmalloc,
stdc_free
};
/* ---------------------------------------------------------------------- */
#ifdef HAVE_STRMALLOC
uvec_str strmalloc_str_fns = {
UVEC_STRMALLOC,
strnmalloc,
strfree
};
uvec_str default_str_fns = {
UVEC_STRMALLOC,
strnmalloc,
strfree
};
#else
uvec_str default_str_fns = {
UVEC_STDC,
stdc_nmalloc,
stdc_free
};
#endif
/* set the default set of string functions to use
*/
int uvec_set_def_strfns(enum uvec_def_str_fns type, uvec_str *strfns) {
if (type == UVEC_DEFAULT) {
#ifdef HAVE_STRMALLOC
type = UVEC_STRMALLOC;
#else
type = UVEC_STDC;
#endif
}
if (type == UVEC_STDC) {
default_str_fns.str_alloc = stdc_str_fns.str_alloc;
default_str_fns.str_free = stdc_str_fns.str_free;
#ifdef HAVE_STRMALLOC
} else if (type == UVEC_STRMALLOC) {
default_str_fns.str_alloc = strmalloc_str_fns.str_alloc;
default_str_fns.str_free = strmalloc_str_fns.str_free;
#endif
} else if (type == UVEC_USER) {
if (strfns == (uvec_str*) NULL
|| strfns->str_alloc == NULL
|| strfns->str_free == NULL) {
return -1;
}
default_str_fns.str_alloc = strfns->str_alloc;
default_str_fns.str_free = strfns->str_free;
}
default_str_fns.type = type;
return 0;
}
/* return what type of string functions are currently default
*/
uvec_str *uvec_get_def_strfns(void) {
return &default_str_fns;
}
/* set the string functions to use
*/
int uvec_set_strfns(uvec *uv, uvec_str *strfns) {
if (!strfns) {
return -1;
}
if (uv) {
uv->str_fns.type = strfns->type;
uv->str_fns.str_alloc = strfns->str_alloc;
uv->str_fns.str_free = strfns->str_free;
} else {
return -2;
}
return 0;
}
/* return what type of string functions are currently default
*/
uvec_str *uvec_get_strfns(uvec *uv) {
if (uvec_exists(uv)) {
return &(uv->str_fns);
} else {
return (uvec_str *) NULL;
}
}
/* ====================================================================== */
/* === uvec_ctor.c === */
/* uvec.c -
* uvec_ctor_ construct Unix vector to capacity ``cap''
* using the given set of string functions.
*
* uvec_ctor construct Unix vector to capacity ``cap''
* using the default set of string functions.
*/
/* uvec_ctor_ - construct Unix vector to capacity cap and use the
* given string functions.
* returns NULL if an error, else the memory location if OK.
* uvec_ctor_ will call uvec_init_() to set things up.
*/
uvec *uvec_ctor_(int cap, uvec_str str_fns) {
uvec *uv = (uvec *) NULL;
if ((uvec *) NULL == (uv = uvec_alloc_(str_fns))) {
return uv;
}
if (uvec_init_(uv,cap,str_fns)) {
(void) uvec_dealloc(&uv);
uv = (uvec *) NULL;
}
return uv;
}
/* uvec_ctor - construct Unix vector to capacity cap
* uvec_ctor will call uvec_ctor_() to set things up.
*/
uvec *uvec_ctor(int cap) {
return uvec_ctor_(cap, default_str_fns);
}
/* ====================================================================== */
/* === uvec_dtor.c === */
/* uvec.c -
* uvec_dtor destroy the Unix vector and it's contents.
*/
/* uvec_dtor - destroy the uvec (calls uvec_close also) */
int uvec_dtor(uvec **uv) {
int retval = 0;
if ((retval = uvec_close(*uv))) return retval;
if ((retval = uvec_dealloc(uv))) return retval;
return retval;
}
/* ====================================================================== */
/* === uvec_alloc.c === */
/* uvec.c -
* uvec_alloc_ allocate the uninitialized uvec object (no
* vector is allocated) using the given set of
* string functions.
*
* uvec_alloc allocate the uninitialized uvec object (no
* vector is allocated) using the default set
* of string functions.
*/
/* uvec_alloc_ - allocate an unitialized uvec object and use the
* given string functions.
* returns NULL if an error, else the memory location if OK.
*/
uvec *uvec_alloc_(uvec_str strfns) {
uvec *uv = (uvec *) NULL;
if (!(uv = (uvec *) module_malloc(sizeof(uvec)))) {
return uv;
}
uv->str_fns.str_alloc = strfns.str_alloc;
uv->str_fns.str_free = strfns.str_free;
return uv;
}
/* uvec_alloc - allocate an unitialized uvec object
* set to use the default string functions
* uvec_alloc will call uvec_alloc_() to set things up.
*/
uvec *uvec_alloc(void) {
return uvec_alloc_(default_str_fns);
}
/* ====================================================================== */
/* === uvec_dealloc.c === */
/* uvec.c -
* uvec_dealloc Deallocate the uvec object (vector is not touched)
*
*/
/* uvec_dealloc - deallocate the unitialized uvec */
int uvec_dealloc(uvec **uv) {
int retval = 0;
null_free ((void **) uv);
/* *uv = (uvec *) NULL; */
return retval;
}
/* ====================================================================== */
/* === uvec_init.c === */
/* uvec.c -
* uvec_init_ initialize Unix vector to capacity ``cap''
* using the given set of string functions.
*
* uvec_init initialize Unix vector to capacity ``cap''
* using the default set of string functions.
*/
/* uvec_init_ - construct Unix vector to capacity cap use the
* given string functions.
* returns <0 if an error, else 0 if OK as well as all the other functions
*/
int uvec_init_(uvec *uv, int cap, uvec_str strfns) {
if (uv == (uvec *) NULL) {
return -1;
}
/* can't guarantee that struct will be initialized to 0 hence use "tag" */
if (!strncmp(uv->tag,uvec_TAG, 5)) {
return -2;
}
if (cap < 1) {
return -3;
}
if (!(uv->vector = (char **) module_calloc(cap, sizeof(char *)))) {
return -4;
}
(void) strcpy(uv->tag, uvec_TAG);
uv->capacity = cap;
uv->number = 0;
uv->str_fns.type = strfns.type;
uv->str_fns.str_alloc = strfns.str_alloc;
uv->str_fns.str_free = strfns.str_free;
return 0;
}
/* uvec_init_ - construct Unix vector to capacity cap
* use the default string functions
* uvec_init will call uvec_init_() to set things up.
*/
int uvec_init(uvec *uv, int cap) {
return uvec_init_(uv, cap, default_str_fns);
}
/* ====================================================================== */
/* === uvec_close.c === */
/* uvec.c -
* uvec_close destroy the Unix vector contents.
*/
/* uvec_close - destroy the uvec contents */
int uvec_close(uvec *uv) {
int i;
if (uv == (uvec *) NULL) {
return -1;
}
*(uv->tag) = '\0';
for (i = 0; i < uv->number; ++i) {
(uv->str_fns.str_free)(&(uv->vector[i]));
uv->vector[i] = (char *) NULL;
}
null_free((void **) &(uv->vector));
/* uv->vector = (char **) NULL; */
uv->capacity = 0;
uv->number = 0;
return 0;
}
/* ====================================================================== */
/* === uvec_accessor.c === */
/* uvec.c -
*/
/* accessor functions */
int uvec_exists(uvec const *uv) {
int retval = 0;
if (uv == (uvec *) NULL) {
retval = 0;
} else {
if (strncmp(uv->tag,uvec_TAG, 5)) {
retval = 0;
} else {
retval = 1;
}
}
return retval;
}
int uvec_capacity(uvec const *uv) {
if (uvec_exists(uv)) {
return uv->capacity;
} else {
return -1;
}
}
int uvec_number(uvec const *uv) {
if (uvec_exists(uv)) {
return uv->number;
} else {
return -1;
}
}
int uvec_end(uvec const *uv) {
if (uvec_exists(uv)) {
return uv->number - 1;
} else {
return -1;
}
}
char ** uvec_vector(uvec const *uv) {
if (uvec_exists(uv)) {
return uv->vector;
} else {
return (char **) NULL;
}
}
/* ====================================================================== */
/* === uvec_insert.c === */
/* uvec.c -
* uvec_insert insert an element before element ``place''.
*
* uvec_ninsert insert an element of size n before element ``place''.
*
* uvec_delete delete an element at element ``place''.
*
*/
/* uvec_increase - internal function to increase the size of vector
* if newcap <= 0 then increase by default size else compute new capacity
*/
static int uvec_increase(uvec *uv, int newcap) {
int i;
newcap = (newcap <= 0 ? UVECNEXTLENGTH(uv->capacity) : newcap);
if (!(uv->vector = (char **) module_realloc(uv->vector,
newcap*sizeof(char *)))) {
return -1;
}
uv->capacity = newcap;
/* zero out extra capacity */
for (i = uv->number; i < uv->capacity; ++i)
uv->vector[i] = (char *) NULL;
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_decrease - internal function to decrease the size of vector
* if newcap <= 0 then decrease by default size else compute new capacity
*/
static int uvec_decrease(uvec *uv, int newcap) {
newcap = (newcap <= 0 ? UVECPREVLENGTH(uv->capacity) : newcap);
if (!(uv->vector = (char **) module_realloc(uv->vector,
newcap*sizeof(char *)))) {
return -1;
}
uv->capacity = newcap;
return 0;
}
/* uvec_nmalloc - str_alloc & copy a string to element "place"
* assume no element exists there yet and you have the capacity.
*/
static int uvec_nmalloc(uvec *uv, char const *str, size_t n, int place) {
if (place < 0 || place >= uv->capacity) {
return -1;
}
if (!(uv->vector[place] = (uv->str_fns.str_alloc)(str,n))) {
return -2;
}
return 0;
}
/* uvec_shift_vec - internal function to shift part of the vector */
/* if start <= 0, then set start=0 (start of vector)
* if end <= 0, then set end=number (end of vector)
* newstart = where to put the range of vector
* everything between start & newstart is zeroed and assumed to be
*
* note 'end' = first element after the last element to move
*/
static int uvec_shift_vec(uvec *uv, int start, int end, int newstart) {
register int i;
int n;
int rstat;
if (start <= 0) start = 0;
if (end <= 0) end = uv->number;
if (newstart < 0) {
return -1;
}
if (start > uv->number) {
return -2;
}
if (end > uv->number || end < start) {
return -3;
}
if (newstart == start) { /* do nothing */
return 0;
}
if (newstart < start) { /* shift upwards */
for (i = newstart; i < start; ++i) {
/* dealloc elements */
(uv->str_fns.str_free)(&(uv->vector[i]));
}
for (i = start; i < end; ++i) {
uv->vector[newstart++] = uv->vector[i];
uv->vector[i] = (char *) NULL;
}
} else { /* shift downwards */
n = newstart + end - start;
if (n >= uv->capacity) {
/* increase capacity */
if ((rstat = uvec_increase(uv,
(n>UVECNEXTLENGTH(uv->capacity)
? n : UVECNEXTLENGTH(uv->capacity))))) {
return rstat - 128;
}
}
for (i = end; i < n; ++i) {
/* dealloc elements */
(uv->str_fns.str_free)(&(uv->vector[i]));
uv->vector[i] = (char *) NULL;
}
for (i = end - 1; i >= start; --i) {
uv->vector[--n] = uv->vector[i];
uv->vector[i] = (char *) NULL;
}
}
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_ninsert - insert an element of length n before element "place"
*/
int uvec_ninsert(uvec *uv, char const *str, size_t n, int place) {
int rstat;
if (place < 0 || place > uv->number) {
return -1;
}
if ((rstat = uvec_shift_vec(uv, place, 0, place + 1))) {
return rstat - 128;
}
if ((rstat = uvec_nmalloc(uv, str, n, place))) {
return rstat - 128;
}
++(uv->number);
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_insert - insert an element before element "place"
*/
int uvec_insert(uvec *uv, char const *str, int place) {
if (! str ) {
return -1;
}
return uvec_ninsert(uv, str, strlen(str)+1, place);
}
/* ---------------------------------------------------------------------- */
/* uvec_delete - delete an element at element "place"
*/
int uvec_delete(uvec *uv, int place) {
int rstat;
if (uv->number <= 0) {
return -1;
}
if (place < 0 || place > uv->number - 1) {
return -2;
}
if ((rstat = uvec_shift_vec(uv, place + 1, 0, place))) {
return rstat - 128;
}
--(uv->number);
(uv->str_fns.str_free)(&(uv->vector[uv->number]));
uv->vector[uv->number] = (char *) NULL;
if (uv->number < uv->capacity/2) {
if ((rstat = uvec_decrease(uv, 0))) {
return rstat - 128;
}
}
return 0;
}
/* ====================================================================== */
/* === uvec_add.c === */
/* uvec.c -
* uvec_add add one element to end of vector.
*
* uvec_nadd add one element of size n to end of vector.
*
* uvec_addl add a NULL terminated list of elements to
* end of vector.
*/
/* uvec_add - add 1 element to end of vector */
int uvec_add(uvec *uv, char const *str) {
if (! str ) {
return -1;
}
return uvec_nadd(uv, str, strlen(str)+1);
}
/* ---------------------------------------------------------------------- */
/* uvec_nadd - add 1 element of size n to end of vector */
int uvec_nadd(uvec *uv, char const *str, size_t n) {
int rstat;
if ((rstat = uvec_ninsert(uv, str, n, uv->number))) {
return rstat - 128;
}
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_addl - add a variable number of elements (terminated by a NULL
* argument) to end of vector
*/
int uvec_addl(uvec *uv, ...) {
int rstat;
const char *str;
va_list ap;
va_start(ap, uv);
while ((str = va_arg(ap, const char *))) {
if ((rstat = uvec_add(uv, str))) {
return rstat - 128;
}
}
va_end(ap);
return 0;
}
/* ====================================================================== */
/* === uvec_push.c === */
/* uvec.c -
* uvec_push same as uvec_add
*
* uvec_pop pop off one element at end of vector.
*/
/* uvec_push - add 1 element to end of vector (same as uvec_add) */
int uvec_push(uvec *uv, char const *str) {
return uvec_add(uv,str);
}
/* ---------------------------------------------------------------------- */
/* uvec_pop - pop off 1 element at end of vector
* and delete it
*/
int uvec_pop(uvec *uv) {
int rstat;
if ((rstat = uvec_delete(uv, uv->number - 1))) {
return rstat - 128;
}
return 0;
}
/* ====================================================================== */
/* === uvec_shift.c === */
/* uvec.c -
* uvec_unshift add one element to start of vector.
*
* uvec_shift shift one element from start of vector.
*/
/* uvec_unshift - add 1 element to the beginning of vector */
int uvec_unshift(uvec *uv, char const *str) {
int rstat;
if (! str ) {
return -1;
}
if ((rstat = uvec_ninsert(uv, str, strlen(str)+1, 0))) {
return rstat - 128;
}
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_shift - shift off 1 element at the beginning of vector
* and delete it
*/
int uvec_shift(uvec *uv) {
int rstat;
if ((rstat = uvec_delete(uv, 0))) {
return rstat - 128;
}
return 0;
}
/* ====================================================================== */
/* === uvec_count_tok.c === */
/* uvec.c -
* uvec_copy_str * create a uvec from a token delimited string.
* Tokens escaped with a '\' are not counted.
*/
/* some useful functions ...
* note that all these use no direct access to the structure
* and form an example set of how to use uvec */
/* ---------------------------------------------------------------------- */
/* uvec_count_tok - returns the number of token delimited elements
* in a string (trailing following token is optional)
* returns -1 if an error
* '\' before the token escapes it
* ... but it's still possible to confuse the count
*/
int uvec_count_tok(char const *token, char const *string) {
int num = 0;
int toklen = 0;
const char *ptr = string;
const char *tokptr;
if (! string) return 0;
if (! *string) return 0;
if (! token) return -1; /* gotta supply a token */
if (! *token) return -1;
toklen = strlen(token);
if (! toklen) return -1;
while (*ptr) {
num++;
tokptr = strstr(ptr, token);
if (! tokptr ) break;
if ( tokptr > string) {
if (*(tokptr-1) == '\\') num--;
}
ptr = tokptr + toklen;
}
return num;
}
/* ====================================================================== */
/* === uvec_copy_str.c === */
/* uvec.c -
* uvec_copy_str create a uvec from a token delimited string.
* Tokens escaped with a '\' are not counted.
*/
/* uvec_copy_str - create a uvec from a token delimited string
* '\' before the token escapes it
* ... but it's still possible to confuse the count
*/
int uvec_copy_str(uvec *u, char const *token, char const *string) {
int num = uvec_count_tok(token,string);
const char *ptr = string;
const char *tokptr;
int toklen = strlen(token);
int tokfound = 0;
int rstat;
size_t fraglen;
if (num < 0) {
return -1;
}
#if 0
if (! string) {
return -2;
}
#endif
if ((rstat = uvec_init(u, num + 1))) {
return rstat - 128;
}
while (ptr && *ptr) {
tokptr = ptr;
while (! tokfound) {
tokptr = strstr(tokptr, token);
if (tokptr && (tokptr > string)) {
if (*(tokptr-1) != '\\') {
tokfound = 1;
} else {
tokptr++;
continue;
}
}
if ((tokptr == string) || !tokptr) tokfound = 1;
}
tokfound = 0;
if (! tokptr ) {
if ((rstat = uvec_add(u, ptr))) {
return rstat - 128;
}
break;
} else {
/* add 1 to length and set last char to \0 */
fraglen = tokptr - ptr;
uvec_nadd(u, ptr, fraglen + 1);
ptr = uvec_vector(u)[uvec_end(u)];
*((char *) ptr + fraglen) = '\0';
}
ptr = tokptr + toklen;
}
return 0;
}
/* ====================================================================== */
/* === uvec_copy_vec.c === */
/* uvec.c -
* uvec_copy_vec copy an existing char vector to an
* unitialized uvec. Set num to a value less
* than or equal to 0 to grab the entire vector.
*/
/* uvec_copy_vec - copy an existing vector to an unitialized uvec */
int uvec_copy_vec(uvec *u, char const * const *vec, int number) {
int rstat;
char const * const *ptr = vec;
int num = 0;
if (number <= 0) {
/* count number in vector (add 1) */
while (*ptr++) ++num;
number = ++num;
}
if ((rstat = uvec_init_(u, number,u->str_fns))) {
return rstat - 128;
}
for (ptr = vec; *ptr != (char *)NULL; ++ptr) {
if ((rstat = uvec_add(u, *ptr))) {
return rstat - 128;
}
}
return 0;
}
/* ====================================================================== */
/* === uvec_copy.c === */
/* uvec.c -
* uvec_copy copy one uvec to another unitialized one.
*/
/* uvec_copy - copy one uvec to another unitialized one */
int uvec_copy(uvec *u, uvec const *v) {
int rstat;
if ((rstat = uvec_copy_vec(u, (char const * const *) uvec_vector(v),
uvec_capacity(v)))) {
return rstat - 128;
}
return 0;
}
/* ====================================================================== */
/* === uvec_sort.c === */
/* uvec.c -
* uvec_sort sort the vector, given the following types:
* UVEC_ASCEND, UVEC_DESCEND,
* UVEC_CASE_ASCEND, UVEC_CASE_DESCEND, where
* the last two are only available if the str
* casecmp function is available for ``case
* less'' string comparisons.
*
* uvec_qsort sort the vector, according to the passed comparison function,
* which is of the same type as needed by qsort()
* namely int cmp(void const *a, void const *b)
*
* uvec_find find the first or last element that matches
* the string str depending on the sort type
* and returns the element number. If the
* string is not found then returns -1. A
* value less than -1 indicates an error,
* which probably can be ignored. type = uvec
* sorting type.
*
* uvec_uniq remove all adjacent duplicate elements,
* where type = uvec sorting type ... the
* important information is whether to use a
* caseless comparison or not, but there may
* be subtle side effects depending on whether
* the sort type is ascending or descending.
*/
/* comparison functions */
static int uvec_sort_cmp_ascend(void const *a, void const *b) {
return strcmp(*(char **) a, *(char **) b);
}
static int uvec_sort_cmp_descend(void const *a, void const *b) {
return strcmp(*(char **) b, *(char **) a);
}
#ifdef HAVE_STRCASECMP
static int uvec_sort_cmp_case_ascend(void const *a, void const *b) {
return strcasecmp(*(char **) a, *(char **) b);
}
static int uvec_sort_cmp_case_descend(void const *a, void const *b) {
return strcasecmp(*(char **) b, *(char **) a);
}
#endif /* HAVE_STRCASECMP */
/* ---------------------------------------------------------------------- */
/* uvec_sort - sort the vector */
int uvec_sort(uvec *uv, enum uvec_order type) {
int (*cmp)(void const *, void const *);
if (!uvec_exists(uv)) {
return -1;
}
if (uvec_number(uv) > 1) {
switch (type) {
case UVEC_ASCEND:
cmp = uvec_sort_cmp_ascend;
break;
case UVEC_DESCEND:
cmp = uvec_sort_cmp_descend;
break;
#ifdef HAVE_STRCASECMP
case UVEC_CASE_ASCEND:
cmp = uvec_sort_cmp_case_ascend;
break;
case UVEC_CASE_DESCEND:
cmp = uvec_sort_cmp_case_descend;
break;
#endif /* HAVE_STRCASECMP */
default:
return -2;
}
}
uvec_qsort(uv, cmp);
return 0;
}
/* ---------------------------------------------------------------------- */
/* uvec_qsort - sort the vector */
int uvec_qsort(uvec *uv, int (*cmp)(void const *a, void const *b)) {
int retval = -1;
if (uvec_exists(uv) && cmp) {
qsort((void *) uvec_vector(uv), (size_t) uvec_number(uv),
sizeof(char *), cmp);
return 0;
} else {
return retval;
}
}
/* ---------------------------------------------------------------------- */
/* uvec_find - finds the first or last entry in a vector that matches
* the string given the uvec sorting type.
*/
int uvec_find(uvec *uv, char const *str, enum uvec_order type) {
int i = 0;
char **vec;
int (*cmp)(void const *, void const *);
if (!uvec_exists(uv)) {
return -2;
}
if (uvec_number(uv) > 0) {
switch (type) {
case UVEC_ASCEND:
cmp = uvec_sort_cmp_descend;
break;
case UVEC_DESCEND:
cmp = uvec_sort_cmp_ascend;
break;
#ifdef HAVE_STRCASECMP
case UVEC_CASE_ASCEND:
cmp = uvec_sort_cmp_case_descend;
break;
case UVEC_CASE_DESCEND:
cmp = uvec_sort_cmp_case_ascend;
break;
#endif /* HAVE_STRCASECMP */
default: /* assume case sensitive */
cmp = uvec_sort_cmp_ascend;
}
}
while (*(vec = (uvec_vector(uv) + i))) {
if (!(*cmp)(vec, &str)) return i;
i++;
}
return -1; /* string not found */
}
/* ---------------------------------------------------------------------- */
/* uvec_uniq - remove all adjacent duplicate elements
* type = uvec sorting type ... the import information is whether
* to use a caseless comparison or not.
*/
int uvec_uniq(uvec *uv, enum uvec_order type) {
int i = 1;
char **vec;
int (*cmp)(void const *, void const *);
if (!uvec_exists(uv)) {
return -1;
}
if (uvec_number(uv) > 1) {
switch (type) {
case UVEC_ASCEND:
cmp = uvec_sort_cmp_ascend;
break;
case UVEC_DESCEND:
cmp = uvec_sort_cmp_descend;
break;
#ifdef HAVE_STRCASECMP
case UVEC_CASE_ASCEND:
cmp = uvec_sort_cmp_case_ascend;
break;
case UVEC_CASE_DESCEND:
cmp = uvec_sort_cmp_case_descend;
break;
#endif /* HAVE_STRCASECMP */
default:
return -2;
}
while (*(vec = (uvec_vector(uv) + i))) {
if ((*cmp)(vec, (vec - 1))) {
i++; /* go to next */
} else {
uvec_delete(uv,i); /* found match */
}
}
}
return 0;
}
/* ====================================================================== */
/* === uvec_reverse.c === */
/* uvec.c -
* uvec_reverse reverses the element order of the vector.
*/
/* uvec_reverse - reverses the element order of the vector */
int uvec_reverse(uvec const *uv) {
int num;
char **ptrstart, **ptrend;
char *swap;
if (!uvec_exists(uv)) {
}
num = uvec_number(uv);
if (num > 1) { /* do reversal */
ptrstart = uvec_vector(uv);
ptrend = ptrstart + uvec_end(uv);
while (ptrstart < ptrend) {