1 | // ================================================================ // |
---|
2 | // // |
---|
3 | // File : arb_write_tree_comment.cxx // |
---|
4 | // Purpose : append text to comment of existing tree // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in December 2016 // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ================================================================ // |
---|
10 | |
---|
11 | #include <arbdbt.h> |
---|
12 | |
---|
13 | static GBDATA *gb_msg_main = NULp; |
---|
14 | |
---|
15 | static void show_message(const char *msg) { |
---|
16 | if (gb_msg_main) { |
---|
17 | GBT_message(gb_msg_main, msg); |
---|
18 | } |
---|
19 | else { |
---|
20 | fflush(stdout); |
---|
21 | printf("arb_write_tree_comment: %s\n", msg); |
---|
22 | } |
---|
23 | } |
---|
24 | static void show_error(GB_ERROR error) { |
---|
25 | if (error) show_message(GBS_global_string("Error running arb_write_tree_comment (%s)", error)); |
---|
26 | } |
---|
27 | |
---|
28 | static void error_with_usage(GB_ERROR error) { |
---|
29 | fputs("Usage: arb_write_tree_comment [options] treeName textToAppend\n" |
---|
30 | "Purpose: appends 'textToAppend' to comment of existing tree 'treeName'\n" |
---|
31 | "Available options:\n" |
---|
32 | " -db database savename specify database and savename (default is 'running ARB')\n" |
---|
33 | " -plain do NOT prefix timestamp before textToAppend\n" |
---|
34 | , stdout); |
---|
35 | |
---|
36 | show_error(error); |
---|
37 | } |
---|
38 | |
---|
39 | struct parameters { |
---|
40 | const char *dbname; |
---|
41 | const char *dbsavename; |
---|
42 | const char *tree_name; |
---|
43 | const char *text; |
---|
44 | |
---|
45 | bool stamp; |
---|
46 | |
---|
47 | parameters() |
---|
48 | : dbname(":"), |
---|
49 | dbsavename(NULp), |
---|
50 | tree_name(NULp), |
---|
51 | text(NULp), |
---|
52 | stamp(true) |
---|
53 | {} |
---|
54 | |
---|
55 | #define SHIFT_ARGS(off) do { argc -= off; argv += off; } while (0) |
---|
56 | #define SHIFT_NONSWITCHES(off) do { nonSwitches -= off; nonSwitch += off; } while (0) |
---|
57 | |
---|
58 | GB_ERROR scan(int argc, char **argv) { |
---|
59 | GB_ERROR error = NULp; |
---|
60 | |
---|
61 | const char *nonSwitch_buf[20]; |
---|
62 | const char **nonSwitch = nonSwitch_buf; |
---|
63 | int nonSwitches = 0; |
---|
64 | |
---|
65 | SHIFT_ARGS(1); // position onto first argument |
---|
66 | |
---|
67 | while (argc>0 && !error) { |
---|
68 | if (strcmp("-db", argv[0]) == 0) { |
---|
69 | if (argc<3) error = "-db expects two arguments (database and savename)"; |
---|
70 | else { |
---|
71 | dbname = argv[1]; |
---|
72 | dbsavename = argv[2]; |
---|
73 | SHIFT_ARGS(3); |
---|
74 | } |
---|
75 | } |
---|
76 | else if (strcmp("-plain", argv[0]) == 0) { |
---|
77 | stamp = false; |
---|
78 | SHIFT_ARGS(1); |
---|
79 | } |
---|
80 | else { |
---|
81 | nonSwitch[nonSwitches++] = argv[0]; |
---|
82 | SHIFT_ARGS(1); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | if (!error) { |
---|
87 | if (!nonSwitches) error = "Missing argument 'treeName'"; |
---|
88 | else { |
---|
89 | tree_name = nonSwitch[0]; |
---|
90 | SHIFT_NONSWITCHES(1); |
---|
91 | } |
---|
92 | } |
---|
93 | if (!error) { |
---|
94 | if (!nonSwitches) error = "Missing argument 'textToAppend'"; |
---|
95 | else { |
---|
96 | text = nonSwitch[0]; |
---|
97 | SHIFT_NONSWITCHES(1); |
---|
98 | } |
---|
99 | } |
---|
100 | if (!error && nonSwitches>0) { |
---|
101 | error = GBS_global_string("unexpected argument(s): %s ..", nonSwitch[0]); |
---|
102 | } |
---|
103 | return error; |
---|
104 | } |
---|
105 | }; |
---|
106 | |
---|
107 | int main(int argc, char **argv) { |
---|
108 | parameters param; |
---|
109 | GB_ERROR error = param.scan(argc, argv); |
---|
110 | |
---|
111 | GBDATA *gb_main = NULp; |
---|
112 | bool connectToArb = strcmp(param.dbname, ":") == 0; |
---|
113 | GB_shell shell; |
---|
114 | |
---|
115 | if (!error || connectToArb) { |
---|
116 | gb_main = GB_open(param.dbname, connectToArb ? "r" : "rw"); |
---|
117 | if (connectToArb) gb_msg_main = gb_main; |
---|
118 | } |
---|
119 | |
---|
120 | if (error) error_with_usage(error); |
---|
121 | else { |
---|
122 | if (!gb_main) { |
---|
123 | if (connectToArb) error = "you have to start an arbdb server first"; |
---|
124 | else error = GBS_global_string("can't open db (Reason: %s)", GB_await_error()); |
---|
125 | } |
---|
126 | |
---|
127 | if (!error) { |
---|
128 | error = GBT_log_to_named_trees_remark(gb_main, param.tree_name, param.text, param.stamp); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | if (gb_main) { |
---|
133 | if (!error && !connectToArb) { |
---|
134 | error = GB_save_as(gb_main, param.dbsavename, "a"); |
---|
135 | if (error) show_error(error); |
---|
136 | } |
---|
137 | GB_close(gb_main); |
---|
138 | } |
---|
139 | |
---|
140 | return error ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
141 | } |
---|