1 | #include <stdio.h>
|
---|
2 |
|
---|
3 | #include <File.h>
|
---|
4 | #include <Path.h>
|
---|
5 | #include <Query.h>
|
---|
6 | #include <String.h>
|
---|
7 | #include <Volume.h>
|
---|
8 | #include <VolumeRoster.h>
|
---|
9 |
|
---|
10 |
|
---|
11 | int main(int, char **)
|
---|
12 | {
|
---|
13 | BPath filePath("/boot/home", "querytest\".cpp");
|
---|
14 | status_t error = filePath.InitCheck();
|
---|
15 | if (error != B_OK) {
|
---|
16 | fprintf(stderr, "Failed to initialize path: %s\n", strerror(error));
|
---|
17 | return error;
|
---|
18 | }
|
---|
19 |
|
---|
20 | BFile file(filePath.Path(), B_CREATE_FILE | B_WRITE_ONLY);
|
---|
21 |
|
---|
22 | error = file.InitCheck();
|
---|
23 | if (error != B_OK) {
|
---|
24 | fprintf(stderr, "Failed to create test file: %s\n", strerror(error));
|
---|
25 | return error;
|
---|
26 | }
|
---|
27 |
|
---|
28 | BVolumeRoster roster;
|
---|
29 | BVolume volume;
|
---|
30 | while (roster.GetNextVolume(&volume) == B_OK) {
|
---|
31 | if (!volume.KnowsQuery())
|
---|
32 | continue;
|
---|
33 |
|
---|
34 | char volumeName[B_FILE_NAME_LENGTH];
|
---|
35 | volume.GetName(volumeName);
|
---|
36 | printf("Evaluating volume: %s\n", volumeName);
|
---|
37 |
|
---|
38 | BQuery query;
|
---|
39 | error = query.SetVolume(&volume);
|
---|
40 | if (error != B_OK) {
|
---|
41 | fprintf(stderr, "Failed to set query volume: %s\n",
|
---|
42 | strerror(error));
|
---|
43 | return error;
|
---|
44 | }
|
---|
45 |
|
---|
46 | BString predicate;
|
---|
47 | predicate.SetToFormat("name==%s", filePath.Leaf());
|
---|
48 | error = query.SetPredicate(predicate);
|
---|
49 | if (error != B_OK) {
|
---|
50 | fprintf(stderr, "Failed to set explicit predicate: %s\n",
|
---|
51 | strerror(error));
|
---|
52 | return error;
|
---|
53 | }
|
---|
54 |
|
---|
55 | error = query.Fetch();
|
---|
56 | if (error != B_OK) {
|
---|
57 | fprintf(stderr, "Failed to fetch explicit query results: %s\n",
|
---|
58 | strerror(error));
|
---|
59 | return error;
|
---|
60 | }
|
---|
61 |
|
---|
62 | int32 count = 0;
|
---|
63 | entry_ref ref;
|
---|
64 | for (; query.GetNextRef(&ref) == B_OK; count++);
|
---|
65 |
|
---|
66 | printf("Explicit predicate '%s' returned %" B_PRId32 " entries.\n",
|
---|
67 | predicate.String(), count);
|
---|
68 |
|
---|
69 | query.Clear();
|
---|
70 | query.SetVolume(&volume);
|
---|
71 | query.PushAttr("name");
|
---|
72 | query.PushString(filePath.Leaf());
|
---|
73 | query.PushOp(B_EQ);
|
---|
74 |
|
---|
75 | error = query.GetPredicate(&predicate);
|
---|
76 | if (error != B_OK) {
|
---|
77 | fprintf(stderr, "Failed to get RPN predicate: %s\n",
|
---|
78 | strerror(error));
|
---|
79 | return error;
|
---|
80 | }
|
---|
81 |
|
---|
82 | printf("RPN-generated predicate: %s\n", predicate.String());
|
---|
83 |
|
---|
84 | error = query.Fetch();
|
---|
85 | if (error != B_OK) {
|
---|
86 | fprintf(stderr, "Failed to fetch RPN query results: %s\n",
|
---|
87 | strerror(error));
|
---|
88 | return error;
|
---|
89 | }
|
---|
90 |
|
---|
91 | count = 0;
|
---|
92 | for (; query.GetNextRef(&ref) == B_OK; count++);
|
---|
93 |
|
---|
94 | printf("RPN predicate returned %" B_PRId32 " entries.\n\n", count);
|
---|
95 | query.Clear();
|
---|
96 | }
|
---|
97 |
|
---|
98 | return 0;
|
---|
99 | }
|
---|