8000 Patches implemented ... should resolve some open issues. by bobhairgrove · Pull Request #11 · rgamble/csvutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Patches implemented ... should resolve some open issues. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef CSVUTILS_VERSION_H__
#define CSVUTILS_VERSION_H__

void print_version(char *program_name);
void print_version(const char *program_name);

#endif
10 changes: 10 additions & 0 deletions src/csvbreak.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ make_file_name(const char *name)
void
cb1 (void *data, size_t len, void *vp)
{
(void)vp;

if (need_name_resolution) {
if ((strlen(break_field_name) == len) && !strncmp(break_field_name, data, len)) {
break_field = current_field + 1;
Expand Down Expand Up @@ -437,6 +439,9 @@ cb1 (void *data, size_t len, void *vp)
void
cb2 (int c, void *vp)
{
(void)c;
(void)vp;

/* No longer first record when first non-empty record seen */
if (first_record && current_field > 0) {
if (write_header)
Expand Down Expand Up @@ -556,12 +561,16 @@ main (int argc, char *argv[])
while ((bytes_read=fread(buf, 1, 1024, infile)) > 0) {
if (csv_parse(&p, buf, bytes_read, cb1, cb2, NULL) != bytes_read) {
fprintf(stderr, "Error while parsing file: %s\n", csv_strerror(csv_error(&p)));
csv_free(&p);
if (infile != stdin) fclose(infile);
exit(EXIT_FAILURE);
}
}

if (csv_fini(&p, cb1, cb2, NULL)) {
fprintf(stderr, "Error while parsing file: %s\n", csv_strerror(csv_error(&p)));
csv_free(&p);
if (infile != stdin) fclose(infile);
exit(EXIT_FAILURE);
}

Expand All @@ -570,6 +579,7 @@ main (int argc, char *argv[])
if (just_print_counts)
print_counts();

if (infile != stdin) fclose(infile);
call_remove_files = 0;
exit(EXIT_SUCCESS);
}
Expand Down
22 changes: 12 additions & 10 deletions src/csvcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

/*lint -ecall(732, csv_set_delim, csv_set_quote) */
/*lint -esym(750, AUTHORS, PROGRAM_NAME) */
/*lint -e801 goto used */

#include <stdio.h>
#include <stdlib.h>
Expand All @@ -47,7 +46,7 @@ static struct option const longopts[] =


/* The name this program was called with */
char *program_name;
const char *program_name;

/* The delimiter character */
char delimiter = CSV_COMMA;
Expand Down Expand Up @@ -98,6 +97,7 @@ check_file(char *filename)
FILE *fp;
size_t bytes_read;
size_t retval;
int ok = 1;

if (csv_init(&p, CSV_STRICT|CSV_STRICT_FINI) != 0) {
fprintf(stderr, "Failed to initialize csv parser\n");
Expand All @@ -119,24 +119,26 @@ check_file(char *filename)

while ((bytes_read=fread(buf, 1, 1024, fp)) > 0) {
if ((retval = csv_parse(&p, buf, bytes_read, NULL, NULL, NULL)) != bytes_read) {
ok = 0;
if (csv_error(&p) == CSV_EPARSE) {
printf("%s: malformed at byte %lu\n", filename ? filename : "stdin", (unsigned long)pos + retval + 1);
goto end;
} else {
printf("Error while processing %s: %s\n", filename ? filename : "stdin", csv_strerror(csv_error(&p)));
goto end;
}
break;
}
pos += 1024;
}

if (csv_fini(&p, NULL, NULL, NULL) != 0)
printf("%s: missing closing quote at end of input\n", filename ? filename : "stdin");
else
printf("%s well-formed\n", filename ? filename : "data is");
if (ok) {
if (csv_fini(&p, NULL, NULL, NULL) != 0)
printf("%s: missing closing quote at end of input\n", filename ? filename : "stdin");
else
printf("%s well-formed\n", filename ? filename : "data is");
}

end:
fclose(fp);
csv_free(&p);
if (fp != stdin) fclose(fp);
}

int
Expand Down
55 changes: 44 additions & 11 deletions src/csvcut.c
6D47
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ cleanup(void)
for (i = 0; i < entry_array_size; i++)
free(entry_array[i].data);
free(entry_array);
/* field_spec_array: */
if (field_spec_array) {
for (i=0; i<field_spec_size; ++i) {
if (field_spec_array[i].start_name)
free(field_spec_array[i].start_name);
if (field_spec_array[i].stop_name)
free(field_spec_array[i].stop_name);
}
free(field_spec_array);
}
}

void
Expand Down Expand Up @@ -221,6 +231,7 @@ Print selected fields of CSV files or CSV data received from standard input\n\
int
not_a_space(unsigned char c)
{
(void)c;
/* Preserve spaces when parsing field specs */
return 0;
}
Expand All @@ -229,20 +240,32 @@ void
process_field_specs(const char *f)
{
struct csv_parser p;
const char *errmsg[] = {
"Failed to initialize csv parser",
"Invalid field spec",
"Field list cannot be empty"
};
const char *errstr = NULL;

size_t len = strlen(f);

if (csv_init(&p, CSV_STRICT|CSV_STRICT_FINI))
err("Failed to initialize csv parser");
errstr = errmsg[0];

csv_set_space_func(&p, not_a_space);
if (!errstr) csv_set_space_func(&p, not_a_space);

if (csv_parse(&p, f, len, field_spec_cb1, field_spec_cb2, NULL) != len)
err("Invalid field spec");
if ((!errstr) && (csv_parse(&p, f, len, field_spec_cb1, field_spec_cb2, NULL) != len))
errstr = errmsg[1];

if (csv_fini(&p, field_spec_cb1, field_spec_cb2, NULL))
err("Invalid field spec");
if ((!errstr) && (csv_fini(&p, field_spec_cb1, field_spec_cb2, NULL)))
errstr = errmsg[1];

if ((!errstr) && (field_spec_size == 0))
errstr = errmsg[2];

csv_free(&p);

if (field_spec_size == 0)
err("Field list cannot be empty");
if (errstr) err(errstr);
}


Expand Down Expand Up @@ -318,15 +341,17 @@ field_spec_cb1(void *s, size_t len, void *data)
size_t left_size = 0, right_size = 0;
long unsigned left_value = 0, right_value = 0;
char *left = NULL, *right = NULL;
char *ptr;
char *ptr = NULL, *s_copy = NULL;
int left_ended = 0;
(void)data;

left = ptr = Strndup(s, len);
ptr = s_copy = Strndup(s, len);

while (*ptr) {
if (*ptr == '-') {
if (left_ended || left_size == 0)
err("Invalid field spec");
if (left) free(left);
left = Strndup(s, left_size);
if (Is_numeric(left)) {
left_value = strtoul(left, NULL, 10);
Expand Down Expand Up @@ -355,6 +380,7 @@ field_spec_cb1(void *s, size_t len, void *data)
} else
unresolved_fields++;
} else {
if (left) free(left); 9E81
left = Strndup(s, left_size);
if (Is_numeric(left)) {
left_value = strtoul(left, NULL, 10);
Expand All @@ -377,11 +403,14 @@ field_spec_cb1(void *s, size_t len, void *data)
}

add_field_spec(left, right, left_value, right_value);

free(s_copy);
}

void
field_spec_cb2(int c, void *data)
{
(void)data;
/* Field spec should not contain newlines */
if (c >= 0)
err("Invalid field spec");
Expand All @@ -391,6 +420,7 @@ void
cb1(void *s, size_t len, void *data)
{
size_t i = 0;
(void)data;

if (unresolved_fields) {
if (first_record) {
Expand Down Expand Up @@ -434,11 +464,14 @@ cb1(void *s, size_t len, void *data)
}

void
cb2 (int c, void *data)
cb2 (int m, void *data)
{
size_t i, j;
int first_field = 1;

(void)m;
(void)data;

if (first_record && current_field > 0)
first_record = 0;

Expand Down
17 changes: 9 additions & 8 deletions src/csvfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define AUTHORS "Robert Gamble"

/* The name this program was called with */
char *program_name;
const char *program_name;

/* The delimiter character */
char delimiter = CSV_COMMA;
Expand Down Expand Up @@ -119,6 +119,7 @@ cb1 (void *s, size_t i, void *outfile)
void
cb2 (int c, void *outfile)
{
(void)c;
fputc('\n', (FILE *)outfile);
current_field = 0;
}
Expand Down Expand Up @@ -207,7 +208,7 @@ main (int argc, char *argv[])
outfile = fopen(argv[optind+1], "wb");
if (outfile == NULL) {
fprintf(stderr, "Failed to open file %s: %s\n", argv[optind+1], strerror(errno));
fclose(infile);
if (infile != stdin) fclose(infile);
exit(EXIT_FAILURE);
}
}
Expand All @@ -216,8 +217,8 @@ main (int argc, char *argv[])
while ((i=fread(buf, 1, 1024, infile)) > 0) {
if (csv_parse(&p, buf, i, cb1, cb2, outfile) != i) {
fprintf(stderr, "Error parsing file: %s\n", csv_strerror(csv_error(&p)));
fclose(infile);
fclose(outfile);
if (infile != stdin) fclose(infile);
if (outfile != stdout) fclose(outfile);
if (argc - optind == 2) remove(argv[optind]);
exit(EXIT_FAILURE);
}
Expand All @@ -228,14 +229,14 @@ main (int argc, char *argv[])

if (ferror(infile)) {
fprintf(stderr, "Error reading from input file");
fclose(infile);
fclose(outfile);
if (infile != stdin) fclose(infile);
if (outfile != stdout) fclose(outfile);
if (argc - optind == 2) remove(argv[argc - optind]);
exit(EXIT_FAILURE);
}

fclose(infile);
fclose(outfile);
if (infile != stdin) fclose(infile);
if (outfile != stdout) fclose(outfile);
return EXIT_SUCCESS;
}

Loading
0