Some infrastructure for directory trees.
[platform/upstream/libsolv.git] / tools / dumpattr.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
16
17 #include "attr_store.h"
18 #include "pool.h"
19 #include "attr_store_p.h"
20
21 static void
22 dump_attrs (Attrstore *s, unsigned int entry)
23 {
24   attr_iterator ai;
25   FOR_ATTRS (s, entry, &ai)
26     {
27       fprintf (stdout, "%s:", id2str (s->pool, ai.name));
28       switch (ai.type)
29         {
30         case TYPE_ATTR_INT:
31           fprintf (stdout, "int  %u\n", ai.as_int);
32           break;
33         case TYPE_ATTR_CHUNK:
34           {
35             const char *str = attr_retrieve_blob (s, ai.as_chunk[0], ai.as_chunk[1]);
36             if (str)
37               fprintf (stdout, "blob %s\n", str);
38             else
39               fprintf (stdout, "blob %u+%u\n", ai.as_chunk[0], ai.as_chunk[1]);
40           }
41           break;
42         case TYPE_ATTR_STRING:
43           fprintf (stdout, "str  %s\n", ai.as_string);
44           break;
45         case TYPE_ATTR_INTLIST:
46           {
47             fprintf (stdout, "lint\n ");
48             while (1)
49               {
50                 int val;
51                 get_num (ai.as_numlist, val);
52                 if (!val)
53                   break;
54                 fprintf (stdout, " %d", val);
55               }
56             fprintf (stdout, "\n");
57             break;
58           }
59         case TYPE_ATTR_LOCALIDS:
60           {
61             fprintf (stdout, "lids");
62             while (1)
63               {
64                 Id val;
65                 get_num (ai.as_numlist, val);
66                 if (!val)
67                   break;
68                 fprintf (stdout, "\n  %s(%d)", localid2str (s, val), val);
69               }
70             fprintf (stdout, "\n");
71             break;
72           }
73         default:
74           if (ai.type >= TYPE_ATTR_SPECIAL_START
75               && ai.type <= TYPE_ATTR_SPECIAL_END)
76             fprintf (stdout, "spec %d", ai.type - TYPE_ATTR_SPECIAL_START);
77           fprintf (stdout, "\n");
78           break;
79         }
80     }
81 }
82
83 static int
84 callback (Attrstore *s, unsigned entry, Id name, const char *str)
85 {
86   fprintf (stdout, "%d:%s:%s\n", entry, id2str (s->pool, name), str);
87   return 1;
88 }
89
90 int
91 main (int argc, char *argv[])
92 {
93   unsigned int i;
94   Pool *pool = pool_create ();
95   Attrstore *s = attr_store_read (stdin, pool);
96   /* For now test the packing code.  */
97   attr_store_unpack (s);
98   attr_store_pack (s);
99   fprintf (stdout, "attribute store contains %d entities\n", s->entries);
100   if (argc == 1)
101     for (i = 0; i < s->entries; i++)
102       {
103         fprintf (stdout, "\nentity %u:\n", i);
104         dump_attrs (s, i);
105       }
106   else
107     {
108       int g;
109       unsigned flags;
110       unsigned search_type;
111       Id name;
112       name = 0;
113       flags = SEARCH_IDS;
114       search_type = SEARCH_SUBSTRING;
115       while ((g = getopt (argc, argv, "-n:bgeri")) >= 0)
116         switch (g)
117         {
118           case 'n': name = str2id (s->pool, optarg, 1); break;
119           case 'b': flags |= SEARCH_BLOBS; break;
120           case 'g': search_type = SEARCH_GLOB; break;
121           case 'e': search_type = SEARCH_STRING; break;
122           case 'r': search_type = SEARCH_REGEX; break;
123           case 'i': flags |= SEARCH_NOCASE; break;
124           case 1:
125             attr_store_search_s (s, optarg, search_type | flags, name, callback);
126             flags = SEARCH_IDS;
127             name = 0;
128             search_type = SEARCH_SUBSTRING;
129             break;
130         }
131     }
132   return 0;
133 }