ARB
arb_write_tree_comment.cxx
Go to the documentation of this file.
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 
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 
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 
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) {
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];
91  }
92  }
93  if (!error) {
94  if (!nonSwitches) error = "Missing argument 'textToAppend'";
95  else {
96  text = nonSwitch[0];
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 }
const char * GB_ERROR
Definition: arb_core.h:25
GBDATA * GB_open(const char *path, const char *opent)
Definition: ad_load.cxx:1363
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
#define EXIT_SUCCESS
Definition: arb_a2ps.c:154
const char * dbsavename
int main(int argc, char **argv)
GB_ERROR scan(int argc, char **argv)
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
fflush(stdout)
static void error_with_usage(GB_ERROR error)
#define true
Definition: ureadseq.h:14
GB_ERROR GB_save_as(GBDATA *gbd, const char *path, const char *savetype)
#define SHIFT_NONSWITCHES(off)
static void error(const char *msg)
Definition: mkptypes.cxx:96
static void show_error(GB_ERROR error)
#define EXIT_FAILURE
Definition: arb_a2ps.c:157
fputs(TRACE_PREFIX, stderr)
static GBDATA * gb_msg_main
void GBT_message(GBDATA *gb_main, const char *msg)
Definition: adtools.cxx:238
GB_ERROR GBT_log_to_named_trees_remark(GBDATA *gb_main, const char *tree_name, const char *log_entry, bool stamp)
Definition: adtree.cxx:556
#define NULp
Definition: cxxforward.h:116
const char * tree_name
const char * dbname
GBDATA * gb_main
Definition: adname.cxx:32
#define SHIFT_ARGS(off)
static void show_message(const char *msg)
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:655