fix dataiterator returning random data in some cases
[platform/upstream/libsolv.git] / src / dataiterator.h
1 /*
2  * Copyright (c) 2007-2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * dataiterator.h
10  * 
11  */
12
13 #ifndef LIBSOLV_DATAITERATOR_H
14 #define LIBSOLV_DATAITERATOR_H
15
16 #include "pooltypes.h"
17 #include "pool.h"
18
19 struct _Repo;
20
21 typedef struct _KeyValue {
22   Id id;
23   const char *str;
24   unsigned int num;
25   unsigned int num2;
26
27   int entry;    /* array entry, starts with 0 */
28   int eof;      /* last entry reached */
29
30   struct _KeyValue *parent;
31 } KeyValue;
32
33 #define SOLV_KV_NUM64(kv) (((unsigned long long)((kv)->num2)) << 32 | (kv)->num)
34
35 /* search matcher flags */
36 #define SEARCH_STRINGMASK               15
37 #define SEARCH_STRING                   1
38 #define SEARCH_STRINGSTART              2
39 #define SEARCH_STRINGEND                3
40 #define SEARCH_SUBSTRING                4
41 #define SEARCH_GLOB                     5
42 #define SEARCH_REGEX                    6
43 #define SEARCH_ERROR                    15
44 #define SEARCH_NOCASE                   (1<<7)
45
46 /* iterator control */
47 #define SEARCH_NO_STORAGE_SOLVABLE      (1<<8)
48 #define SEARCH_SUB                      (1<<9)
49 #define SEARCH_ARRAYSENTINEL            (1<<10)
50 #define SEARCH_DISABLED_REPOS           (1<<11)
51 #define SEARCH_COMPLETE_FILELIST        (1<<12)
52
53 /* stringification flags */
54 #define SEARCH_SKIP_KIND                (1<<16)
55 /* By default we stringify just to the basename of a file because
56    the construction of the full filename is costly.  Specify this
57    flag if you want to match full filenames */
58 #define SEARCH_FILES                    (1<<17)
59 #define SEARCH_CHECKSUMS                (1<<18)
60
61 /* dataiterator internal */
62 #define SEARCH_THISSOLVID               (1<<31)
63
64 /*
65  * Datamatcher: match a string against a query
66  */
67 typedef struct _Datamatcher {
68   int flags;            /* see matcher flags above */
69   const char *match;    /* the query string */
70   void *matchdata;      /* e.g. compiled regexp */
71   int error;
72 } Datamatcher;
73
74 int  datamatcher_init(Datamatcher *ma, const char *match, int flags);
75 void datamatcher_free(Datamatcher *ma);
76 int  datamatcher_match(Datamatcher *ma, const char *str);
77 int  datamatcher_checkbasename(Datamatcher *ma, const char *str);
78
79
80 /*
81  * Dataiterator
82  * 
83  * Iterator like interface to 'search' functionality
84  * 
85  * Dataiterator is per-pool, additional filters can be applied
86  * to limit the search domain. See dataiterator_init below.
87  * 
88  * Use these like:
89  *    Dataiterator di;
90  *    dataiterator_init(&di, repo->pool, repo, 0, 0, "bla", SEARCH_SUBSTRING);
91  *    while (dataiterator_step(&di))
92  *      dosomething(di.solvid, di.key, di.kv);
93  *    dataiterator_free(&di);
94  */
95 typedef struct _Dataiterator
96 {
97   int state;
98   int flags;
99
100   Pool *pool;
101   struct _Repo *repo;
102   struct _Repodata *data;
103
104   /* data pointers */
105   unsigned char *dp;
106   unsigned char *ddp;
107   Id *idp;
108   Id *keyp;
109
110   /* the result */
111   struct _Repokey *key;
112   KeyValue kv;
113
114   /* our matcher */
115   Datamatcher matcher;
116
117   /* iterators/filters */
118   Id keyname;
119   Id repodataid;
120   Id solvid;
121   Id repoid;
122
123   Id keynames[3 + 1];
124   int nkeynames;
125   int rootlevel;
126
127   /* recursion data */
128   struct di_parent {
129     KeyValue kv;
130     unsigned char *dp;
131     Id *keyp;
132   } parents[3];
133   int nparents;
134
135   /* vertical data */
136   unsigned char *vert_ddp;
137   Id vert_off;
138   Id vert_len;
139   Id vert_storestate;
140
141   /* strdup data */
142   char *dupstr;
143   int dupstrn;
144
145 } Dataiterator;
146
147
148 /*
149  * Initialize dataiterator
150  * 
151  * di:      Pointer to Dataiterator to be initialized
152  * pool:    Search domain for the iterator
153  * repo:    if non-null, limit search to this repo
154  * solvid:  if non-null, limit search to this solvable
155  * keyname: if non-null, limit search to this keyname
156  * match:   if non-null, limit search to this match
157  */
158 int  dataiterator_init(Dataiterator *di, Pool *pool, struct _Repo *repo, Id p, Id keyname, const char *match, int flags);
159 void dataiterator_init_clone(Dataiterator *di, Dataiterator *from);
160 void dataiterator_set_search(Dataiterator *di, struct _Repo *repo, Id p);
161 void dataiterator_set_keyname(Dataiterator *di, Id keyname);
162 int  dataiterator_set_match(Dataiterator *di, const char *match, int flags);
163
164 void dataiterator_prepend_keyname(Dataiterator *di, Id keyname);
165 void dataiterator_free(Dataiterator *di);
166 int  dataiterator_step(Dataiterator *di);
167 void dataiterator_setpos(Dataiterator *di);
168 void dataiterator_setpos_parent(Dataiterator *di);
169 int  dataiterator_match(Dataiterator *di, Datamatcher *ma);
170 void dataiterator_skip_attribute(Dataiterator *di);
171 void dataiterator_skip_solvable(Dataiterator *di);
172 void dataiterator_skip_repo(Dataiterator *di);
173 void dataiterator_jump_to_solvid(Dataiterator *di, Id solvid);
174 void dataiterator_jump_to_repo(Dataiterator *di, struct _Repo *repo);
175 void dataiterator_entersub(Dataiterator *di);
176 void dataiterator_clonepos(Dataiterator *di, Dataiterator *from);
177 void dataiterator_seek(Dataiterator *di, int whence);
178 void dataiterator_strdup(Dataiterator *di);
179
180 #define DI_SEEK_STAY    (1 << 16)
181 #define DI_SEEK_CHILD   1
182 #define DI_SEEK_PARENT  2
183 #define DI_SEEK_REWIND  3
184
185 #endif /* LIBSOLV_DATAITERATOR_H */