1 /* Copyright (C) 2010-2017 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (libretrodb_tool.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <string/stdstring.h>
27
28 #include "libretrodb.h"
29 #include "rmsgpack_dom.h"
30
main(int argc,char ** argv)31 int main(int argc, char ** argv)
32 {
33 int rv;
34 struct rmsgpack_dom_value item;
35 const char *command = NULL;
36 const char *path = NULL;
37 const char *query_exp = NULL;
38 const char *error = NULL;
39 libretrodb_t *db = NULL;
40 libretrodb_cursor_t *cur = NULL;
41 libretrodb_query_t *q = NULL;
42
43 if (argc < 3)
44 {
45 printf("Usage: %s <db file> <command> [extra args...]\n", argv[0]);
46 printf("Available Commands:\n");
47 printf("\tlist\n");
48 printf("\tcreate-index <index name> <field name>\n");
49 printf("\tfind <query expression>\n");
50 printf("\tget-names <query expression>\n");
51 return 1;
52 }
53
54 command = argv[2];
55 path = argv[1];
56
57 db = libretrodb_new();
58 cur = libretrodb_cursor_new();
59
60 if (!db || !cur)
61 goto error;
62
63 if ((rv = libretrodb_open(path, db)) != 0)
64 {
65 printf("Could not open db file '%s': %s\n", path, strerror(-rv));
66 goto error;
67 }
68 else if (memcmp(command, "list", 4) == 0)
69 {
70 if ((rv = libretrodb_cursor_open(db, cur, NULL)) != 0)
71 {
72 printf("Could not open cursor: %s\n", strerror(-rv));
73 goto error;
74 }
75
76 if (argc != 3)
77 {
78 printf("Usage: %s <db file> list\n", argv[0]);
79 goto error;
80 }
81
82 while (libretrodb_cursor_read_item(cur, &item) == 0)
83 {
84 rmsgpack_dom_value_print(&item);
85 printf("\n");
86 rmsgpack_dom_value_free(&item);
87 }
88 }
89 else if (memcmp(command, "find", 4) == 0)
90 {
91 if (argc != 4)
92 {
93 printf("Usage: %s <db file> find <query expression>\n", argv[0]);
94 goto error;
95 }
96
97 query_exp = argv[3];
98 error = NULL;
99 q = libretrodb_query_compile(db, query_exp, strlen(query_exp), &error);
100
101 if (error)
102 {
103 printf("%s\n", error);
104 goto error;
105 }
106
107 if ((rv = libretrodb_cursor_open(db, cur, q)) != 0)
108 {
109 printf("Could not open cursor: %s\n", strerror(-rv));
110 goto error;
111 }
112
113 while (libretrodb_cursor_read_item(cur, &item) == 0)
114 {
115 rmsgpack_dom_value_print(&item);
116 printf("\n");
117 rmsgpack_dom_value_free(&item);
118 }
119 }
120 else if (memcmp(command, "get-names", 9) == 0)
121 {
122 if (argc != 4)
123 {
124 printf("Usage: %s <db file> get-names <query expression>\n", argv[0]);
125 goto error;
126 }
127
128 query_exp = argv[3];
129 error = NULL;
130 q = libretrodb_query_compile(db, query_exp, strlen(query_exp), &error);
131
132 if (error)
133 {
134 printf("%s\n", error);
135 goto error;
136 }
137
138 if ((rv = libretrodb_cursor_open(db, cur, q)) != 0)
139 {
140 printf("Could not open cursor: %s\n", strerror(-rv));
141 goto error;
142 }
143
144 while (libretrodb_cursor_read_item(cur, &item) == 0)
145 {
146 if (item.type == RDT_MAP) //should always be true, but if false the program would segfault
147 {
148 unsigned i;
149 for (i = 0; i < item.val.map.len; i++)
150 {
151 if (item.val.map.items[i].key.type == RDT_STRING && (strncmp(item.val.map.items[i].key.val.string.buff, "name", item.val.map.items[i].key.val.string.len) == 0))
152 {
153 rmsgpack_dom_value_print(&item.val.map.items[i].value);
154 printf("\n");
155 }
156 }
157 }
158
159 rmsgpack_dom_value_free(&item);
160 }
161 }
162 else if (memcmp(command, "create-index", 12) == 0)
163 {
164 const char * index_name, * field_name;
165
166 if (argc != 5)
167 {
168 printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
169 goto error;
170 }
171
172 index_name = argv[3];
173 field_name = argv[4];
174
175 libretrodb_create_index(db, index_name, field_name);
176 }
177 else
178 {
179 printf("Unknown command %s\n", argv[2]);
180 goto error;
181 }
182
183 libretrodb_cursor_close(cur);
184 libretrodb_close(db);
185
186 error:
187 if (db)
188 libretrodb_free(db);
189 if (cur)
190 libretrodb_cursor_free(cur);
191 if (q)
192 libretrodb_query_free(q);
193 return 1;
194 }
195