Annotation of libaitio/example/test_vars.c, revision 1.2
1.2 ! misho 1: /*
! 2: *
! 3: * Sorting example: ./test_vars 9 sdf 2 aaa 6 gghfh 7 piuk 6 uaaa 3 abc 1 000 11 mda 0 cccc 0 fiup 23 blah
! 4: *
! 5: */
! 6: #include <stdio.h>
! 7: #include <string.h>
! 8: #include <aitio.h>
! 9:
! 10:
! 11: static int
! 12: cmpfunc(const void *a, const void *b)
! 13: {
! 14: return strcmp(AIT_GET_STR(*(ait_val_t**) a), AIT_GET_STR(*(ait_val_t**) b));
! 15: }
! 16:
! 17: int
! 18: main(int argc, char **argv)
! 19: {
! 20: array_t *arr;
! 21: ait_val_t *v;
! 22: register int i;
! 23:
! 24: arr = io_allocVars((argc - 1 ) / 2);
! 25: if (!arr) {
! 26: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
! 27: return 1;
! 28: }
! 29:
! 30: for (i = 0; i < io_arraySize(arr); i++) {
! 31: v = io_arrayGet(arr, i);
! 32: AIT_KEY(v) = atoi(argv[i * 2 + 1]);
! 33: AIT_SET_STR(v, argv[i * 2 + 2]);
! 34: }
! 35:
! 36: v = io_findKeyVars(arr, 7);
! 37: printf("v=%p\n", v);
! 38: fflush(stdout);
! 39: printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
! 40: v = io_findKeyVars(arr, 23);
! 41: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
! 42: v = io_findKeyVars(arr, 0);
! 43: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
! 44: v = io_findKeyVars(arr, 99);
! 45: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
! 46:
! 47: printf("Before sorting::\n");
! 48: for (i = 0; i < io_arraySize(arr); i++)
! 49: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
! 50: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
! 51:
! 52: io_sortVars(arr, 0, NULL);
! 53: printf("After KEY ascend sorting::\n");
! 54: for (i = 0; i < io_arraySize(arr); i++)
! 55: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
! 56: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
! 57:
! 58: io_sortVars(arr, 42, NULL);
! 59: printf("After KEY descend sorting::\n");
! 60: for (i = 0; i < io_arraySize(arr); i++)
! 61: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
! 62: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
! 63:
! 64: io_sortVars(arr, 0, cmpfunc);
! 65: printf("After string sorting::\n");
! 66: for (i = 0; i < io_arraySize(arr); i++)
! 67: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
! 68: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
! 69:
! 70: io_freeVars(&arr);
! 71: return 0;
! 72: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>