4 #include <readline/readline.h>
5 #include <readline/history.h>
9 MArray
tokenize(char *string
, char *delimiter
) {
10 MArray tokens
= myarray_new(5, 5, sizeof(char *));
14 while ((temp
= strstr(string
, delimiter
)) != NULL
) {
15 substring
= malloc((temp
- string
+ 1) * sizeof(char));
16 strncpy(substring
, string
, temp
-string
);
17 substring
[temp
-string
] = '\0';
18 myarray_append(tokens
, substring
);
19 string
= temp
+ strlen(delimiter
);
23 substring
= malloc((strlen(string
) + 1) * sizeof(char));
24 strcpy(substring
, string
);
25 myarray_append(tokens
, substring
);
31 void free_tokens(MArray tokens
) {
33 for (i
= 0; i
< tokens
->len
; i
++) {
34 free(myarray_get(tokens
, char *, i
));
40 int command_prompt(sqlite3
*db
) {
45 line
= readline("todo> ");
48 if (!line
|| !strlen(line
)) {
53 MArray tokens
= tokenize(line
, " ");
54 token
= myarray_get(tokens
, char *, 0);
56 if (!strcmp(token
, "quit")) {
57 retVal
= quit(db
, tokens
);
58 } else if (!strcmp(token
, "list")) {
59 retVal
= list(db
, tokens
);
60 } else if (!strcmp(token
, "help")) {
61 retVal
= help(db
, tokens
);
62 } else if (!strcmp(token
, "show")) {
63 retVal
= show(db
, tokens
);
65 printf("Unknown command %s\n", token
);
66 remove_history(where_history());
75 int main(int argc
, char **argv
) {
80 retVal
= sqlite3_open("todo.db", &db
);
82 if (retVal
!= SQLITE_OK
) {
83 printf("Couldn't open the database file: %s\n", sqlite3_errmsg(db
));
86 retVal
= sqlite3_exec(db
,
87 "CREATE TABLE IF NOT EXISTS todo \
88 (key INTEGER PRIMARY KEY ASC ON CONFLICT ABORT AUTOINCREMENT,\
92 parent INTEGER REFERENCES todo (key));"
93 , NULL
, NULL
, &errmsg
);
95 if (retVal
!= SQLITE_OK
) {
96 printf("Couldn't create the table for this application: %s\n", errmsg
);
101 while (!command_prompt(db
)) {};