Annotation of libaitio/example/test_vars.c, revision 1.4.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.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);
1.4.4.1 ! misho 92: // AIT_KEY(v) = atoi(argv[i * 2 + 1]);
1.2 misho 93: AIT_SET_STR(v, argv[i * 2 + 2]);
1.4.4.1 ! misho 94: io_hashVar(v);
1.2 misho 95: }
96:
1.4.4.1 ! misho 97: // io_hashKeyVars(arr);
! 98:
1.2 misho 99: v = io_findKeyVars(arr, 7);
100: printf("v=%p\n", v);
101: fflush(stdout);
102: printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
103: v = io_findKeyVars(arr, 23);
104: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
105: v = io_findKeyVars(arr, 0);
106: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
107: v = io_findKeyVars(arr, 99);
108: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
109:
110: printf("Before sorting::\n");
111: for (i = 0; i < io_arraySize(arr); i++)
112: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
113: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
114:
1.3 misho 115: io_sortVarsByKey(arr, 0);
1.2 misho 116: printf("After KEY ascend sorting::\n");
117: for (i = 0; i < io_arraySize(arr); i++)
118: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
119: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
120:
1.3 misho 121: io_sortVarsByKey(arr, 42);
1.2 misho 122: printf("After KEY descend sorting::\n");
123: for (i = 0; i < io_arraySize(arr); i++)
124: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
125: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
126:
1.3 misho 127: io_sortVarsByVal(arr, 0, cmpfunc);
1.2 misho 128: printf("After string sorting::\n");
129: for (i = 0; i < io_arraySize(arr); i++)
130: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
131: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
132:
133: io_freeVars(&arr);
134: return 0;
135: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>