1 #include <ctf-api.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 static int
print_enum(const char * name,int val,void * unused)6 print_enum (const char *name, int val, void *unused)
7 {
8   printf ("iter test: %s has value %i\n", name, val);
9   return 0;
10 }
11 
12 int
main(int argc,char * argv[])13 main (int argc, char *argv[])
14 {
15   ctf_dict_t *fp;
16   ctf_archive_t *ctf;
17   ctf_id_t type;
18   const char *name;
19   ctf_next_t *i = NULL;
20   int val;
21   int err;
22 
23   if (argc != 2)
24     {
25       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
26       exit(1);
27     }
28 
29   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
30     goto open_err;
31   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
32     goto open_err;
33 
34   /* Try getting some enum values by hand.  */
35 
36   if ((type = ctf_lookup_by_name (fp, "enum e") ) == CTF_ERR)
37     goto err;
38   if (ctf_enum_value (fp, type, "ENUMSAMPLE_1", &val) < 0)
39     goto err;
40   printf ("Enum e enumerand ENUMSAMPLE_1 has value %i\n", val);
41 
42   if ((name = ctf_enum_name (fp, type, 1)) == NULL)
43     goto err;
44   printf ("Enum e enumerand %s has value 1\n", name);
45 
46   /* Try getting some values using both sorts of iterator.  */
47 
48   if ((type = ctf_lookup_by_name (fp, "enum ie") ) == CTF_ERR)
49     goto err;
50 
51   if ((ctf_enum_iter (fp, type, print_enum, NULL)) < 0)
52     goto ierr;
53 
54   while ((name = ctf_enum_next (fp, type, &i, &val)) != NULL)
55     {
56       printf ("next test: %s has value %i\n", name, val);
57     }
58   if (ctf_errno (fp) != ECTF_NEXT_END)
59     goto nerr;
60 
61   ctf_dict_close (fp);
62   ctf_close (ctf);
63 
64   return 0;
65 
66  open_err:
67   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
68   return 1;
69  err:
70   fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
71   return 1;
72  ierr:
73   fprintf (stderr, "_iter iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
74   return 1;
75  nerr:
76   fprintf (stderr, "_next iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
77   return 1;
78 }
79