| 452 | } |
| 453 | |
| 454 | static int |
| 455 | get_actual_line_size(FormatConfig *format_in_config, char *line_start, int cur_size, int tot_size, PG_FUNCTION_ARGS) |
| 456 | { |
| 457 | int row_size; |
| 458 | int actual_fields_size; |
| 459 | int remaining; |
| 460 | char *line_end; |
| 461 | char *expected_delim_loc = line_start + format_in_config->fields_tot_size; |
| 462 | |
| 463 | /* |
| 464 | * the case where there is no line delimiter |
| 465 | */ |
| 466 | if ( 0 == format_in_config->line_delimiter_length ) |
| 467 | { |
| 468 | return format_in_config->fields_tot_size; |
| 469 | } |
| 470 | |
| 471 | if ( 1 == format_in_config->line_delimiter_length ) |
| 472 | { |
| 473 | char delim = format_in_config->line_delimiter[0]; |
| 474 | |
| 475 | if ( *expected_delim_loc == delim ) |
| 476 | line_end = expected_delim_loc; |
| 477 | else |
| 478 | line_end = strchr(line_start, delim); |
| 479 | |
| 480 | } |
| 481 | else /* > 1 */ |
| 482 | { |
| 483 | int i; |
| 484 | bool as_expected = true; |
| 485 | |
| 486 | for ( i = 0; i < format_in_config->line_delimiter_length; i++) |
| 487 | { |
| 488 | if ( expected_delim_loc[i] != format_in_config->line_delimiter[i] ) |
| 489 | { |
| 490 | as_expected = false; |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if ( as_expected ) |
| 496 | line_end = expected_delim_loc; |
| 497 | else |
| 498 | line_end = strstr(line_start, format_in_config->line_delimiter); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * line_end will be 0, if strchr or strstr did not find the delimiter. |
| 503 | * In this case we throw an exception ( unless this is the last line in the buffer )--> The line delimiter specified in |
| 504 | * FormatConfig must be present in the file. |
| 505 | */ |
| 506 | if ( 0 == line_end /*did not find delimiter*/ ) |
| 507 | { |
| 508 | remaining = tot_size - cur_size - format_in_config->fields_tot_size; |
| 509 | if (1 == remaining) /* we are at the last line so we cannot find a custom delimiter - we have an OS line delimiter here */ |
| 510 | { |
| 511 | return (format_in_config->fields_tot_size + 1); |
no test coverage detected