* Create a 'record' of 'fields' from our recorded leaf values. If * this is the first line and "no-header" isn't given, make a record * containing the leaf names. */
| 334 | * containing the leaf names. |
| 335 | */ |
| 336 | static void |
| 337 | csv_emit_record (xo_handle_t *xop, csv_private_t *csv) |
| 338 | { |
| 339 | csv_dbg(xop, csv, "csv: emit: ...\n"); |
| 340 | |
| 341 | ssize_t fnum; |
| 342 | uint32_t quote_flags; |
| 343 | leaf_t *lp; |
| 344 | |
| 345 | /* If we have no data, then don't bother */ |
| 346 | if (csv->c_leaf_depth == 0) |
| 347 | return; |
| 348 | |
| 349 | if (!(csv->c_flags & (CF_HEADER_DONE | CF_NO_HEADER))) { |
| 350 | csv->c_flags |= CF_HEADER_DONE; |
| 351 | |
| 352 | for (fnum = 0; fnum < csv->c_leaf_depth; fnum++) { |
| 353 | lp = &csv->c_leaf[fnum]; |
| 354 | const char *name = xo_buf_data(&csv->c_name_buf, lp->f_name); |
| 355 | |
| 356 | if (fnum != 0) |
| 357 | xo_buf_append(&csv->c_data, ",", 1); |
| 358 | |
| 359 | xo_buf_append(&csv->c_data, name, strlen(name)); |
| 360 | } |
| 361 | |
| 362 | csv_append_newline(&csv->c_data, csv); |
| 363 | } |
| 364 | |
| 365 | for (fnum = 0; fnum < csv->c_leaf_depth; fnum++) { |
| 366 | lp = &csv->c_leaf[fnum]; |
| 367 | const char *value; |
| 368 | |
| 369 | if (lp->f_flags & LF_HAS_VALUE) { |
| 370 | value = xo_buf_data(&csv->c_value_buf, lp->f_value); |
| 371 | } else { |
| 372 | value = ""; |
| 373 | } |
| 374 | |
| 375 | quote_flags = csv_quote_flags(xop, csv, value); |
| 376 | |
| 377 | if (fnum != 0) |
| 378 | xo_buf_append(&csv->c_data, ",", 1); |
| 379 | |
| 380 | if (quote_flags & QF_NEEDS_QUOTES) |
| 381 | xo_buf_append(&csv->c_data, "\"", 1); |
| 382 | |
| 383 | if (quote_flags & QF_NEEDS_ESCAPE) |
| 384 | csv_escape(&csv->c_data, value, strlen(value)); |
| 385 | else |
| 386 | xo_buf_append(&csv->c_data, value, strlen(value)); |
| 387 | |
| 388 | if (quote_flags & QF_NEEDS_QUOTES) |
| 389 | xo_buf_append(&csv->c_data, "\"", 1); |
| 390 | } |
| 391 | |
| 392 | csv_append_newline(&csv->c_data, csv); |
| 393 |
no test coverage detected