-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2dxf.cpp
More file actions
208 lines (196 loc) · 6.1 KB
/
csv2dxf.cpp
File metadata and controls
208 lines (196 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//-------------------------------------------------------------------------------------------------
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "fmt/core.h" // https://github.com/fmtlib/fmt
#include "dxflib/dl_dxf.h"
#include "csv.h" // https://github.com/rgamble/libcsv
#include "cvalue.h"
#include "cdxfwriter.h"
#include "options.h"
//-------------------------------------------------------------------------------------------------
#define COLS_LIMIT 5
#define MAX_FIELD_SIZE 20
#define LAYER_POINTS 0
#define LAYER_LABELS 1
//-------------------------------------------------------------------------------------------------
struct Meta
{
CValue *fields[COLS_LIMIT];
unsigned int fields_count = 0;
};
//-------------------------------------------------------------------------------------------------
static int endline_detector(unsigned char c)
{
switch(c)
{
case CSV_CR:
case CSV_LF:
return 1;
}
return 0;
}
//-------------------------------------------------------------------------------------------------
void process_line(size_t line_number, CValue *fields[])
{
if(line_number < options.skip_first_lines)
{
// skip first lines
return;
}
// ========================================================================================= //
static char nocode[] = "(none)";
char fmt[10];
char label[100];
char layer[100];
char *point_name = fields[0]->text();
double N = fields[1]->to_double(options.round_digits);
double E = fields[2]->to_double(options.round_digits);
double Z = fields[3]->to_double(options.round_digits);
char *code = fields[4]->text();
// create a point
if(options.split_by_code)
{
if(*code)
{
dxf.use_layer(dxf.upsert_layer(code));
}
else
{
dxf.use_layer(dxf.upsert_layer(nocode));
}
}
else
{
dxf.use_layer(LAYER_POINTS);
}
dxf.add_point(E, N, Z);
// create a label
if(options.custom_label)
{
std::string result = fmt::format(options.custom_label,
fmt::arg("p", std::string(point_name)),
fmt::arg("c", std::string(code)),
fmt::arg("n", N),
fmt::arg("e", E),
fmt::arg("z", Z));
strcpy(label, result.c_str());
}
else
{
sprintf(fmt, "%%.%df", options.round_digits);
if(*code && !options.split_by_code)
{
sprintf(label, "%s %s %s", code, fields[3]->format(fmt, options.round_digits), point_name);
}
else
{
sprintf(label, "%s %s", fields[3]->format(fmt, options.round_digits), point_name);
}
}
printf("%s\n", label);
if(options.split_labels)
{
if(options.split_by_code)
{
if(*code)
{
sprintf(layer, "%s-labels", code);
}
else
{
sprintf(layer, "%s-labels", nocode);
}
dxf.use_layer(dxf.upsert_layer(layer));
}
else
{
dxf.use_layer(LAYER_LABELS);
}
}
dxf.add_label(E + options.labels_height / 2,
N + options.labels_height / 4,
Z,
label);
}
//-------------------------------------------------------------------------------------------------
void csv_line_handler(int c, void *data)
{
static unsigned int line_pos = 0;
Meta* meta = (Meta*) data;
process_line(line_pos++, meta->fields);
for(size_t i = 0; i < COLS_LIMIT; i++)
{
delete meta->fields[i];
}
meta->fields_count = 0;
}
//-------------------------------------------------------------------------------------------------
void csv_field_handler(void *s, size_t len, void *data)
{
struct Meta* meta = (Meta*) data;
if(meta->fields_count < COLS_LIMIT)
{
meta->fields[meta->fields_count] = new CValue((char*)s, len);
}
meta->fields_count++;
}
//-------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
csv_parser csv_parser;
FILE *csv_file = NULL;
struct Meta meta;
size_t bytes_read = 0;
char buf[4096];
printf("CSV to DXF points converter\n");
// parse cmd options
if(cmd_options(argc, argv))
{
exit(EXIT_FAILURE);
};
// open a CSV
csv_file = fopen(options.csv_file_name, "rb");
if (!csv_file)
{
fprintf(stderr, "Failed to open source file '%s': %s\n", options.csv_file_name, strerror(errno));
exit(errno);
}
// prepare CSV parser
if(csv_init(&csv_parser, options.csv_options) != 0)
{
fprintf(stderr, "Failed to initialize csv parser\n");
exit(EXIT_FAILURE);
}
csv_set_term_func(&csv_parser, endline_detector);
csv_set_delim(&csv_parser, ',');
// prepare default layers
if(options.split_by_code == false)
{
dxf.add_layer("points");
if(options.split_labels)
{
dxf.add_layer("labels");
}
}
// main loop
while((bytes_read = fread(buf, 1, sizeof(buf), csv_file)) > 0)
{
if(csv_parse(&csv_parser, buf, bytes_read, csv_field_handler, csv_line_handler, &meta) != bytes_read)
{
fprintf(stderr, "Error while parsing source file '%s': %s\n", options.csv_file_name, csv_strerror(csv_error(&csv_parser)));
}
}
// close CSV
csv_fini(&csv_parser, csv_field_handler, csv_line_handler, &meta);
csv_free(&csv_parser);
if(csv_file) { fclose(csv_file); }
// write DXF
if(int error = dxf.write(options.dxf_file_name))
{
fprintf(stderr, "Failed to create destination file '%s': %s\n", options.dxf_file_name, strerror(error));
exit(errno);
}
return 0;
}
//-------------------------------------------------------------------------------------------------