Annotation of libaitio/example/test_vars.c, revision 1.3.4.1
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.3.4.1 ! 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\n", AIT_GET_PTR(v));
! 27: io_freeVar(&v);
! 28: v = io_makeVar(data, "TEST DATA !!!", 14);
! 29: printf("Variable is data=%s\n", AIT_GET_DATA(v));
! 30: io_freeVar(&v);
! 31: v = io_makeVar(buffer, "TEST Buffer !!!", 16);
! 32: printf("Variable is buf=%s\n", AIT_GET_BUF(v));
! 33: io_freeVar(&v);
! 34: v = io_makeVar(string, "TEST String ...");
! 35: printf("Variable is str=%s\n", AIT_GET_STR(v));
! 36: io_freeVar(&v);
! 37: v = io_makeVar(blob, 1234, 10000);
! 38: printf("Variable is blob=%s 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:
79: arr = io_allocVars((argc - 1 ) / 2);
80: if (!arr) {
81: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
82: return 1;
83: }
84:
85: for (i = 0; i < io_arraySize(arr); i++) {
86: v = io_arrayGet(arr, i);
87: AIT_KEY(v) = atoi(argv[i * 2 + 1]);
88: AIT_SET_STR(v, argv[i * 2 + 2]);
89: }
90:
91: v = io_findKeyVars(arr, 7);
92: printf("v=%p\n", v);
93: fflush(stdout);
94: printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
95: v = io_findKeyVars(arr, 23);
96: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
97: v = io_findKeyVars(arr, 0);
98: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
99: v = io_findKeyVars(arr, 99);
100: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
101:
102: printf("Before sorting::\n");
103: for (i = 0; i < io_arraySize(arr); i++)
104: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
105: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
106:
1.3 misho 107: io_sortVarsByKey(arr, 0);
1.2 misho 108: printf("After KEY ascend sorting::\n");
109: for (i = 0; i < io_arraySize(arr); i++)
110: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
111: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
112:
1.3 misho 113: io_sortVarsByKey(arr, 42);
1.2 misho 114: printf("After KEY descend sorting::\n");
115: for (i = 0; i < io_arraySize(arr); i++)
116: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
117: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
118:
1.3 misho 119: io_sortVarsByVal(arr, 0, cmpfunc);
1.2 misho 120: printf("After string sorting::\n");
121: for (i = 0; i < io_arraySize(arr); i++)
122: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
123: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
124:
125: io_freeVars(&arr);
126: return 0;
127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>