BRL-CAD
gcv.c
Go to the documentation of this file.
1 #include "bu.h"
2 #include "gcv_private.h"
3 
4 struct gcv_context {
5  struct db_i *db_instance;
6 };
7 
8 struct gcv_opts {
9  void *p;
10 };
11 
12 struct gcv_filter {
13  const char *name;
14  const struct gcv_opts *options;
16  const struct gcv_converter *converter;
17 };
18 
19 
20 int
22 {
23  context->db_instance = db_create_inmem();
24  return 0;
25 }
26 
27 
28 int
30 {
31  db_close(context->db_instance);
32  return 0;
33 }
34 
35 
36 void
37 gcv_reader(struct gcv_filter *reader, const char *source,
38  const struct gcv_opts *opts)
39 {
40  if (!source) source = "-";
41 
42  if (!BU_STR_EQUAL(source, "-")) {
43  bu_log("Scheduling read from %s\n", source);
44  } else {
45  bu_log("Scheduling read from STDIN\n");
46  }
47 
48  reader->name = source;
49  reader->options = opts;
50  reader->type = GCV_CONVERSION_READ;
51  reader->converter = gcv_converter_find(source, reader->type);
52 
53  if (!reader->converter)
54  bu_log("No reader found for '%s'\n", source);
55 }
56 
57 
58 void
59 gcv_writer(struct gcv_filter *writer, const char *target,
60  const struct gcv_opts *opts)
61 {
62  if (!target) target = "-";
63 
64  if (!BU_STR_EQUAL(target, "-")) {
65  bu_log("Scheduling write to %s\n", target);
66  } else {
67  bu_log("Scheduling write to STDOUT\n");
68  }
69 
70  writer->name = target;
71  writer->options = opts;
72  writer->type = GCV_CONVERSION_WRITE;
73  writer->converter = gcv_converter_find(target, writer->type);
74 
75  if (!writer->converter)
76  bu_log("No writer found for '%s'\n", target);
77 }
78 
79 
80 int
81 gcv_execute(struct gcv_context *cxt, const struct gcv_filter *filter)
82 {
83  if (!cxt || !filter)
84  return 0;
85 
86  bu_log("Filtering %s\n", filter->name);
87 
88  if (filter->type == GCV_CONVERSION_READ) {
89  if (!filter->converter || !filter->converter->reader_fn) {
90  bu_log("Filter has no reader\n");
91  return -1;
92  }
93 
94  return !filter->converter->reader_fn(filter->name, cxt->db_instance->dbi_wdbp,
95  filter->options);
96  } else if (filter->type == GCV_CONVERSION_WRITE) {
97  int ret;
98  struct db_i *temp_dbip = db_create_inmem();
99 
100  if (db_dump(temp_dbip->dbi_wdbp, cxt->db_instance)) {
101  bu_log("db_dump() failed\n");
102  db_close(temp_dbip);
103  return -1;
104  }
105 
106  if (!filter->converter || !filter->converter->writer_fn) {
107  bu_log("Filter has no writer\n");
108  db_close(temp_dbip);
109  return -1;
110  }
111 
112  ret = filter->converter->writer_fn(filter->name, temp_dbip, filter->options);
113  db_close(temp_dbip);
114 
115  return !ret;
116  }
117 
118  bu_bomb("Unknown filter type");
119  return -1;
120 }
121 
122 
123 int
124 gcv_convert(const char *in_file, const struct gcv_opts *in_opts,
125  const char *out_file, const struct gcv_opts *out_opts)
126 {
127  struct gcv_context context;
128  struct gcv_filter reader;
129  struct gcv_filter writer;
130  int result;
131 
132  result = gcv_init(&context);
133 
134  if (result)
135  bu_exit(2, "ERROR: Failed to initialize the GCV context\n");
136 
137  gcv_reader(&reader, in_file, in_opts);
138  gcv_writer(&writer, out_file, out_opts);
139 
140  result = gcv_execute(&context, &reader);
141 
142  if (result)
143  bu_exit(2, "ERROR: Unable to read input data from %s\n", in_file);
144 
145  result = gcv_execute(&context, &writer);
146 
147  if (result)
148  bu_exit(2, "ERROR: Unable to write output data to %s\n", out_file);
149 
150  gcv_destroy(&context);
151 
152  return 0;
153 }
154 
155 
156 int
157 main(int ac, char **av)
158 {
159  if (ac < 3
160  || (ac == 2 && av[1][0] == '-' && av[1][1] == 'h')) {
161  bu_log("Usage: %s [-h] [input[:options]] [output[:options]]\n", av[0]);
162  return 1;
163  }
164 
165  gcv_convert(av[1], NULL, av[2], NULL);
166 
167  return 0;
168 }
gcv_conversion_type
Definition: gcv_private.h:39
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
void gcv_reader(struct gcv_filter *reader, const char *source, const struct gcv_opts *opts)
Definition: gcv.c:37
Definition: gcv.c:4
int gcv_destroy(struct gcv_context *context)
Definition: gcv.c:29
enum gcv_conversion_type type
Definition: gcv.c:15
struct rt_wdb * dbi_wdbp
PRIVATE: ptr back to containing rt_wdb.
Definition: raytrace.h:825
struct db_i * db_create_inmem(void)
Definition: db_inmem.c:93
Definition: gcv.c:8
const char * name
Definition: gcv.c:13
int(* writer_fn)(const char *path, struct db_i *dbip, const struct gcv_opts *options)
Definition: plugin.h:43
int(* reader_fn)(const char *path, struct rt_wdb *wdbp, const struct gcv_opts *options)
Definition: plugin.h:41
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
int db_dump(struct rt_wdb *wdbp, struct db_i *dbip)
Definition: db_open.c:384
struct db_i * db_instance
Definition: gcv.c:5
void db_close(struct db_i *dbip)
int gcv_convert(const char *in_file, const struct gcv_opts *in_opts, const char *out_file, const struct gcv_opts *out_opts)
Definition: gcv.c:124
const struct gcv_converter * converter
Definition: gcv.c:16
Definition: gcv.c:12
int gcv_init(struct gcv_context *context)
Definition: gcv.c:21
const struct gcv_opts * options
Definition: gcv.c:14
int gcv_execute(struct gcv_context *cxt, const struct gcv_filter *filter)
Definition: gcv.c:81
void bu_bomb(const char *str) _BU_ATTR_NORETURN
Definition: bomb.c:91
const struct gcv_converter * gcv_converter_find(const char *path, enum gcv_conversion_type type)
Definition: plugin.c:98
int main(int ac, char **av)
Definition: gcv.c:157
void * p
Definition: gcv.c:9
void gcv_writer(struct gcv_filter *writer, const char *target, const struct gcv_opts *opts)
Definition: gcv.c:59
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126