GRIB-797: grib_compare: allow last argument to be a directory

This commit is contained in:
Shahram Najm 2015-07-17 11:23:43 +01:00
parent 563c480bf1
commit 93fa444e11
4 changed files with 64 additions and 2 deletions

View File

@ -109,9 +109,19 @@ status=$?
set -e
[ $status -eq 1 ]
#----------------------------------------------------
# Compare file with directory
#----------------------------------------------------
temp_dir=tempdir.bufr_compare
mkdir -p $temp_dir
infile=aaen_55.bufr
cp $infile $temp_dir
${tools_dir}bufr_compare $infile $temp_dir
#Clean up
rm -f $fLog
rm -f $fBufrTmp | true
rm -f $fBufrInput1 | true
rm -f $fBufrInput2 | true
rm -rf $temp_dir

View File

@ -8,7 +8,6 @@
# virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
#
. ./include.sh
REDIRECT=/dev/null
@ -33,5 +32,12 @@ cat temp_comp.3 temp_comp.2 temp_comp.1 > temp_comp.321
# Compare files in which the messages are not in the same order
${tools_dir}grib_compare -r temp_comp.123 temp_comp.321
# GRIB-797: test last argument being a directory
temp_dir=tempdir.grib_compare
mkdir -p $temp_dir
cp $infile $temp_dir
${tools_dir}grib_compare $infile $temp_dir
rm -rf $temp_dir
rm -f temp_comp.1 temp_comp.2 temp_comp.3 temp_comp.123 temp_comp.321
rm -f $outfile || true

View File

@ -16,6 +16,12 @@
#include "grib_tools.h"
static const char* basename(const char* path)
{
char* s = strrchr(path, '/');
if (!s) return path;
else return s + 1;
}
GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b)
{
@ -324,6 +330,22 @@ int grib_tool_init(grib_runtime_options* options)
if (grib_options_on("t:"))
tolerance_factor=atof(grib_options_get_option("t:"));
{
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
/* with the same name as first file in that directory */
struct stat s;
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
if (infile) {
int stat_val = stat(infile->name, &s);
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
/* Take the filename of the 1st file and append to dir */
char bufr[2048] = {0,};
/* options->infile_extra->name is the 1st file */
sprintf(bufr, "%s/%s", infile->name, basename(options->infile_extra->name));
infile->name = strdup(bufr);
}
}
}
return 0;
}

View File

@ -10,6 +10,13 @@
#include "grib_tools.h"
static const char* basename(const char* path)
{
char* s = strrchr(path, '/');
if (!s) return path;
else return s + 1;
}
GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b)
{
if (*a != *b) return 1;
@ -283,6 +290,23 @@ int grib_tool_init(grib_runtime_options* options)
}
}
{
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
/* with the same name as first file in that directory */
struct stat s;
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
if (infile) {
int stat_val = stat(infile->name, &s);
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
/* Take the filename of the 1st file and append to dir */
char bufr[2048] = {0,};
/* options->infile_extra->name is the 1st file */
sprintf(bufr, "%s/%s", infile->name, basename(options->infile_extra->name));
infile->name = strdup(bufr);
}
}
}
return 0;
}