xref: /haiku/src/tests/add-ons/kernel/file_systems/shared/queries/QueryParserTest.cpp (revision b8a45b3a2df2379b4301bf3bd5949b9a105be4ba)
1 /*
2  * Copyright 2024, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <stdio.h>
7 
8 #define DEBUG_QUERY
9 #define PRINT(expr) printf expr
10 #define __out(message...) PRINT((message))
11 #define QUERY_INFORM __out
12 #define QUERY_FATAL(message...) { fprintf(stderr, message); abort(); }
13 #define QUERY_D(block) block
14 #include <file_systems/QueryParser.h>
15 
16 
17 class Entry;
18 
19 
20 class Query {
21 public:
22 	static	status_t		Create(void* volume, const char* queryString,
23 								uint32 flags, port_id port, uint32 token,
24 								Query*& _query);
25 
26 private:
27 	struct QueryPolicy;
28 	friend struct QueryPolicy;
29 	typedef QueryParser::Query<QueryPolicy> QueryImpl;
30 
31 private:
32 							Query();
33 
34 			status_t		_Init(const char* queryString, uint32 flags,
35 								port_id port, uint32 token);
36 
37 private:
38 			QueryImpl*		fImpl;
39 };
40 
41 
42 struct Query::QueryPolicy {
43 	typedef Query Context;
44 	typedef ::Entry Entry;
45 	typedef ::Entry Node;
46 	typedef void* NodeHolder;
47 
48 	struct Index {
49 		Query*		query;
50 
51 		Index(Context* context)
52 			:
53 			query(context)
54 		{
55 		}
56 	};
57 
58 	struct IndexIterator {
59 		Entry* entry;
60 	};
61 
62 	static const int32 kMaxFileNameLength = B_FILE_NAME_LENGTH;
63 
64 	// Entry interface
65 
66 	static ino_t EntryGetParentID(Entry* entry)
67 	{
68 		return -1;
69 	}
70 
71 	static Node* EntryGetNode(Entry* entry)
72 	{
73 		return entry;
74 	}
75 
76 	static ino_t EntryGetNodeID(Entry* entry)
77 	{
78 		return -1;
79 	}
80 
81 	static ssize_t EntryGetName(Entry* entry, void* buffer, size_t bufferSize)
82 	{
83 		return B_ERROR;
84 	}
85 
86 	static const char* EntryGetNameNoCopy(NodeHolder& holder, Entry* entry)
87 	{
88 		return NULL;
89 	}
90 
91 	// Index interface
92 
93 	static status_t IndexSetTo(Index& index, const char* attribute)
94 	{
95 		return B_ERROR;
96 	}
97 
98 	static void IndexUnset(Index& index)
99 	{
100 	}
101 
102 	static int32 IndexGetWeightedScore(Index& index, int32 score)
103 	{
104 		return 0;
105 	}
106 
107 	static type_code IndexGetType(Index& index)
108 	{
109 		return 0;
110 	}
111 
112 	static int32 IndexGetKeySize(Index& index)
113 	{
114 		return 0;
115 	}
116 
117 	static IndexIterator* IndexCreateIterator(Index& index)
118 	{
119 		return NULL;
120 	}
121 
122 	// IndexIterator interface
123 
124 	static void IndexIteratorDelete(IndexIterator* indexIterator)
125 	{
126 		delete indexIterator;
127 	}
128 
129 	static status_t IndexIteratorFind(IndexIterator* indexIterator,
130 		const void* value, size_t size)
131 	{
132 		return B_ERROR;
133 	}
134 
135 	static status_t IndexIteratorFetchNextEntry(IndexIterator* indexIterator,
136 		void* value, size_t* _valueLength, size_t bufferSize, size_t* duplicate)
137 	{
138 		return B_ERROR;
139 	}
140 
141 	static status_t IndexIteratorGetEntry(Context* context, IndexIterator* indexIterator,
142 		NodeHolder& holder, Entry** _entry)
143 	{
144 		*_entry = indexIterator->entry;
145 		return B_OK;
146 	}
147 
148 	static void IndexIteratorSkipDuplicates(IndexIterator* indexIterator)
149 	{
150 	}
151 
152 	static void IndexIteratorSuspend(IndexIterator* indexIterator)
153 	{
154 	}
155 
156 	static void IndexIteratorResume(IndexIterator* indexIterator)
157 	{
158 	}
159 
160 	// Node interface
161 
162 	static const off_t NodeGetSize(Node* node)
163 	{
164 		return 0;
165 	}
166 
167 	static time_t NodeGetLastModifiedTime(Node* node)
168 	{
169 		return 0;
170 	}
171 
172 	static status_t NodeGetAttribute(NodeHolder& nodeHolder, Node* node,
173 		const char* attribute, void* buffer, size_t* _size, int32* _type)
174 	{
175 		return B_ERROR;
176 	}
177 
178 	static Entry* NodeGetFirstReferrer(Node* node)
179 	{
180 		return node;
181 	}
182 
183 	static Entry* NodeGetNextReferrer(Node* node, Entry* entry)
184 	{
185 		return NULL;
186 	}
187 
188 	// Volume interface
189 
190 	static dev_t ContextGetVolumeID(Context* context)
191 	{
192 		return 0;
193 	}
194 };
195 
196 
197 /*static*/ status_t
198 Query::Create(void* volume, const char* queryString, uint32 flags,
199 	port_id port, uint32 token, Query*& _query)
200 {
201 	Query* query = new(std::nothrow) Query();
202 	if (query == NULL)
203 		return B_NO_MEMORY;
204 
205 	status_t error = query->_Init(queryString, flags, port, token);
206 	if (error != B_OK) {
207 		delete query;
208 		return error;
209 	}
210 
211 	_query = query;
212 	return B_OK;
213 }
214 
215 
216 Query::Query()
217 	:
218 	fImpl(NULL)
219 {
220 }
221 
222 
223 status_t
224 Query::_Init(const char* queryString, uint32 flags, port_id port, uint32 token)
225 {
226 	status_t error = QueryImpl::Create(this, queryString, flags, port, token,
227 		fImpl);
228 	if (error != B_OK)
229 		return error;
230 
231 	return B_OK;
232 }
233 
234 
235 int
236 main(int argc, char* argv[])
237 {
238 	for (int i = 1; i < argc; i++) {
239 		Query* query;
240 		status_t error = Query::Create(NULL, argv[i], 0, 0, 0, query);
241 		if (error != B_OK) {
242 			fprintf(stderr, "Error creating query %d: %s\n", i - 1, strerror(error));
243 			continue;
244 		}
245 		delete query;
246 	}
247 
248 	return 0;
249 }
250