| 1 |
#include "TodoDB.h" |
|---|
| 2 |
#include "support.h" |
|---|
| 3 |
#include "config.h" |
|---|
| 4 |
#include "todorl.h" |
|---|
| 5 |
|
|---|
| 6 |
void joinArgs(TodoDB &todo, vector<string> const &args, int argc, char const **argv) { |
|---|
| 7 |
char const *av[args.size() + argc]; |
|---|
| 8 |
int arg = 0; |
|---|
| 9 |
|
|---|
| 10 |
av[0] = argv[arg++]; |
|---|
| 11 |
for (vector<string>::const_iterator i = args.begin(); i != args.end(); i++) |
|---|
| 12 |
av[arg++] = (*i).c_str(); |
|---|
| 13 |
for (int i = 1; i < argc; i++) |
|---|
| 14 |
av[arg++] = argv[i]; |
|---|
| 15 |
parseArgs(todo, arg, av); |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
int main(int argc, char const **argv) { |
|---|
| 19 |
// Initialise readline |
|---|
| 20 |
rl_readline_name = const_cast<char*>(PACKAGE); |
|---|
| 21 |
rl_initialize(); |
|---|
| 22 |
|
|---|
| 23 |
string database; |
|---|
| 24 |
|
|---|
| 25 |
try { |
|---|
| 26 |
TodoDB todo; |
|---|
| 27 |
|
|---|
| 28 |
// Combine arguments from RC and command line |
|---|
| 29 |
vector<string> rcargs = parseRC(); |
|---|
| 30 |
joinArgs(todo, rcargs, argc, argv); |
|---|
| 31 |
|
|---|
| 32 |
// Default database to use |
|---|
| 33 |
database = options.database; |
|---|
| 34 |
|
|---|
| 35 |
// Load todo db |
|---|
| 36 |
try { |
|---|
| 37 |
// Switch to global database |
|---|
| 38 |
if (options.global) { |
|---|
| 39 |
database = options.globaldatabase; |
|---|
| 40 |
if (options.verbose > 1) |
|---|
| 41 |
cout << "todo: switched to global database '" << database << "'" << endl; |
|---|
| 42 |
} |
|---|
| 43 |
if (options.verbose > 1) |
|---|
| 44 |
cout << "todo: attempting load of '" << database << "'" << endl; |
|---|
| 45 |
todo.load(database); |
|---|
| 46 |
} catch (TodoDB::quit &e) { |
|---|
| 47 |
// Quit is thrown if the .todo file can't be accessed |
|---|
| 48 |
if (options.verbose > 1) |
|---|
| 49 |
cout << "todo: no database found, continuing" << endl; |
|---|
| 50 |
} catch (...) { |
|---|
| 51 |
throw; |
|---|
| 52 |
} |
|---|
| 53 |
// perform the relevant action |
|---|
| 54 |
todo(options.mode); |
|---|
| 55 |
// save if modified (ie. not just viewing) |
|---|
| 56 |
if (options.mode != TodoDB::View) { |
|---|
| 57 |
todo.save(database); |
|---|
| 58 |
if (options.verbose > 1) |
|---|
| 59 |
cout << "todo: saved database to '" << database << "'" << endl; |
|---|
| 60 |
} |
|---|
| 61 |
} catch (exception &e) { |
|---|
| 62 |
cerr << "todo: error, " << e.what() << endl; |
|---|
| 63 |
return 1; |
|---|
| 64 |
} |
|---|
| 65 |
return 0; |
|---|
| 66 |
} |
|---|