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