Skip to content

Commit

Permalink
Merge pull request #4200 from rouault/fix_4198
Browse files Browse the repository at this point in the history
cct: emit error message and return error code when not being able to open input file
  • Loading branch information
rouault authored Jul 21, 2024
2 parents be9b42a + e9557f1 commit 7d766a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/apps/cct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ int main(int argc, char **argv) {

/* Loop over all records of all input files */
int previous_index = -1;
while (opt_input_loop(o, optargs_file_format_text)) {
bool gotError = false;
while (opt_input_loop(o, optargs_file_format_text, &gotError)) {
int err;
char *bufptr = fgets(buf, BUFFER_SIZE - 1, o->input);
if (opt_eof(o)) {
Expand Down Expand Up @@ -547,7 +548,7 @@ int main(int argc, char **argv) {
fclose(fout);
free(o);
free(buf);
return 0;
return gotError ? 1 : 0;
}

/* return a pointer to the n'th column of buf */
Expand Down
26 changes: 17 additions & 9 deletions src/apps/optargpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ handle open/close of a sequence of input files:
enum OPTARGS_FILE_MODE:
indicates whether to read operands in text (0) or binary (1) mode
opt_input_loop (o, mode):
opt_input_loop (o, mode, &gotError):
When used as condition in a while loop, traverses all operands,
giving the impression of reading just a single input file.
Expand Down Expand Up @@ -129,7 +129,8 @@ to "o" internally
P = proj_create_argv (0, o->pargc, o->pargv);
// Loop over all lines of all input files
while (opt_input_loop (o, optargs_file_format_text)) {
bool gotError = false;
while (opt_input_loop (o, optargs_file_format_text, &gotError)) {
char buf[1000];
int ret = fgets (buf, 1000, o->input);
if (opt_eof (o)) {
Expand Down Expand Up @@ -186,6 +187,7 @@ Thomas Knudsen, [email protected], 2016-05-25/2017-09-10
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -201,7 +203,7 @@ enum OPTARGS_FILE_FORMAT {
char *opt_filename(OPTARGS *opt);
static int opt_eof(OPTARGS *opt);
int opt_record(OPTARGS *opt);
int opt_input_loop(OPTARGS *opt, int binary);
int opt_input_loop(OPTARGS *opt, int binary, bool *gotError);
static int opt_is_flag(OPTARGS *opt, int ordinal);
static int opt_raise_flag(OPTARGS *opt, int ordinal);
static int opt_ordinal(OPTARGS *opt, const char *option);
Expand Down Expand Up @@ -261,7 +263,9 @@ int opt_record(OPTARGS *opt) {
}

/* handle closing/opening of a "stream-of-streams" */
int opt_input_loop(OPTARGS *opt, int binary) {
int opt_input_loop(OPTARGS *opt, int binary, bool *gotError) {
if (gotError)
*gotError = false;
if (nullptr == opt)
return 0;

Expand Down Expand Up @@ -291,12 +295,16 @@ int opt_input_loop(OPTARGS *opt, int binary) {
return 0;

/* otherwise, open next input file */
opt->input = fopen(opt->fargv[opt->input_index++], binary ? "rb" : "rt");
if (nullptr != opt->input)
return 1;
const char *filename = opt->fargv[opt->input_index++];
opt->input = fopen(filename, binary ? "rb" : "rt");
if (nullptr == opt->input) {
fprintf(stderr, "Cannot open file %s\n", filename);
if (gotError)
*gotError = true;
return 0;
}

/* ignore non-existing files - go on! */
return opt_input_loop(opt, binary);
return 1;
}

/* return true if option with given ordinal is a flag, false if undefined or
Expand Down
4 changes: 4 additions & 0 deletions test/cli/test_cct.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ tests:
args: +proj=noop input_file1_with_utf8_bom.txt
# no BOM with output
out: " 0.0000 3.0000 0.0000 inf"
- comment: Test cct with non existing input file
args: +proj=noop i_do_not_exist.txt
stderr: "Cannot open file i_do_not_exist.txt"
exitcode: 1

0 comments on commit 7d766a4

Please sign in to comment.