Annotation of libaitio/example/test_vars.c, revision 1.4
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:
1.4 ! misho 17: static void
! 18: makevars()
! 19: {
! 20: ait_val_t *v;
! 21:
! 22: v = io_makeVar(empty);
! 23: printf("Variable is empty? %d\n", AIT_ISEMPTY(v));
! 24: io_freeVar(&v);
! 25: v = io_makeVar(ptr, 0x12345678, 42);
! 26: printf("Variable is ptr=%p len=%u\n", AIT_GET_PTR(v), AIT_LEN(v));
! 27: io_freeVar(&v);
! 28: v = io_makeVar(data, "TEST DATA !!!", 14);
! 29: printf("Variable is data=%s len=%u\n", AIT_GET_DATA(v), AIT_LEN(v));
! 30: io_freeVar(&v);
! 31: v = io_makeVar(buffer, "TEST Buffer !!!", 16);
! 32: printf("Variable is buf=%s len=%u\n", AIT_GET_BUF(v), AIT_LEN(v));
! 33: io_freeVar(&v);
! 34: v = io_makeVar(string, "TEST String ...");
! 35: printf("Variable is str=%s len=%u\n", AIT_GET_STR(v), AIT_LEN(v));
! 36: io_freeVar(&v);
! 37: v = io_makeVar(blob, 1234, 10000);
! 38: printf("Variable is blob=%u len=%u\n", AIT_GET_BLOB(v), AIT_LEN(v));
! 39: io_freeVar(&v);
! 40: v = io_makeVar(f32, 1.0123);
! 41: printf("Variable is f32=%f\n", AIT_GET_F32(v));
! 42: io_freeVar(&v);
! 43: v = io_makeVar(f64, 0.00123321);
! 44: printf("Variable is f64=%f\n", AIT_GET_F64(v));
! 45: io_freeVar(&v);
! 46: v = io_makeVar(u8, 1);
! 47: printf("Variable is u8=%hhu\n", AIT_GET_U8(v));
! 48: io_freeVar(&v);
! 49: v = io_makeVar(u16, 32000);
! 50: printf("Variable is u16=%hu\n", AIT_GET_U16(v));
! 51: io_freeVar(&v);
! 52: v = io_makeVar(u32, 0xfedecaa0);
! 53: printf("Variable is u32=%u\n", AIT_GET_U32(v));
! 54: io_freeVar(&v);
! 55: v = io_makeVar(u64, 0xffeedccfedecaa01LL);
! 56: printf("Variable is u64=%llu\n", AIT_GET_U64(v));
! 57: io_freeVar(&v);
! 58: v = io_makeVar(i8, -1);
! 59: printf("Variable is i8=%hhd\n", AIT_GET_I8(v));
! 60: io_freeVar(&v);
! 61: v = io_makeVar(i16, -28888);
! 62: printf("Variable is i16=%hd\n", AIT_GET_I16(v));
! 63: io_freeVar(&v);
! 64: v = io_makeVar(i32, -23424234);
! 65: printf("Variable is i32=%d\n", AIT_GET_I32(v));
! 66: io_freeVar(&v);
! 67: v = io_makeVar(i64, -123234200000342LL);
! 68: printf("Variable is i64=%lld\n", AIT_GET_I64(v));
! 69: io_freeVar(&v);
! 70: }
! 71:
1.2 misho 72: int
73: main(int argc, char **argv)
74: {
75: array_t *arr;
76: ait_val_t *v;
77: register int i;
78:
1.4 ! misho 79: if (argc < 2) {
! 80: makevars();
! 81: return 0;
! 82: }
! 83:
1.2 misho 84: arr = io_allocVars((argc - 1 ) / 2);
85: if (!arr) {
86: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
87: return 1;
88: }
89:
90: for (i = 0; i < io_arraySize(arr); i++) {
91: v = io_arrayGet(arr, i);
92: AIT_KEY(v) = atoi(argv[i * 2 + 1]);
93: AIT_SET_STR(v, argv[i * 2 + 2]);
94: }
95:
96: v = io_findKeyVars(arr, 7);
97: printf("v=%p\n", v);
98: fflush(stdout);
99: printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
100: v = io_findKeyVars(arr, 23);
101: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
102: v = io_findKeyVars(arr, 0);
103: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
104: v = io_findKeyVars(arr, 99);
105: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
106:
107: printf("Before sorting::\n");
108: for (i = 0; i < io_arraySize(arr); i++)
109: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
110: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
111:
1.3 misho 112: io_sortVarsByKey(arr, 0);
1.2 misho 113: printf("After KEY ascend sorting::\n");
114: for (i = 0; i < io_arraySize(arr); i++)
115: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
116: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
117:
1.3 misho 118: io_sortVarsByKey(arr, 42);
1.2 misho 119: printf("After KEY descend sorting::\n");
120: for (i = 0; i < io_arraySize(arr); i++)
121: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
122: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
123:
1.3 misho 124: io_sortVarsByVal(arr, 0, cmpfunc);
1.2 misho 125: printf("After string sorting::\n");
126: for (i = 0; i < io_arraySize(arr); i++)
127: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
128: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
129:
130: io_freeVars(&arr);
131: return 0;
132: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>