Commit f8a564dc authored by Oliver Heidmann's avatar Oliver Heidmann
Browse files

now compiling with c++ and --with-grib_api

parent 73685001
......@@ -69,7 +69,7 @@ static inline void *gribHandleNew(int editionNumber)
static inline void gribHandleDelete(void *gh)
{
grib_handle_delete(gh);
grib_handle_delete((struct grib_handle *)gh);
}
#else
#define gribHandleNew(editionNumber) (NULL)
......
......@@ -32,9 +32,9 @@ char* gribCopyString(grib_handle* gribHandle, const char* key)
#ifdef HAVE_GRIB_GET_LENGTH
if(!grib_get_length(gribHandle, key, &length))
{
char* result = xmalloc(length);
char* result = (char *)xmalloc(length);
if(!grib_get_string(gribHandle, key, result, &length))
result = xrealloc(result, length);
result = (char *)xrealloc(result, length);
else
{
free(result);
......@@ -47,7 +47,7 @@ char* gribCopyString(grib_handle* gribHandle, const char* key)
* this unlikely in grib_api versions
* not providing grib_get_length */
int rc;
result = xmalloc(length);
result = (char *)xmalloc(length);
while ((rc = grib_get_string(gribHandle, key, result, &length))
== GRIB_BUFFER_TOO_SMALL || rc == GRIB_ARRAY_TOO_SMALL)
{
......@@ -78,7 +78,7 @@ bool gribCheckString(grib_handle* gribHandle, const char* key, const char* expec
size_t length;
if(grib_get_length(gribHandle, key, &length)) return false;
if(length != expectedLength) return false;
char *value = xmalloc(length);
char *value = (char *)xmalloc(length);
if(grib_get_string(gribHandle, key, value, &length)) return false;
int rc = !strcmp(value, expectedValue);
free(value);
......@@ -257,7 +257,7 @@ static int addToDate(struct tm* me, long long amount, long unit)
static char* makeDateString(struct tm* me)
{
char *result
= xmalloc( 4+1+ 2+1+ 2+1+ 2+1+ 2+1+ 2+ 4+ 1);
= (char *)xmalloc( 4+1+ 2+1+ 2+1+ 2+1+ 2+1+ 2+ 4+ 1);
sprintf(result, "%04d-%02d-%02dT%02d:%02d:%02d.000", me->tm_year + 1900, me->tm_mon + 1, me->tm_mday, me->tm_hour, me->tm_min, me->tm_sec);
return result;
}
......@@ -288,12 +288,21 @@ static int getAvailabilityOfRelativeTimes(grib_handle* gh, bool* outHaveForecast
char* gribMakeTimeString(grib_handle* gh, bool getEndTime)
{
//Get the parts of the reference date.
#ifdef __cplusplus
struct tm date;
date.tm_mon = (int) gribGetLong(gh, "month") - 1; //months are zero based in struct tm and one based in GRIBy
date.tm_mday = (int)gribGetLong(gh, "day");
date.tm_hour = (int)gribGetLong(gh, "hour");
date.tm_min = (int)gribGetLong(gh, "minute");
#else
struct tm date = {
.tm_mon = (int)gribGetLong(gh, "month") - 1, //months are zero based in struct tm and one based in GRIB
.tm_mon = (int)gribGetLong(gh, "month") - 1, //months are zero based in struct tm and one based in GRIBy
.tm_mday = (int)gribGetLong(gh, "day"),
.tm_hour = (int)gribGetLong(gh, "hour"),
.tm_min = (int)gribGetLong(gh, "minute")
};
#endif
if(gribEditionNumber(gh) == 1)
{
date.tm_year = (int)gribGetLong(gh, "yearOfCentury"); //years are -1900 based both in struct tm and GRIB1
......
......@@ -26,7 +26,7 @@ static CdiGribIterator* cdiGribIterator_condestruct(CdiGribIterator* me, const c
{
#define super() (&me->super)
if(me) goto destruct;
me = xmalloc(sizeof(*me));
me = (CdiGribIterator *)xmalloc(sizeof(*me));
baseIterConstruct(super(), filetype);
me->file = cdiInputFile_make(path);
......@@ -45,7 +45,7 @@ static CdiGribIterator* cdiGribIterator_condestruct(CdiGribIterator* me, const c
destruct:
if(me->super.gridId != CDI_UNDEFID) gridDestroy(me->super.gridId);
if(me->gribHandle) grib_handle_delete(me->gribHandle);
if(me->gribHandle) grib_handle_delete((struct grib_handle *)me->gribHandle);
free(me->gribBuffer);
cdiRefObject_release(&me->file->super);
destructSuper:
......@@ -68,20 +68,19 @@ CdiGribIterator* cdiGribIterator_makeClone(CdiIterator* super)
CdiGribIterator* me = (CdiGribIterator*)super;
//Allocate memory and copy data. (operations that may fail)
CdiGribIterator* result = xmalloc(sizeof(*result));
CdiGribIterator* result = (struct CdiGribIterator *)xmalloc(sizeof(*result));
if(!result) goto fail;
*result = (CdiGribIterator)
{
.file = me->file,
.fileOffset = me->fileOffset,
.gribBuffer = NULL,
.bufferSize = me->bufferSize,
.curRecordSize = me->curRecordSize,
.gribHandle = NULL
};
result->file = me->file;
result->fileOffset = me->fileOffset;
result->gribBuffer = NULL;
result->bufferSize = me->bufferSize;
result->curRecordSize = me->curRecordSize;
result->gribHandle = NULL;
if(me->gribBuffer)
{
result->gribBuffer = xmalloc(me->bufferSize);
result->gribBuffer = (unsigned char *)xmalloc(me->bufferSize);
if(!result->gribBuffer) goto freeResult;
memcpy(result->gribBuffer, me->gribBuffer, me->curRecordSize);
}
......@@ -107,7 +106,7 @@ CdiGribIterator* cdiGribIterator_makeClone(CdiIterator* super)
//Error handling.
deleteGribHandle:
if(result->gribHandle) grib_handle_delete(result->gribHandle);
if(result->gribHandle) grib_handle_delete((grib_handle *)result->gribHandle);
freeBuffer:
free(result->gribBuffer);
freeResult:
......@@ -120,9 +119,9 @@ char* cdiGribIterator_serialize(CdiIterator* super)
{
CdiGribIterator* me = (CdiGribIterator*)super;
const char* path = cdiInputFile_getPath(me->file);
char* escapedPath = cdiEscapeSpaces(path);
char* result = xmalloc(strlen(escapedPath) + 3 * sizeof(int) * CHAR_BIT/8);
const char* path = (char *)cdiInputFile_getPath(me->file);
char* escapedPath = (char *)cdiEscapeSpaces(path);
char* result = (char *)xmalloc(strlen(escapedPath) + 3 * sizeof(int) * CHAR_BIT/8);
sprintf(result, "%s %zu", escapedPath, me->fileOffset);
free(escapedPath);
return result;
......@@ -132,7 +131,7 @@ char* cdiGribIterator_serialize(CdiIterator* super)
CdiGribIterator* cdiGribIterator_deserialize(const char* description)
{
char* path;
CdiGribIterator* me = xmalloc(sizeof(*me));
CdiGribIterator* me = (CdiGribIterator *)xmalloc(sizeof(*me));
if(!me) goto fail;
description = baseIter_constructFromString(&me->super, description);
......@@ -177,7 +176,7 @@ static void cdiGribIterator_ensureBuffer(CdiGribIterator* me, size_t requiredSiz
{
me->bufferSize *= 2;
if(me->bufferSize < requiredSize) me->bufferSize = requiredSize;
me->gribBuffer = xrealloc(me->gribBuffer, me->bufferSize);
me->gribBuffer = (unsigned char *)xrealloc(me->gribBuffer, me->bufferSize);
}
}
......@@ -197,7 +196,7 @@ static const unsigned char* positionOfGribMarker(const unsigned char* data, size
{
for(const unsigned char* currentPosition = data, *end = data + size; currentPosition < end; currentPosition++)
{
currentPosition = memchr(currentPosition, 'G', size - (size_t)(currentPosition - data) - 3); //-3 to ensure that we don't overrun the buffer during the strncmp() call.
currentPosition = (unsigned char *)memchr(currentPosition, 'G', size - (size_t)(currentPosition - data) - 3); //-3 to ensure that we don't overrun the buffer during the strncmp() call.
if(!currentPosition) return NULL;
if(!strncmp((const char*)currentPosition, "GRIB", 4)) return currentPosition;
}
......@@ -235,13 +234,13 @@ static ssize_t scanToGribMarker(CdiGribIterator* me)
static unsigned decode24(void* beData)
{
unsigned char* bytes = beData;
unsigned char* bytes = (unsigned char *)beData;
return ((unsigned)bytes[0] << 16) + ((unsigned)bytes[1] << 8) + (unsigned)bytes[2];
}
static uint64_t decode64(void* beData)
{
unsigned char* bytes = beData;
unsigned char* bytes = (unsigned char *)beData;
uint64_t result = 0;
for(size_t i = 0; i < 8; i++) result = (result << 8) + bytes[i];
return result;
......@@ -302,7 +301,7 @@ static void hexdump(void* data, size_t size)
static int readMessage(CdiGribIterator* me)
{
//Destroy the old grib_handle.
if(me->gribHandle) grib_handle_delete(me->gribHandle), me->gribHandle = NULL;
if(me->gribHandle) grib_handle_delete((grib_handle *)me->gribHandle), me->gribHandle = NULL;
me->fileOffset += (off_t)me->curRecordSize;
//Find the next record and determine its size.
......@@ -338,11 +337,11 @@ int cdiGribIterator_nextField(CdiIterator* super)
if(result) return result;
//Get the metadata that's published as variables in the base class.
super->datatype = gribGetDatatype(me->gribHandle);
super->timesteptype = gribapiGetTsteptype(me->gribHandle);
cdiDecodeParam(gribapiGetParam(me->gribHandle), &super->param.number, &super->param.category, &super->param.discipline);
super->datatype = gribGetDatatype((grib_handle *)me->gribHandle);
super->timesteptype = gribapiGetTsteptype((grib_handle *)me->gribHandle);
cdiDecodeParam(gribapiGetParam((grib_handle *)me->gribHandle), &super->param.number, &super->param.category, &super->param.discipline);
grid_t grid;
gribapiGetGrid(me->gribHandle, &grid);
gribapiGetGrid((grib_handle *)me->gribHandle, &grid);
super->gridId = gridGenerate(&grid);
return CDI_NOERR;
......@@ -351,7 +350,7 @@ int cdiGribIterator_nextField(CdiIterator* super)
char* cdiGribIterator_inqTime(CdiIterator* super, bool getEndTime)
{
CdiGribIterator* me = (CdiGribIterator*)super;
return gribMakeTimeString(me->gribHandle, getEndTime);
return gribMakeTimeString((grib_handle *)me->gribHandle, getEndTime);
}
int cdiGribIterator_levelType(CdiIterator* super, int levelSelector, char** outName, char** outLongName, char** outStdName, char** outUnit)
......@@ -360,15 +359,15 @@ int cdiGribIterator_levelType(CdiIterator* super, int levelSelector, char** outN
//First determine the zaxis type corresponding to the given level.
int zaxisType = ZAXIS_GENERIC;
if(gribEditionNumber(me->gribHandle) <= 1)
if(gribEditionNumber((grib_handle *)me->gribHandle) <= 1)
{
int levelType = (int)gribGetLongDefault(me->gribHandle, "indicatorOfTypeOfLevel", 255);
int levelType = (int)gribGetLongDefault((grib_handle *)me->gribHandle, "indicatorOfTypeOfLevel", 255);
if(levelSelector && !isGrib1DualLevel(levelType)) levelType = 255;
zaxisType = grib1ltypeToZaxisType(levelType);
}
else
{
int levelType = (int)gribGetLongDefault(me->gribHandle, levelSelector ? "typeOfSecondFixedSurface" : "typeOfFirstFixedSurface", 255);
int levelType = (int)gribGetLongDefault((grib_handle *)me->gribHandle, levelSelector ? "typeOfSecondFixedSurface" : "typeOfFirstFixedSurface", 255);
zaxisType = grib2ltypeToZaxisType(levelType);
}
......@@ -448,33 +447,33 @@ int cdiGribIterator_level(CdiIterator* super, int levelSelector, double* outValu
if(!outValue2) outValue2 = &trash;
*outValue1 = *outValue2 = 0;
if(gribEditionNumber(me->gribHandle) > 1)
if(gribEditionNumber((grib_handle *)me->gribHandle) > 1)
{
if(levelSelector)
{
return readLevel2(me->gribHandle, "typeOfFirstFixedSurface", "scaleFactorOfFirstFixedSurface", "scaledValueOfFirstFixedSurface", outValue1, outValue2);
return readLevel2((grib_handle *)me->gribHandle, "typeOfFirstFixedSurface", "scaleFactorOfFirstFixedSurface", "scaledValueOfFirstFixedSurface", outValue1, outValue2);
}
else
{
return readLevel2(me->gribHandle, "typeOfSecondFixedSurface", "scaleFactorOfSecondFixedSurface", "scaledValueOfSecondFixedSurface", outValue1, outValue2);
return readLevel2((grib_handle *)me->gribHandle, "typeOfSecondFixedSurface", "scaleFactorOfSecondFixedSurface", "scaledValueOfSecondFixedSurface", outValue1, outValue2);
}
}
else
{
long levelType = (uint8_t)gribGetLongDefault(me->gribHandle, "indicatorOfTypeOfLevel", -1); //1 byte
long levelType = (uint8_t)gribGetLongDefault((grib_handle *)me->gribHandle, "indicatorOfTypeOfLevel", -1); //1 byte
if(levelType == 255)
{}
else if(isGrib1DualLevel((int)levelType))
{
*outValue1 = (double)gribGetLongDefault(me->gribHandle, (levelSelector ? "bottomLevel" : "topLevel"), 0);
*outValue1 = (double)gribGetLongDefault((grib_handle *)me->gribHandle, (levelSelector ? "bottomLevel" : "topLevel"), 0);
}
else if(levelType == 100)
{
*outValue1 = 100 * (double)gribGetLongDefault(me->gribHandle, "level", 0); //2 bytes
*outValue1 = 100 * (double)gribGetLongDefault((grib_handle *)me->gribHandle, "level", 0); //2 bytes
}
else
{
*outValue1 = (double)gribGetLongDefault(me->gribHandle, "level", 0); //2 bytes
*outValue1 = (double)gribGetLongDefault((grib_handle *)me->gribHandle, "level", 0); //2 bytes
}
}
return CDI_NOERR;
......@@ -487,19 +486,19 @@ int cdiGribIterator_zaxisUuid(CdiIterator* super, int* outVgridNumber, int* outL
if(outVgridNumber)
{
long temp;
if(grib_get_long(me->gribHandle, "numberOfVGridUsed", &temp)) return CDI_EINVAL;
if(grib_get_long((grib_handle *)me->gribHandle, "numberOfVGridUsed", &temp)) return CDI_EINVAL;
*outVgridNumber = (int)temp;
}
if(outLevelCount)
{
long temp;
if(grib_get_long(me->gribHandle, "nlev", &temp)) return CDI_EINVAL;
if(grib_get_long((grib_handle *)me->gribHandle, "nlev", &temp)) return CDI_EINVAL;
*outLevelCount = (int)temp;
}
if(outUuid)
{
size_t size = sizeof(*outUuid);
if(grib_get_bytes(me->gribHandle, "uuidOfVGrid", *outUuid, &size)) return CDI_EINVAL;
if(grib_get_bytes((grib_handle *)me->gribHandle, "uuidOfVGrid", *outUuid, &size)) return CDI_EINVAL;
if(size != sizeof(*outUuid)) return CDI_EUFSTRUCT;
}
......@@ -509,18 +508,18 @@ int cdiGribIterator_zaxisUuid(CdiIterator* super, int* outVgridNumber, int* outL
char* cdiGribIterator_copyVariableName(CdiIterator* super)
{
CdiGribIterator* me = (CdiGribIterator*)super;
return gribCopyString(me->gribHandle, "shortName");
return gribCopyString((grib_handle *)me->gribHandle, "shortName");
}
void cdiGribIterator_readField(CdiIterator* super, double* buffer, size_t* nmiss)
{
CdiGribIterator* me = (CdiGribIterator*)super;
gribGetDoubleArray(me->gribHandle, "values", buffer);
long gridType = gribGetLong(me->gribHandle, "gridDefinitionTemplateNumber");
gribGetDoubleArray((grib_handle *)me->gribHandle, "values", buffer);
long gridType = gribGetLong((grib_handle *)me->gribHandle, "gridDefinitionTemplateNumber");
if(nmiss)
{
*nmiss = (gridType >= 50 && gridType <= 53) ? (size_t)0 : (size_t)gribGetLong(me->gribHandle, "numberOfMissing"); //The condition excludes harmonic data.
*nmiss = (gridType >= 50 && gridType <= 53) ? (size_t)0 : (size_t)gribGetLong((grib_handle *)me->gribHandle, "numberOfMissing"); //The condition excludes harmonic data.
}
}
......@@ -528,8 +527,8 @@ void cdiGribIterator_readFieldF(CdiIterator* super, float* buffer, size_t* nmiss
{
CdiGribIterator* me = (CdiGribIterator*)super;
size_t valueCount = gribGetArraySize(me->gribHandle, "values");
double* temp = malloc(valueCount*sizeof(*temp));
size_t valueCount = gribGetArraySize((grib_handle *)me->gribHandle, "values");
double* temp = (double *)malloc(valueCount*sizeof(*temp));
cdiGribIterator_readField(super, temp, nmiss);
for(size_t i = valueCount; i--; ) buffer[i] = (float)temp[i];
free(temp);
......@@ -578,7 +577,7 @@ void cdiGribIterator_delete(CdiGribIterator* me)
int cdiGribIterator_inqEdition(CdiGribIterator* me)
{
#ifdef HAVE_LIBGRIB_API
return (int)gribEditionNumber(me->gribHandle);
return (int)gribEditionNumber((grib_handle *)me->gribHandle);
#else
(void)me;
xabort("CDI was compiled without GribAPI support, so you can't possibly have a valid CdiGribIterator* to call this function with");
......@@ -602,7 +601,7 @@ int cdiGribIterator_inqEdition(CdiGribIterator* me)
int cdiGribIterator_getLong(CdiGribIterator* me, const char* key, long* result)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_long(me->gribHandle, key, result);
return grib_get_long((grib_handle *)me->gribHandle, key, result);
#else
(void)me;
(void)key;
......@@ -628,7 +627,7 @@ 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);
return grib_get_length((grib_handle *)me->gribHandle, key, result);
#elif defined(HAVE_LIBGRIB_API)
(void)me;
(void)key;
......@@ -660,7 +659,7 @@ int cdiGribIterator_getLength(CdiGribIterator* me, const char* key, size_t* resu
int cdiGribIterator_getString(CdiGribIterator* me, const char* key, char* result, size_t* length)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_string(me->gribHandle, key, result, length);
return grib_get_string((grib_handle *)me->gribHandle, key, result, length);
#else
(void)me;
(void)key;
......@@ -688,7 +687,7 @@ int cdiGribIterator_getString(CdiGribIterator* me, const char* key, char* result
long cdiGribIterator_inqLongValue(CdiGribIterator* me, const char* key)
{
#ifdef HAVE_LIBGRIB_API
return gribGetLong(me->gribHandle, key);
return gribGetLong((grib_handle *)me->gribHandle, key);
#else
(void)me;
(void)key;
......@@ -715,7 +714,7 @@ long cdiGribIterator_inqLongValue(CdiGribIterator* me, const char* key)
long cdiGribIterator_inqLongDefaultValue(CdiGribIterator* me, const char* key, long defaultValue)
{
#ifdef HAVE_LIBGRIB_API
return gribGetLongDefault(me->gribHandle, key, defaultValue);
return gribGetLongDefault((grib_handle *)me->gribHandle, key, defaultValue);
#else
(void)me;
(void)key;
......@@ -743,7 +742,7 @@ long cdiGribIterator_inqLongDefaultValue(CdiGribIterator* me, const char* key, l
char* cdiGribIterator_inqStringValue(CdiGribIterator* me, const char* key)
{
#ifdef HAVE_LIBGRIB_API
return gribCopyString(me->gribHandle, key);
return gribCopyString((grib_handle *)me->gribHandle, key);
#else
(void)me;
(void)key;
......@@ -768,7 +767,7 @@ char* cdiGribIterator_inqStringValue(CdiGribIterator* me, const char* key)
int cdiGribIterator_getDouble(CdiGribIterator* me, const char* key, double* result)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_double(me->gribHandle, key, result);
return grib_get_double((grib_handle *)me->gribHandle, key, result);
#else
(void)me;
(void)key;
......@@ -794,7 +793,7 @@ int cdiGribIterator_getDouble(CdiGribIterator* me, const char* key, double* resu
int cdiGribIterator_getSize(CdiGribIterator* me, const char* key, size_t* result)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_size(me->gribHandle, key, result);
return grib_get_size((grib_handle *)me->gribHandle, key, result);
#else
(void)me;
(void)key;
......@@ -820,7 +819,7 @@ int cdiGribIterator_getSize(CdiGribIterator* me, const char* key, size_t* result
int cdiGribIterator_getLongArray(CdiGribIterator* me, const char* key, long* result, size_t* size)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_long_array(me->gribHandle, key, result, size);
return grib_get_long_array((grib_handle *)me->gribHandle, key, result, size);
#else
(void)me;
(void)key;
......@@ -847,7 +846,7 @@ int cdiGribIterator_getLongArray(CdiGribIterator* me, const char* key, long* res
int cdiGribIterator_getDoubleArray(CdiGribIterator* me, const char* key, double* result, size_t* size)
{
#ifdef HAVE_LIBGRIB_API
return grib_get_double_array(me->gribHandle, key, result, size);
return grib_get_double_array((grib_handle *)me->gribHandle, key, result, size);
#else
(void)me;
(void)key;
......@@ -875,7 +874,7 @@ int cdiGribIterator_getDoubleArray(CdiGribIterator* me, const char* key, double*
double cdiGribIterator_inqDoubleValue(CdiGribIterator* me, const char* key)
{
#ifdef HAVE_LIBGRIB_API
return gribGetDouble(me->gribHandle, key);
return gribGetDouble((grib_handle *)me->gribHandle, key);
#else
(void)me;
(void)key;
......@@ -902,7 +901,7 @@ double cdiGribIterator_inqDoubleValue(CdiGribIterator* me, const char* key)
double cdiGribIterator_inqDoubleDefaultValue(CdiGribIterator* me, const char* key, double defaultValue)
{
#ifdef HAVE_LIBGRIB_API
return gribGetDoubleDefault(me->gribHandle, key, defaultValue);
return gribGetDoubleDefault((grib_handle *)me->gribHandle, key, defaultValue);
#else
(void)me;
(void)key;
......
......@@ -732,7 +732,7 @@ static void ensureBufferSize(size_t requiredSize, size_t* curSize, unsigned char
if ( *curSize < requiredSize )
{
*curSize = requiredSize;
*buffer = realloc(*buffer, *curSize);
*buffer = (unsigned char *)realloc(*buffer, *curSize);
}
}
......@@ -2612,7 +2612,7 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
#if defined(GRIBAPIENCODETEST)
grib_handle *gh = (grib_handle *) gribHandleNew(editionNumber);
#else
grib_handle *gh = gc->gribHandle;
grib_handle *gh = (struct grib_handle *)gc->gribHandle;
#endif
GRIB_CHECK(grib_get_long(gh, "editionNumber", &editionNumber), 0);
......
Markdown is supported
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