Move CSV code into its own file

This commit is contained in:
Eric Fischer
2017-10-10 14:57:38 -07:00
parent 54e9cd3cae
commit 19117d8060
5 changed files with 125 additions and 86 deletions
+25 -1
View File
@@ -13,6 +13,10 @@ int fail = EXIT_SUCCESS;
bool wrap = false;
const char *extract = NULL;
FILE *csvfile = NULL;
std::vector<std::string> cols;
std::vector<std::string> fields;
std::string buffered;
int buffered_type = -1;
// 0: nothing yet
@@ -247,11 +251,12 @@ void process(FILE *fp, const char *fname) {
}
int main(int argc, char **argv) {
const char *csv = NULL;
struct option long_options[] = {
{"wrap", no_argument, 0, 'w'},
{"extract", required_argument, 0, 'e'},
{"csv", required_argument, 0, 'c'},
{"join", required_argument, 0, 'j'},
{0, 0, 0, 0},
};
@@ -280,6 +285,10 @@ int main(int argc, char **argv) {
extract = optarg;
break;
case 'c':
csv = optarg;
break;
default:
fprintf(stderr, "Unexpected option -%c\n", i);
exit(EXIT_FAILURE);
@@ -291,6 +300,14 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}
if (csv != NULL) {
csvfile = fopen(csv, "r");
if (csvfile == NULL) {
perror(csv);
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
process(stdin, "standard input");
} else {
@@ -312,5 +329,12 @@ int main(int argc, char **argv) {
printf("]}\n");
}
if (csvfile != NULL) {
if (fclose(csvfile) != 0) {
perror("close");
exit(EXIT_FAILURE);
}
}
return fail;
}