Commit ef7bc20e authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Add work-around for potentially unavailable grib_get_length function.

* It's only in grib_api releases 1.12 and after.
parent 8b6ae015
......@@ -27312,6 +27312,17 @@ else
ENABLE_NETCDF_FALSE=
fi
 
for ac_func in grib_get_length
do :
ac_fn_c_check_func "$LINENO" "grib_get_length" "ac_cv_func_grib_get_length"
if test "x$ac_cv_func_grib_get_length" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_GRIB_GET_LENGTH 1
_ACEOF
fi
done
# ----------------------------------------------------------------------
# Compile with MPI support
# Sed expression to map a string onto a valid argument string part.
......
......@@ -129,6 +129,7 @@ AC_CHECK_LIB(m, floor)
# Add basic configure options
ACX_OPTIONS
AM_CONDITIONAL([ENABLE_NETCDF],[test x$ENABLE_NETCDF = xyes])
AC_CHECK_FUNCS([grib_get_length])
# ----------------------------------------------------------------------
# Compile with MPI support
AC_ARG_ENABLE([mpi],
......
......@@ -46,6 +46,9 @@
/* Define to 1 if you have the <grib_api.h> header file. */
#undef HAVE_GRIB_API_H
/* Define to 1 if you have the `grib_get_length' function. */
#undef HAVE_GRIB_GET_LENGTH
/* Define to 1 if you have the <hdf5.h> header file. */
#undef HAVE_HDF5_H
......
......@@ -28,11 +28,45 @@
//A simple wrapper for grib_get_string() that returns a newly allocated string.
char* gribCopyString(grib_handle* gribHandle, const char* key)
{
char* result = NULL;
size_t length;
if(grib_get_length(gribHandle, key, &length)) return NULL;
char* result = xmalloc(length);
if(!grib_get_string(gribHandle, key, result, &length)) return result;
#ifdef HAVE_GRIB_GET_LENGTH
if(!grib_get_length(gribHandle, key, &length))
{
char* result = xmalloc(length);
if(!grib_get_string(gribHandle, key, result, &length))
result = xrealloc(result, length);
else
{
free(result);
result = NULL;
}
}
#else
length = 1024; /* there's an implementation limit
* that makes strings longer than
* this unlikely in grib_api versions
* not providing grib_get_length */
int rc;
result = xmalloc(length);
while ((rc = grib_get_string(gribHandle, key, result, &length))
== GRIB_BUFFER_TOO_SMALL || rc == GRIB_ARRAY_TOO_SMALL)
{
if (length <= 1024UL * 1024UL)
{
length *= 2;
result = xrealloc(result, length);
}
else
break;
}
if (!rc)
{
result = xrealloc(result, length);
return result;
}
free(result);
#endif
return NULL;
}
......@@ -40,12 +74,29 @@ char* gribCopyString(grib_handle* gribHandle, const char* key)
//Returns true if the key exists and the value is equal to the given string.
bool gribCheckString(grib_handle* gribHandle, const char* key, const char* expectedValue)
{
size_t expectedLength = strlen(expectedValue) + 1, length;
size_t expectedLength = strlen(expectedValue) + 1;
#ifdef HAVE_GRIB_GET_LENGTH
size_t length;
if(grib_get_length(gribHandle, key, &length)) return false;
if(length != expectedLength) return false;
char value[length];
char *value = xmalloc(length);
if(grib_get_string(gribHandle, key, value, &length)) return false;
return !strcmp(value, expectedValue);
int rc = !strcmp(value, expectedValue);
free(value);
#else
char *value = gribCopyString(gribHandle, key);
int rc;
if (value)
{
rc = strlen(value) + 1 == expectedLength ?
!strcmp(value, expectedValue)
: false;
}
else
rc = false;
free(value);
#endif
return rc;
}
//A simple wrapper for grib_get_long() for the usecase that the result is only compared to a given constant value.
......
......@@ -611,7 +611,12 @@ int cdiGribIterator_getLong(CdiGribIterator* me, const char* key, long* result)
*/
int cdiGribIterator_getLength(CdiGribIterator* me, const char* key, size_t* result)
{
#ifdef HAVE_GRIB_GET_LENGTH
return grib_get_length(me->gribHandle, key, result);
#else
Error("unimplemented");
return -1;
#endif
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment