-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.c
355 lines (289 loc) · 11.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright 2018-2020 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "objectbox-model.h"
#include "objectbox.h"
#include "tasklist.obx.h"
#if !defined(_MSC_VER)
#include <libgen.h>
#endif
#define DATE_FORMAT_STRING "%Y-%m-%d %H:%M:%S"
#define DATE_BUFFER_LENGTH 100
// List of action codes returned by parse_action
#define ACTION_NEW 1
#define ACTION_DONE 2
#define ACTION_LIST_UNFINISHED 3
#define ACTION_LIST_ALL 4
#define ACTION_HELP 9
// Utility functions
int parse_action(int argc, char* argv[]);
int parse_text(int argc, char** argv, char** outText);
uint64_t timestamp_now();
void date_to_str(char* buff, uint64_t timestamp);
// functions to handle each requested action
void do_action_help(char* program_path);
int do_action_new(OBX_box* box, int argc, char* argv[]);
int do_action_done(OBX_box* box, int argc, char* argv[]);
int do_action_list(OBX_box* box, OBX_query* query);
//--------------------------------------------------------------------------------------------------------------------
// main - parse the action from the command line. If required, open an ObjectBox store,
// call the corresponding action function and then clean up the store, printing out
// the error code, if an error has occurred
//--------------------------------------------------------------------------------------------------------------------
OBX_store* store_open();
int main(int argc, char* argv[]) {
obx_err rc = 0;
// print version of ObjectBox in use
printf("Using libobjectbox version %s, core version: %s\n", obx_version_string(), obx_version_core_string());
// determine requested action
int action = parse_action(argc, argv);
// early out if we're just printing the usage
if (action == ACTION_HELP) {
do_action_help(argv[0]);
return rc;
}
// An OBX_store represents the database, so let's open one
OBX_store* store = store_open();
if (store == NULL) {
printf("Could not open store: %s (%d)\n", obx_last_error_message(), obx_last_error_code());
return 1;
}
OBX_box* task_box = obx_box(store, Task_ENTITY_ID);
switch (action) {
case ACTION_NEW:
rc = do_action_new(task_box, argc, argv);
break;
case ACTION_DONE:
rc = do_action_done(task_box, argc, argv);
break;
case ACTION_LIST_UNFINISHED: {
OBX_query_builder* qb = obx_query_builder(store, Task_ENTITY_ID);
if (!qb) {
rc = obx_last_error_code();
break;
}
obx_qb_equals_int(qb, Task_PROP_ID_date_finished, 0);
OBX_query* query = obx_query(qb);
obx_qb_close(qb);
if (!query) {
rc = obx_last_error_code();
break;
}
rc = do_action_list(task_box, query);
obx_query_close(query);
break;
}
case ACTION_LIST_ALL:
rc = do_action_list(task_box, false);
break;
default:
rc = 42;
printf("Internal error - requested action not handled\n");
break;
}
if (rc != 0) {
printf("Last error: %s (%d)\n", obx_last_error_message(), obx_last_error_code());
}
obx_store_close(store);
return rc;
}
//--------------------------------------------------------------------------------------------------------------------
// Opening a store. The store requires a model that describes the schema of the database.
//--------------------------------------------------------------------------------------------------------------------
OBX_store* store_open() {
// Firstly, create our model
OBX_model* model = create_obx_model();
if (!model) {
return NULL;
}
// As we're not doing anything fancy here, we'll just use the default options...
OBX_store_options* opt = obx_opt();
// ...but we need to set our model in the options for the store.
obx_opt_model(opt, model);
// And open the store. Note that the model is freed by obx_store_open(), even
// in the case of failure, so we don't need to free it here
return obx_store_open(opt);
}
//--------------------------------------------------------------------------------------------------------------------
// do_action_help - print out the expected usage of the example
//--------------------------------------------------------------------------------------------------------------------
void do_action_help(char* program_path) {
#ifndef _MSC_VER // Windows is not UNIX
program_path = basename(program_path);
#endif
printf("usage: %s\n", program_path);
printf(" %-30s %s\n", "text of a new task", "create a new task with the given text");
printf(" %-30s %s\n", "", "(default) lists active tasks");
printf(" %-30s %s\n", "--list", "lists active and done tasks");
printf(" %-30s %s\n", "--done ID", "marks the task with the given ID as done");
printf(" %-30s %s\n", "--help", "displays this help");
}
//--------------------------------------------------------------------------------------------------------------------
// do_action_new - create a new task entity and add it to the database
//--------------------------------------------------------------------------------------------------------------------
int do_action_new(OBX_box* box, int argc, char* argv[]) {
Task task = {0};
// grab the task text from the command line
if (parse_text(argc, argv, &task.text) <= 0) {
return -1;
}
task.date_created = timestamp_now();
obx_id id = Task_put(box, &task);
if (id != 0) {
printf("New task: %" PRIu64 " - %s\n", id, task.text);
} else {
printf("Failed to create the task\n");
}
free(task.text); // malloc() by parse_text()
return id == 0 ? obx_last_error_code() : 0;
}
//--------------------------------------------------------------------------------------------------------------------
// do_action_done - mark an extant task entity as done
//--------------------------------------------------------------------------------------------------------------------
int do_action_done(OBX_box* box, int argc, char* argv[]) {
// grab the id from the command line
assert(argc == 2);
obx_id id = (obx_id) atol(argv[2]);
if (!id) {
printf("Error parsing ID \"%s\" as a number\n", argv[2]);
return -1;
}
Task* task = Task_get(box, id);
// First, we read the entity back from the cursor
if (task == NULL) {
printf("Task %ld not found\n", (long) id);
return 1;
}
obx_err rc = 0;
if (task->date_finished != 0) {
printf("Task %ld has already been done\n", (long) id);
} else {
// It's not been marked done. Rebuild the entity, marked as done, and use the cursor to overwrite it
printf("Setting task %ld as done\n", (long) id);
task->date_finished = timestamp_now();
if (!Task_put(box, task)) {
printf("Failed to mark the task as done\n");
rc = obx_last_error_code();
}
}
Task_free(task);
return rc;
}
//--------------------------------------------------------------------------------------------------------------------
// do_action_list - list all the task entities, open or done, depending on list_open
//--------------------------------------------------------------------------------------------------------------------
int do_action_list(OBX_box* box, OBX_query* query) {
OBX_txn* txn = NULL;
OBX_bytes_array* list = NULL;
obx_err rc = 0;
// Note that this time we are using a read transaction
txn = obx_txn_read(obx_box_store(box));
if (!txn) {
printf("Failed to start a transaction\n");
rc = obx_last_error_code();
goto clean_up;
}
if (query) {
list = obx_query_find(query);
} else {
list = obx_box_get_all(box);
}
if (!list) {
printf("Failed to list the tasks\n");
rc = obx_last_error_code();
goto clean_up;
}
// A nice header for the table
printf("%3s %-19s %-19s %s\n", "ID", "Created", "Finished", "Text");
char date_created[DATE_BUFFER_LENGTH];
char date_finished[DATE_BUFFER_LENGTH];
size_t i;
for (i = 0; i < list->count; i++) {
Task* task = Task_new_from_flatbuffer(list->bytes[i].data, list->bytes[i].size);
date_to_str(date_created, task->date_created);
date_to_str(date_finished, task->date_finished);
printf("%3" PRIu64 " %-19s %-19s %s\n", task->id, date_created, date_finished, task->text);
Task_free(task);
}
clean_up:
if (list) obx_bytes_array_free(list);
if (txn) obx_txn_close(txn);
return rc;
}
//--------------------------------------------------------------------------------------------------------------------
// Utility functions
//--------------------------------------------------------------------------------------------------------------------
int parse_text(int argc, char** argv, char** outText) {
int size = 0;
int i; // Avoid "‘for’ loop initial declarations are only allowed in C99 or C11 mode" with older compilers
size += argc - 2; // number of spaces between words
for (i = 1; i < argc; i++) {
size += (int) strlen(argv[i]);
}
assert(size >= 0);
if (size == 0) {
printf("No task text given\n");
return -1;
}
*outText = (char*) malloc(sizeof(char) * (unsigned long)(size + 1));
if (!*outText) {
printf("Could not process task text\n");
return -1;
}
char* p = *outText;
for (i = 1; i < argc; i++) {
strcpy(p, argv[i]);
p += strlen(argv[i]);
if (i != argc - 1) strcpy(p++, " ");
}
return size;
}
uint64_t timestamp_now() { return (uint64_t)(time(NULL) * 1000); }
void date_to_str(char* buff, uint64_t timestamp) {
if (!timestamp) {
// empty string
buff[0] = '\0';
} else {
time_t time = (time_t)(timestamp / 1000);
struct tm* tm_info = localtime(&time);
strftime(buff, DATE_BUFFER_LENGTH, DATE_FORMAT_STRING, tm_info);
}
}
obx_err parse_action(int argc, char* argv[]) {
if (argc < 2) {
return ACTION_LIST_UNFINISHED;
}
if (strcmp("--done", argv[1]) == 0) {
if (argc != 3) {
return ACTION_HELP;
} else {
return ACTION_DONE;
}
}
if (strcmp("--list", argv[1]) == 0) {
return ACTION_LIST_ALL;
}
if (strcmp("--help", argv[1]) == 0 || strcmp("--usage", argv[1]) == 0) {
return ACTION_HELP;
}
return ACTION_NEW;
}