5 #include <readline/history.h>
6 #include <readline/readline.h>
10 MArray
tokenize(char *string
, char *delimiter
) {
11 MArray tokens
= myarray_new(5, 5, sizeof(char *));
15 while ((temp
= strstr(string
, delimiter
)) != NULL
) {
16 substring
= malloc((temp
- string
+ 1) * sizeof(char));
17 strncpy(substring
, string
, temp
-string
);
18 substring
[temp
-string
] = '\0';
19 myarray_append(tokens
, substring
);
20 string
= temp
+ strlen(delimiter
);
24 substring
= malloc((strlen(string
) + 1) * sizeof(char));
25 strcpy(substring
, string
);
26 myarray_append(tokens
, substring
);
32 void free_tokens(MArray tokens
) {
34 for (i
= 0; i
< tokens
->len
; i
++) {
35 free(myarray_get(tokens
, char *, i
));
41 int command_prompt(sqlite3
*db
) {
46 line
= readline("todo> ");
49 if (!line
|| !strlen(line
)) {
54 MArray tokens
= tokenize(line
, " ");
55 token
= myarray_get(tokens
, char *, 0);
57 if (!strcmp(token
, "quit")) {
58 retVal
= quit(db
, tokens
);
59 } else if (!strcmp(token
, "list")) {
60 retVal
= list(db
, tokens
);
61 } else if (!strcmp(token
, "help")) {
62 retVal
= help(db
, tokens
);
63 } else if (!strcmp(token
, "show")) {
64 retVal
= show(db
, tokens
);
65 } else if (!strcmp(token
, "del")) {
66 retVal
= del(db
, tokens
);
67 } else if (!strcmp(token
, "add")) {
68 retVal
= add(db
, tokens
);
70 printf("Unknown command %s\n", token
);
71 remove_history(where_history());
80 int main(int argc
, char **argv
) {
87 retVal
= sqlite3_open_v2("todo.db", &db
, SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
, NULL
);
89 if (retVal
!= SQLITE_OK
) {
90 printf("Couldn't open the database file: %s\n", sqlite3_errmsg(db
));
93 retVal
= sqlite3_exec(db
,
94 "CREATE TABLE IF NOT EXISTS todo \
95 (key INTEGER PRIMARY KEY ASC ON CONFLICT ABORT AUTOINCREMENT,\
98 dueDate INTEGER DEFAULT NULL,\
100 parent INTEGER REFERENCES todo (key));"
101 , NULL
, NULL
, &errmsg
);
103 if (retVal
!= SQLITE_OK
) {
104 printf("Couldn't create the table for this application: %s\n", errmsg
);
107 sqlite3_free(errmsg
);
109 while (!command_prompt(db
)) {};