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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/*
XCSV - X Character Separated Values (.???)
A hopefully not too feeble attempt at parsing whatever separated values
files into the waypoint structure and back out again. This is a config-
file wrapper around csv_util.c.
Copyright (C) 2002 Alex Mottram (geo_alexm at cox-internet.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include <ctype.h>
#include "defs.h"
#include "csv_util.h"
#if CSVFMTS_ENABLED
#define MYNAME "XCSV"
#define ISSTOKEN(a,b) (strncmp(a,b, strlen(b)) == 0)
static char *styleopt = NULL;
static char *snlenopt = NULL;
static char *snwhiteopt = NULL;
static char *snupperopt = NULL;
static char *snuniqueopt = NULL;
char *prefer_shortnames = NULL;
char *xcsv_urlbase = NULL;
static const char *intstylebuf = NULL;
static
arglist_t xcsv_args[] = {
{"style", &styleopt, "Full path to XCSV style file", NULL,
ARGTYPE_FILE | ARGTYPE_REQUIRED, ARG_NOMINMAX },
{"snlen", &snlenopt, "Max synthesized shortname length", NULL,
ARGTYPE_INT, "1", NULL},
{"snwhite", &snwhiteopt, "Allow whitespace synth. shortnames",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX},
{"snupper", &snupperopt, "UPPERCASE synth. shortnames",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX},
{"snunique", &snuniqueopt, "Make synth. shortnames unique",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX},
{"urlbase", &xcsv_urlbase, "Basename prepended to URL on output",
NULL, ARGTYPE_STRING, ARG_NOMINMAX},
{"prefer_shortnames", &prefer_shortnames,
"Use shortname instead of description",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
ARG_TERMINATOR
};
/* a table of config file constants mapped to chars */
static
char_map_t xcsv_char_table[] = {
{ "COMMA", "," },
{ "COMMASPACE", ", " },
{ "SINGLEQUOTE", "'" },
{ "DOUBLEQUOTE", "\"" },
{ "COLON", ":" },
{ "SEMICOLON", ";" },
{ "NEWLINE", "\n" },
{ "CR", "\n" },
{ "CRNEWLINE", "\r\n" },
{ "TAB", "\t" },
{ "SPACE", " " },
{ "HASH", "#" },
{ "WHITESPACE", "\\w" },
{ "PIPE", "|" },
{ NULL, NULL }
};
void
xcsv_destroy_style(void)
{
queue *elem, *tmp;
field_map_t *fmp;
ogue_t *ogp;
int internal = 0;
/*
* If this xcsv_file struct came from a file we can free it all.
* If not, we can at least free the queue elements.
*/
/* destroy the prologue */
QUEUE_FOR_EACH(&xcsv_file.prologue, elem, tmp) {
ogp = (ogue_t *)elem;
if (ogp->val)
xfree(ogp->val);
if (elem)
xfree(elem);
}
/* destroy the epilogue */
QUEUE_FOR_EACH(&xcsv_file.epilogue, elem, tmp) {
ogp = (ogue_t *)elem;
if (ogp->val)
xfree(ogp->val);
if (elem)
xfree(elem);
}
/* destroy the ifields */
QUEUE_FOR_EACH(&xcsv_file.ifield, elem, tmp) {
fmp = (field_map_t *) elem;
if (fmp->key)
xfree(fmp->key);
if (fmp->val)
xfree(fmp->val);
if (fmp->printfc)
xfree(fmp->printfc);
if (elem)
xfree(elem);
}
/* destroy the ofields, if they are not re-mapped to ifields. */
if (xcsv_file.ofield != &xcsv_file.ifield) {
QUEUE_FOR_EACH(xcsv_file.ofield, elem, tmp) {
fmp = (field_map_t *) elem;
if (fmp->key)
xfree(fmp->key);
if (fmp->val)
xfree(fmp->val);
if (fmp->printfc)
xfree(fmp->printfc);
if (elem)
xfree(elem);
}
if (xcsv_file.ofield)
xfree(xcsv_file.ofield);
}
/* other alloc'd glory */
if (xcsv_file.field_delimiter)
xfree(xcsv_file.field_delimiter);
if (xcsv_file.record_delimiter)
xfree(xcsv_file.record_delimiter);
if (xcsv_file.badchars)
xfree(xcsv_file.badchars);
if (xcsv_file.description)
xfree(xcsv_file.description);
if (xcsv_file.extension)
xfree(xcsv_file.extension);
if (xcsv_file.mkshort_handle)
mkshort_del_handle(&xcsv_file.mkshort_handle);
/* return everything to zeros */
internal = xcsv_file.is_internal;
memset(&xcsv_file, '\0', sizeof(xcsv_file));
xcsv_file.is_internal = internal;
}
const char *
xcsv_get_char_from_constant_table(char *key)
{
char_map_t *cm = xcsv_char_table;
while ((cm->key) && (strcmp(key, cm->key) != 0)) {
cm++;
}
return (cm->chars);
}
static void
xcsv_parse_style_line(const char *sbuff)
{
int i, linecount = 0;
char *s, *p, *sp;
const char *cp;
char *key, *val, *pfc;
/*
* tokens should be parsed longest to shortest, unless something
* requires a previously set value. This way something like
* SHORT and SHORTNAME don't collide.
*/
/* whack off any comments */
if ((p = strchr(sbuff, '#')) != NULL) {
if ((p > sbuff) && p[-1] == '\\') {
memmove(p-1, p, strlen(p));
p[strlen(p)-1] = '\0';
} else {
*p = '\0';
}
}
if (strlen(sbuff)) {
if (ISSTOKEN(sbuff, "FIELD_DELIMITER")) {
sp = csv_stringtrim(&sbuff[16], "\"", 1);
cp = xcsv_get_char_from_constant_table(sp);
if (cp) {
xcsv_file.field_delimiter = xstrdup(cp);
xfree(sp);
}
else
xcsv_file.field_delimiter = sp;
p = csv_stringtrim(xcsv_file.field_delimiter, " ", 0);
/* field delimiters are always bad characters */
if (0 == strcmp(p, "\\w")) {
char *s = xstrappend(xcsv_file.badchars, " \n\r");
if (xcsv_file.badchars) xfree(xcsv_file.badchars);
xcsv_file.badchars = s;
} else {
xcsv_file.badchars = xstrappend(xcsv_file.badchars, p);
}
xfree(p);
} else
if (ISSTOKEN(sbuff, "RECORD_DELIMITER")) {
sp = csv_stringtrim(&sbuff[17], "\"", 1);
cp = xcsv_get_char_from_constant_table(sp);
if (cp) {
xcsv_file.record_delimiter = xstrdup(cp);
xfree(sp);
}
else
xcsv_file.record_delimiter = sp;
p = csv_stringtrim(xcsv_file.record_delimiter, " ", 0);
/* record delimiters are always bad characters */
if (xcsv_file.badchars) {
xcsv_file.badchars = (char *) xrealloc(xcsv_file.badchars,
strlen(xcsv_file.badchars) +
strlen(p) + 1);
} else {
xcsv_file.badchars = (char *) xcalloc(strlen(p) + 1, 1);
}
strcat(xcsv_file.badchars, p);
xfree(p);
} else
if (ISSTOKEN(sbuff, "FORMAT_TYPE")) {
const char *p;
for (p = &sbuff[11]; *p && isspace(*p); p++) {
;
}
if (ISSTOKEN(p, "INTERNAL")) {
xcsv_file.type = ff_type_internal;
}
/* this is almost inconcievable... */
if (ISSTOKEN(p, "SERIAL")) {
xcsv_file.type = ff_type_serial;
}
} else
if (ISSTOKEN(sbuff, "DESCRIPTION")) {
xcsv_file.description = csv_stringtrim(&sbuff[11],"", 0);
} else
if (ISSTOKEN(sbuff, "EXTENSION")) {
xcsv_file.extension = csv_stringtrim(&sbuff[10],"", 0);
} else
if (ISSTOKEN(sbuff, "SHORTLEN")) {
if (xcsv_file.mkshort_handle)
setshort_length(xcsv_file.mkshort_handle, atoi(&sbuff[9]));
} else
if (ISSTOKEN(sbuff, "SHORTWHITE")) {
if (xcsv_file.mkshort_handle)
setshort_whitespace_ok(xcsv_file.mkshort_handle, atoi(&sbuff[12]));
} else
if (ISSTOKEN(sbuff, "BADCHARS")) {
sp = csv_stringtrim(&sbuff[9], "\"", 1);
cp = xcsv_get_char_from_constant_table(sp);
if (cp) {
p = xstrdup(cp);
xfree(sp);
}
else
p = sp;
if (xcsv_file.badchars) {
xcsv_file.badchars = (char *) xrealloc(xcsv_file.badchars,
strlen(xcsv_file.badchars) +
strlen(p) + 1);
} else {
xcsv_file.badchars = (char *) xcalloc(strlen(p) + 1, 1);
}
strcat(xcsv_file.badchars, p);
xfree(p);
} else
if (ISSTOKEN(sbuff, "PROLOGUE")) {
xcsv_prologue_add(xstrdup(&sbuff[9]));
} else
if (ISSTOKEN(sbuff, "EPILOGUE")) {
xcsv_epilogue_add(xstrdup(&sbuff[9]));
} else
if (ISSTOKEN(sbuff, "ENCODING")) {
p = csv_stringtrim(&sbuff[8], "\"", 1);
cet_convert_init(p, 1);
xfree(p);
} else
if (ISSTOKEN(sbuff, "IFIELD")) {
key = val = pfc = NULL;
s = csv_lineparse(&sbuff[6], ",", "", linecount);
i = 0;
while (s) {
switch(i) {
case 0:
/* key */
key = csv_stringtrim(s, "\"", 1);
break;
case 1:
/* default value */
val = csv_stringtrim(s, "\"", 1);
break;
case 2:
/* printf conversion */
pfc = csv_stringtrim(s, "\"", 1);
break;
default:
break;
}
i++;
s = csv_lineparse(NULL, ",", "", linecount);
}
xcsv_ifield_add(key, val, pfc);
} else
/*
* as OFIELDs are implemented as an after-thought, I'll
* leave this as it's own parsing for now. We could
* change the world on ifield vs ofield format later..
*/
if (ISSTOKEN(sbuff, "OFIELD")) {
int options = 0;
key = val = pfc = NULL;
s = csv_lineparse(&sbuff[6], ",", "", linecount);
i = 0;
while (s) {
switch(i) {
case 0:
/* key */
key = csv_stringtrim(s, "\"", 1);
break;
case 1:
/* default value */
val = csv_stringtrim(s, "\"", 1);
break;
case 2:
/* printf conversion */
pfc = csv_stringtrim(s, "\"", 1);
break;
case 3:
/* Any additional options. */
if (strstr(s, "no_delim_before")) {
options |= OPTIONS_NODELIM;
}
if (strstr(s, "absolute")) {
options |= OPTIONS_ABSOLUTE;
}
if (strstr(s, "optional")) {
options |= OPTIONS_OPTIONAL;
}
default:
break;
}
i++;
s = csv_lineparse(NULL, ",", "", linecount);
}
xcsv_ofield_add(key, val, pfc, options);
}
}
}
/*
* A wrapper for xcsv_parse_style_line that reads until it hits
* a terminating null. Makes multiple calls to that function so
* that "ignore to end of line" comments work right.
*/
static void
xcsv_parse_style_buff(const char *sbuff)
{
char ibuf[256];
char *ibufp;
size_t i;
while (*sbuff) {
ibuf[0] = 0;
i = 0;
for (ibufp = ibuf; *sbuff != '\n' && i++ < sizeof(ibuf); ) {
*ibufp++ = *sbuff++;
}
while (*sbuff == '\n' || *sbuff == '\r')
sbuff++;
*ibufp = 0;
xcsv_parse_style_line(ibuf);
}
}
static void
xcsv_read_style(const char *fname)
{
char *sbuff;
gbfile *fp;
xcsv_file_init();
fp = gbfopen(fname, "rb", MYNAME);
while ((sbuff = gbfgetstr(fp))) {
sbuff = lrtrim(sbuff);
xcsv_parse_style_line(sbuff);
} while (!gbfeof(fp));
/* if we have no output fields, use input fields as output fields */
if (xcsv_file.ofield_ct == 0) {
if (xcsv_file.ofield)
xfree(xcsv_file.ofield);
xcsv_file.ofield = &xcsv_file.ifield;
xcsv_file.ofield_ct = xcsv_file.ifield_ct;
}
gbfclose(fp);
}
/*
* Passed a pointer to an internal buffer that would be identical
* to the series of bytes that would be in a style file, we set up
* the xcsv parser and make it ready for general use.
*/
void
xcsv_read_internal_style(const char *style_buf)
{
xcsv_file_init();
xcsv_file.is_internal = 1;
xcsv_parse_style_buff(style_buf);
/* if we have no output fields, use input fields as output fields */
if (xcsv_file.ofield_ct == 0) {
if (xcsv_file.ofield)
xfree(xcsv_file.ofield);
xcsv_file.ofield = &xcsv_file.ifield;
xcsv_file.ofield_ct = xcsv_file.ifield_ct;
}
}
void
xcsv_setup_internal_style(const char *style_buf)
{
xcsv_file_init();
xcsv_destroy_style();
xcsv_file.is_internal = !!style_buf;
intstylebuf = style_buf;
}
static void
xcsv_rd_init(const char *fname)
{
/*
* if we don't have an internal style defined, we need to
* read it from a user-supplied style file, or die trying.
*/
if (xcsv_file.is_internal ) {
xcsv_read_internal_style( intstylebuf );
}
else {
if (!styleopt)
fatal(MYNAME ": XCSV input style not declared. Use ... -i xcsv,style=path/to/file.style\n");
xcsv_read_style(styleopt);
}
if (global_opts.masked_objective & (TRKDATAMASK|RTEDATAMASK)) {
warning(MYNAME " attempt to read %s as a track or route, but this module only supports waypoints on read. Reading as waypoints instead.\n", fname);
}
xcsv_file.xcsvfp = gbfopen(fname, "r", MYNAME);
}
static void
xcsv_rd_deinit(void)
{
gbfclose(xcsv_file.xcsvfp);
xcsv_destroy_style();
}
static void
xcsv_wr_init(const char *fname)
{
/* if we don't have an internal style defined, we need to
* read it from a user-supplied style file, or die trying.
*/
if (xcsv_file.is_internal ) {
xcsv_read_internal_style( intstylebuf );
}
else {
if (!styleopt)
fatal(MYNAME ": XCSV output style not declared. Use ... -o xcsv,style=path/to/file.style\n");
xcsv_read_style(styleopt);
}
xcsv_file.xcsvfp = gbfopen(fname, "w", MYNAME);
xcsv_file.fname = (char *)fname;
/* set mkshort options from the command line */
if (global_opts.synthesize_shortnames) {
if (snlenopt)
setshort_length(xcsv_file.mkshort_handle, atoi(snlenopt));
if (snwhiteopt)
setshort_whitespace_ok(xcsv_file.mkshort_handle, atoi(snwhiteopt));
if (snupperopt)
setshort_mustupper(xcsv_file.mkshort_handle, atoi(snupperopt));
if (snuniqueopt)
setshort_mustuniq(xcsv_file.mkshort_handle, atoi(snuniqueopt));
setshort_badchars(xcsv_file.mkshort_handle, xcsv_file.badchars);
}
}
static void
xcsv_wr_deinit(void)
{
gbfclose(xcsv_file.xcsvfp);
xcsv_destroy_style();
}
ff_vecs_t xcsv_vecs = {
ff_type_internal,
FF_CAP_RW_WPT, /* This is a bit of a lie for now... */
xcsv_rd_init,
xcsv_wr_init,
xcsv_rd_deinit,
xcsv_wr_deinit,
xcsv_data_read,
xcsv_data_write,
NULL,
xcsv_args,
CET_CHARSET_ASCII, 0 /* CET-REVIEW */
};
#else
void xcsv_read_internal_style(const char *style_buf) {}
void xcsv_setup_internal_style(const char *style_buf) {}
#endif //CSVFMTS_ENABLED
|