ARB
arb_sub2ascii.cxx
Go to the documentation of this file.
1 // ============================================================= //
2 // //
3 // File : arb_sub2ascii.cxx //
4 // Purpose : unittest support tool //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in April 2018 //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================= //
10 
11 #include <arbdbt.h>
12 
13 static int fail_if_error(const char *error) {
14  if (error) {
15  fprintf(stderr, "arb_sub2ascii: Error: %s\n", error);
16  return EXIT_FAILURE;
17  }
18  return EXIT_SUCCESS;
19 }
20 
21 static int show_usage(const char *error) {
22  fputs("arb_sub2ascii - extract part of database into ascii database\n"
23  "(intended to support fine-grained results in unit-tests)\n"
24  "Syntax: arb_sub2ascii 'srcDB' 'path/to/container' 'dstDB'\n"
25  "Opens arb-database 'srcDB', searches a container using the specified\n"
26  "search path and dumps its contents in arb ascii format to 'dstDB'\n",
27  stderr);
28 
29  return fail_if_error(error);
30 }
31 
32 int ARB_main(int argc, char *argv[]) {
34 
35  bool not_enough_args = argc<4;
36  bool help_requested = argc>1 && strcmp(argv[1], "--help") == 0;
37 
38  if (not_enough_args || help_requested) {
39  return show_usage(help_requested ? NULp : "not enough arguments");
40  }
41 
42  const char *srcname = argv[1];
43  const char *path = argv[2];
44  const char *dstname = argv[3];
45 
46  {
47  GB_shell shell;
48 
49  GBDATA *gb_src = GB_open(srcname, "r");
50  GBDATA *gb_dst = NULp;
51 
52  if (!gb_src) error = GB_await_error();
53  else {
54  gb_dst = GB_open(dstname, "cw");
55  if (!gb_dst) error = GB_await_error();
56  }
57 
58  if (!error) {
59  GB_transaction ts(gb_src);
60  GB_transaction td(gb_dst);
61 
62  GBDATA *gb_sub = GB_search(gb_src, path, GB_FIND);
63  if (!gb_sub) {
64  error = GBS_global_string("could not find a container at '%s' in database '%s'", path, srcname);
65  }
66  else {
67  error = GB_copy_dropMarksAndTempstate(gb_dst, gb_sub);
68  }
69  }
70 
71  if (!error) error = GB_save_as(gb_dst, dstname, "a");
72 
73  if (gb_dst) GB_close(gb_dst);
74  if (gb_src) GB_close(gb_src);
75  }
76 
77  return fail_if_error(error);
78 }
79 
const char * GB_ERROR
Definition: arb_core.h:25
GBDATA * GB_open(const char *path, const char *opent)
Definition: ad_load.cxx:1363
int ARB_main(int argc, char *argv[])
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
#define EXIT_SUCCESS
Definition: arb_a2ps.c:154
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
GB_ERROR GB_save_as(GBDATA *gbd, const char *path, const char *savetype)
static void error(const char *msg)
Definition: mkptypes.cxx:96
static int show_usage(const char *error)
Definition: arbdb.h:86
#define EXIT_FAILURE
Definition: arb_a2ps.c:157
fputs(TRACE_PREFIX, stderr)
static int fail_if_error(const char *error)
GB_ERROR GB_copy_dropMarksAndTempstate(GBDATA *dest, GBDATA *source)
Definition: arbdb.cxx:2131
#define NULp
Definition: cxxforward.h:97
GBDATA * GB_search(GBDATA *gbd, const char *fieldpath, GB_TYPES create)
Definition: adquery.cxx:531
void GB_close(GBDATA *gbd)
Definition: arbdb.cxx:625