Index dump: Move is_index_file to grib_utils and improve error message

This commit is contained in:
Shahram Najm 2014-07-30 17:59:50 +01:00
parent efa037c35a
commit 9f930867f4
4 changed files with 28 additions and 41 deletions

View File

@ -1202,6 +1202,7 @@ int parse_keyval_string(char* grib_tool, char* arg, int values_required, int def
int is_productDefinitionTemplateNumber_EPS(long productDefinitionTemplateNumber);
int is_productDefinitionTemplateNumber_Chemical(long productDefinitionTemplateNumber);
int is_productDefinitionTemplateNumber_Aerosol(long productDefinitionTemplateNumber);
int is_index_file(const char* filename);
/* compile.c */
void grib_compile_flags(grib_compiler *c, long flags);

View File

@ -1290,3 +1290,23 @@ int is_productDefinitionTemplateNumber_Aerosol(long productDefinitionTemplateNum
productDefinitionTemplateNumber == 46 ||
productDefinitionTemplateNumber == 47);
}
int is_index_file(const char* filename)
{
FILE* fh;
char buf[8]={0,};
char* str="GRBIDX";
int ret=0;
fh=fopen(filename,"r");
if (!fh) return 0;
fread(buf,1,1,fh);
fread(buf,6,1,fh);
ret=!strcmp(buf,str);
fclose(fh);
return ret;
}

View File

@ -118,25 +118,6 @@ int grib_tool_new_filename_action(grib_runtime_options* options,const char* file
{
return 0;
}
static int is_index_file(const char* filename)
{
FILE* fh;
char buf[8]={0,};
char* str="GRBIDX";
int ret=0;
fh=fopen(filename,"r");
if (!fh) return 0;
fread(buf,1,1,fh);
fread(buf,6,1,fh);
ret=!strcmp(buf,str);
fclose(fh);
return ret;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file)
{
@ -155,15 +136,20 @@ int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* fil
if (is_index_file(options->current_infile->name))
{
int err = 0;
grib_context* c = grib_context_get_default();
const char* filename = options->current_infile->name;
grib_index* index = grib_index_read(grib_context_get_default(), filename, &err);
grib_index* index = grib_index_read(c, filename, &err);
if (err || index == NULL) {
printf("%s: Could not read index file\n", grib_tool_name);
grib_context_log(c, GRIB_LOG_ERROR, "%s: Could not read index file. %s\n",
grib_tool_name, grib_get_error_message(err));
exit(1);
}
grib_index_dump(stdout, index);
grib_index_delete(index);
index = NULL;
/* Since there are no GRIB messages, we have to stop tool exiting in case there
* are more index files
*/
options->fail = 0;
}
}

View File

@ -103,26 +103,6 @@ grib_runtime_options options={
};
static int is_index_file(const char* filename)
{
FILE* fh;
char buf[8]={0,};
char* str="GRBIDX";
int ret=0;
fh=fopen(filename,"r");
if (!fh) return 0;
fread(buf,1,1,fh);
fread(buf,6,1,fh);
ret=!strcmp(buf,str);
fclose(fh);
return ret;
}
static grib_handle* grib_handle_new_from_file_x(grib_context* c,FILE* f,int mode,int headers_only,int *err)
{
if (mode==MODE_GTS)