-
Notifications
You must be signed in to change notification settings - Fork 0
/
atest.c
98 lines (80 loc) · 2.82 KB
/
atest.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
#include <stdio.h>
#include <string.h>
// First include the assoc.h header
#include "assoc.h"
// Then define a function taking two const void * and returning an integer
// Note: the function must work as strcmp and compare two domain objects
int cmp(const void*a, const void*b) {
return strcmp(a, b);
}
int main(int argc, char *argv[], char *arge[]) {
// Then you declare your associative arrays.
// This is the same as declaring FILE* pointers: these are containers waiting
// to receive a reference to an ASSOC object!
ASSOC *a, *b;
char *s;
// Then you "open" your first ASSOC. This is like using fopen(),
// but you pass your object compare function instead of a filename
a=aopen(cmp);
if(*aerror) printf("aerror=%s\n", aerror);
// awrite associates keys to values. This one is the same as
// a['paris'] = 'france' # in Python
awrite(a, "paris", "france");
if(*aerror) printf("aerror=%s\n", aerror);
// a['dublin'] = 'ireland' # in Python
awrite(a, "dublin", "ireland");
if(*aerror) printf("aerror=%s\n", aerror);
// a['roma'] = 'italia' # in Python
awrite(a, "roma", "italia");
if(*aerror) printf("aerror=%s\n", aerror);
// anext returns the next key in the array
printf("anexts...\n");
while (s = (char*)anext(a) ) {
// aread returns the value associated with a key, if any
// for s in a: # In Python:
// print(a[s])
printf("domain %s: codomain = %s\n", s, aread(a, s));
if(*aerror) printf("aerror=%s\n", aerror);
}
// etc etc
printf("awrites...\n");
awrite(a, "london", "uk");
if(*aerror) printf("aerror=%s\n", aerror);
printf("anexts...\n");
while (s = (char*)anext(a) ) {
printf("domain %s: codomain = %s\n", s, aread(a, s));
if(*aerror) printf("aerror=%s\n", aerror);
}
// arewind "rewinds" the keys of an array. No need to do that in Python...
printf("\narewind\n\n");
arewind(a);
if(*aerror) printf("aerror=%s\n", aerror);
printf("anexts...\n");
while (s = (char*)anext(a) ) {
printf("domain %s: codomain = %s\n", s, aread(a, s));
if(*aerror) printf("aerror=%s\n", aerror);
}
// adel removes a key -> value association
printf("adel...\n");
adel(a, "roma");
printf("\narewind\n\n");
arewind(a);
if(*aerror) printf("aerror=%s\n", aerror);
printf("anexts...\n");
while (s = (char*)anext(a) ) {
printf("domain %s: codomain = %s\n", s, aread(a, s));
if(*aerror) printf("aerror=%s\n", aerror);
}
printf("aread(\"roma\")=%s, aerror=\"%s\"\n", aread(a, "roma"), aerror);
// aenv constructs an ASSOC object with all environment variables
printf("aenv()...\n");
b=aenv();
printf("...done\n");
if(*aerror) printf("aerror=%s\n", aerror);
while (s = (char*)anext(b) ) {
printf("domain %s: codomain = %s\n", s, aread(b, s));
if(*aerror) printf("aerror=%s\n", aerror);
}
// this is destructor! It's like fclose(), but closes the array instead of the file
aclose(a);
}