Commit 7374d6bc authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Set constant variables to const.

parent 6fef6aca
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
UINT32 get_UINT32(unsigned char *x) UINT32 get_UINT32(unsigned char *x)
{ {
// clang-format off
switch (HOST_ENDIANNESS) switch (HOST_ENDIANNESS)
{ {
case CDI_BIGENDIAN: case CDI_BIGENDIAN:
...@@ -17,11 +18,13 @@ UINT32 get_UINT32(unsigned char *x) ...@@ -17,11 +18,13 @@ UINT32 get_UINT32(unsigned char *x)
Error("Unhandled endianness %d", HOST_ENDIANNESS); Error("Unhandled endianness %d", HOST_ENDIANNESS);
return UINT32_C(0xFFFFFFFF); return UINT32_C(0xFFFFFFFF);
} }
// clang-format on
} }
UINT32 get_SUINT32(unsigned char *x) UINT32 get_SUINT32(unsigned char *x)
{ {
// clang-format off
switch (HOST_ENDIANNESS) switch (HOST_ENDIANNESS)
{ {
case CDI_BIGENDIAN: case CDI_BIGENDIAN:
...@@ -32,11 +35,13 @@ UINT32 get_SUINT32(unsigned char *x) ...@@ -32,11 +35,13 @@ UINT32 get_SUINT32(unsigned char *x)
Error("Unhandled endianness %d", HOST_ENDIANNESS); Error("Unhandled endianness %d", HOST_ENDIANNESS);
return UINT32_C(0xFFFFFFFF); return UINT32_C(0xFFFFFFFF);
} }
// clang-format on
} }
UINT64 get_UINT64(unsigned char *x) UINT64 get_UINT64(unsigned char *x)
{ {
// clang-format off
switch (HOST_ENDIANNESS) switch (HOST_ENDIANNESS)
{ {
case CDI_BIGENDIAN: case CDI_BIGENDIAN:
...@@ -49,11 +54,13 @@ UINT64 get_UINT64(unsigned char *x) ...@@ -49,11 +54,13 @@ UINT64 get_UINT64(unsigned char *x)
Error("Unhandled endianness %d", HOST_ENDIANNESS); Error("Unhandled endianness %d", HOST_ENDIANNESS);
return UINT64_C(0xFFFFFFFFFFFFFFFF); return UINT64_C(0xFFFFFFFFFFFFFFFF);
} }
// clang-format on
} }
UINT64 get_SUINT64(unsigned char *x) UINT64 get_SUINT64(unsigned char *x)
{ {
// clang-format off
switch (HOST_ENDIANNESS) switch (HOST_ENDIANNESS)
{ {
case CDI_BIGENDIAN: case CDI_BIGENDIAN:
...@@ -66,6 +73,7 @@ UINT64 get_SUINT64(unsigned char *x) ...@@ -66,6 +73,7 @@ UINT64 get_SUINT64(unsigned char *x)
Error("Unhandled endianness %d", HOST_ENDIANNESS); Error("Unhandled endianness %d", HOST_ENDIANNESS);
return UINT64_C(0xFFFFFFFFFFFFFFFF); return UINT64_C(0xFFFFFFFFFFFFFFFF);
} }
// clang-format on
} }
......
...@@ -16,17 +16,20 @@ enum { ...@@ -16,17 +16,20 @@ enum {
EXT_HEADER_LEN = 4, EXT_HEADER_LEN = 4,
}; };
union EXT_HEADER
{
INT32 i32[EXT_HEADER_LEN];
INT64 i64[EXT_HEADER_LEN];
};
static int initExtLib = 0; static int initExtLib = 0;
static int extDefaultPrec = 0; static int extDefaultPrec = 0;
static int extDefaultNumber = EXT_REAL; static int extDefaultNumber = EXT_REAL;
/* // A version string.
* A version string.
*/
#undef LIBVERSION #undef LIBVERSION
#define LIBVERSION 1.4.1 #define LIBVERSION 1.4.2
#define XSTRING(x) #x #define XSTRING(x) #x
#define STRING(x) XSTRING(x) #define STRING(x) XSTRING(x)
static const char ext_libvers[] = STRING(LIBVERSION); static const char ext_libvers[] = STRING(LIBVERSION);
...@@ -37,13 +40,13 @@ const char *extLibraryVersion(void) ...@@ -37,13 +40,13 @@ const char *extLibraryVersion(void)
} }
static int EXT_Debug = 0; /* If set to 1, debugging */ static int EXT_Debug = 0; // If set to 1, debugging
void extDebug(int debug) void extDebug(int debug)
{ {
if (debug) Message("debug level %d", debug);
EXT_Debug = debug; EXT_Debug = debug;
if (EXT_Debug) Message("debug level %d", debug);
} }
...@@ -141,12 +144,12 @@ int extCheckFiletype(int fileID, int *swap) ...@@ -141,12 +144,12 @@ int extCheckFiletype(int fileID, int *swap)
if ( fileRead(fileID, buffer, 4) != 4 ) return 0; if ( fileRead(fileID, buffer, 4) != 4 ) return 0;
size_t blocklen = (size_t) get_UINT32(buffer); const size_t blocklen = (size_t) get_UINT32(buffer);
size_t sblocklen = (size_t) get_SUINT32(buffer); const size_t sblocklen = (size_t) get_SUINT32(buffer);
if ( EXT_Debug ) if ( EXT_Debug ) Message("blocklen = %d sblocklen = %d", blocklen, sblocklen);
Message("blocklen = %d sblocklen = %d", blocklen, sblocklen);
// clang-format off
if ( blocklen == 16 ) if ( blocklen == 16 )
{ {
*swap = 0; *swap = 0;
...@@ -179,6 +182,7 @@ int extCheckFiletype(int fileID, int *swap) ...@@ -179,6 +182,7 @@ int extCheckFiletype(int fileID, int *swap)
pbuf = buffer+3*fact; dimxy = (size_t) get_SUINT64(pbuf); pbuf = buffer+3*fact; dimxy = (size_t) get_SUINT64(pbuf);
pbuf = buffer+sblocklen+4; data = (size_t) get_SUINT32(pbuf); pbuf = buffer+sblocklen+4; data = (size_t) get_SUINT32(pbuf);
} }
// clang-format on
fileRewind(fileID); fileRewind(fileID);
...@@ -188,7 +192,7 @@ int extCheckFiletype(int fileID, int *swap) ...@@ -188,7 +192,7 @@ int extCheckFiletype(int fileID, int *swap)
Message("dimxy = %lu data = %lu", dimxy, data); Message("dimxy = %lu data = %lu", dimxy, data);
} }
int found = data && (dimxy*fact == data || dimxy*fact*2 == data); const int found = data && (dimxy*fact == data || dimxy*fact*2 == data);
return found; return found;
} }
...@@ -197,9 +201,9 @@ int extInqHeader(void *ext, int *header) ...@@ -197,9 +201,9 @@ int extInqHeader(void *ext, int *header)
{ {
extrec_t *extp = (extrec_t *) ext; extrec_t *extp = (extrec_t *) ext;
for (size_t i = 0; i < EXT_HEADER_LEN; i++) header[i] = extp->header[i]; for (int i = 0; i < EXT_HEADER_LEN; i++) header[i] = extp->header[i];
if ( EXT_Debug ) Message("datasize = %lu", extp->datasize); if ( EXT_Debug ) Message("datasize = %zu", extp->datasize);
return 0; return 0;
} }
...@@ -209,12 +213,12 @@ int extDefHeader(void *ext, const int *header) ...@@ -209,12 +213,12 @@ int extDefHeader(void *ext, const int *header)
{ {
extrec_t *extp = (extrec_t *) ext; extrec_t *extp = (extrec_t *) ext;
for (size_t i = 0; i < EXT_HEADER_LEN; i++) extp->header[i] = header[i]; for (int i = 0; i < EXT_HEADER_LEN; i++) extp->header[i] = header[i];
extp->datasize = (size_t)header[3]; extp->datasize = (size_t)header[3];
if ( extp->number == EXT_COMP ) extp->datasize *= 2; if ( extp->number == EXT_COMP ) extp->datasize *= 2;
if (EXT_Debug) Message("datasize = %lu", extp->datasize); if (EXT_Debug) Message("datasize = %zu", extp->datasize);
return 0; return 0;
} }
...@@ -223,10 +227,10 @@ static ...@@ -223,10 +227,10 @@ static
int extInqData(extrec_t *extp, int prec, void *data) int extInqData(extrec_t *extp, int prec, void *data)
{ {
int ierr = 0; int ierr = 0;
int byteswap = extp->byteswap; const int byteswap = extp->byteswap;
size_t datasize = extp->datasize; const size_t datasize = extp->datasize;
void *buffer = extp->buffer; void *buffer = extp->buffer;
int rprec = extp->prec; const int rprec = extp->prec;
switch ( rprec ) switch ( rprec )
{ {
...@@ -291,26 +295,21 @@ static int extDefData(void *ext, int prec, const void *data) ...@@ -291,26 +295,21 @@ static int extDefData(void *ext, int prec, const void *data)
{ {
extrec_t *extp = (extrec_t *) ext; extrec_t *extp = (extrec_t *) ext;
int rprec = extDefaultPrec ? extDefaultPrec : extp->prec; const int rprec = extDefaultPrec ? extDefaultPrec : extp->prec;
extp->prec = rprec ? rprec : prec; extp->prec = rprec ? rprec : prec;
int *header = extp->header; int *header = extp->header;
size_t datasize = (size_t)header[3]; size_t datasize = (size_t)header[3];
if ( extp->number == EXT_COMP ) datasize *= 2; if ( extp->number == EXT_COMP ) datasize *= 2;
size_t blocklen = datasize * (size_t)rprec; const size_t blocklen = datasize * (size_t)rprec;
extp->datasize = datasize; extp->datasize = datasize;
void *buffer = extp->buffer; if ( extp->buffersize != blocklen )
size_t buffersize = extp->buffersize;
if ( buffersize != blocklen )
{ {
buffersize = blocklen; extp->buffersize = blocklen;
buffer = Realloc(buffer, buffersize); extp->buffer = Realloc(extp->buffer, extp->buffersize);
extp->buffer = buffer;
extp->buffersize = buffersize;
} }
switch ( rprec ) switch ( rprec )
...@@ -318,20 +317,20 @@ static int extDefData(void *ext, int prec, const void *data) ...@@ -318,20 +317,20 @@ static int extDefData(void *ext, int prec, const void *data)
case EXSE_SINGLE_PRECISION: case EXSE_SINGLE_PRECISION:
{ {
if ( rprec == prec ) if ( rprec == prec )
memcpy(buffer, data, datasize*sizeof(FLT32)); memcpy(extp->buffer, data, datasize*sizeof(FLT32));
else else
for (size_t i = 0; i < datasize; i++) for (size_t i = 0; i < datasize; i++)
((float *) buffer)[i] = (float) ((double *) data)[i]; ((float *) extp->buffer)[i] = (float) ((double *) data)[i];
break; break;
} }
case EXSE_DOUBLE_PRECISION: case EXSE_DOUBLE_PRECISION:
{ {
if ( rprec == prec ) if ( rprec == prec )
memcpy(buffer, data, datasize*sizeof(FLT64)); memcpy(extp->buffer, data, datasize*sizeof(FLT64));
else else
for (size_t i = 0; i < datasize; i++) for (size_t i = 0; i < datasize; i++)
((double *) buffer)[i] = (double) ((float *) data)[i]; ((double *) extp->buffer)[i] = (double) ((float *) data)[i];
break; break;
} }
...@@ -369,39 +368,32 @@ int extRead(int fileID, void *ext) ...@@ -369,39 +368,32 @@ int extRead(int fileID, void *ext)
extp->checked = 1; extp->checked = 1;
} }
int byteswap = extp->byteswap; const int byteswap = extp->byteswap;
/* read header record */ // read header record
size_t blocklen = binReadF77Block(fileID, byteswap); size_t blocklen = binReadF77Block(fileID, byteswap);
if ( fileEOF(fileID) ) return -1; if ( fileEOF(fileID) ) return -1;
if ( EXT_Debug ) Message("blocklen = %lu", blocklen); if ( EXT_Debug ) Message("blocklen = %lu", blocklen);
size_t hprec = blocklen / EXT_HEADER_LEN; const size_t hprec = blocklen / EXT_HEADER_LEN;
extp->prec = (int)hprec; extp->prec = (int)hprec;
union EXT_HEADER tempheader;
switch ( hprec ) switch ( hprec )
{ {
case EXSE_SINGLE_PRECISION: case EXSE_SINGLE_PRECISION:
{ {
INT32 tempheader[4]; binReadInt32(fileID, byteswap, EXT_HEADER_LEN, tempheader.i32);
binReadInt32(fileID, byteswap, EXT_HEADER_LEN, tempheader); for (int i = 0; i < EXT_HEADER_LEN; i++) extp->header[i] = (int)tempheader.i32[i];
for (size_t i = 0; i < EXT_HEADER_LEN; i++)
extp->header[i] = (int)tempheader[i];
break; break;
} }
case EXSE_DOUBLE_PRECISION: case EXSE_DOUBLE_PRECISION:
{ {
INT64 tempheader[4]; binReadInt64(fileID, byteswap, EXT_HEADER_LEN, tempheader.i64);
binReadInt64(fileID, byteswap, EXT_HEADER_LEN, tempheader); for (int i = 0; i < EXT_HEADER_LEN; i++) extp->header[i] = (int)tempheader.i64[i];
for (size_t i = 0; i < EXT_HEADER_LEN; i++)
extp->header[i] = (int)tempheader[i];
break; break;
} }
default: default:
...@@ -421,19 +413,14 @@ int extRead(int fileID, void *ext) ...@@ -421,19 +413,14 @@ int extRead(int fileID, void *ext)
extp->datasize = (size_t)extp->header[3]; extp->datasize = (size_t)extp->header[3];
if ( EXT_Debug ) Message("datasize = %lu", extp->datasize); if ( EXT_Debug ) Message("datasize = %zu", extp->datasize);
blocklen = binReadF77Block(fileID, byteswap); blocklen = binReadF77Block(fileID, byteswap);
void *buffer = extp->buffer; if ( extp->buffersize < blocklen )
size_t buffersize = (size_t)extp->buffersize;
if ( buffersize < blocklen )
{ {
buffersize = blocklen; extp->buffersize = blocklen;
buffer = Realloc(buffer, buffersize); extp->buffer = Realloc(extp->buffer, extp->buffersize);
extp->buffer = buffer;
extp->buffersize = buffersize;
} }
size_t dprec = blocklen / extp->datasize; size_t dprec = blocklen / extp->datasize;
...@@ -455,7 +442,7 @@ int extRead(int fileID, void *ext) ...@@ -455,7 +442,7 @@ int extRead(int fileID, void *ext)
return -1; return -1;
} }
fileRead(fileID, buffer, blocklen); fileRead(fileID, extp->buffer, blocklen);
blocklen2 = binReadF77Block(fileID, byteswap); blocklen2 = binReadF77Block(fileID, byteswap);
...@@ -472,13 +459,13 @@ int extRead(int fileID, void *ext) ...@@ -472,13 +459,13 @@ int extRead(int fileID, void *ext)
int extWrite(int fileID, void *ext) int extWrite(int fileID, void *ext)
{ {
extrec_t *extp = (extrec_t *) ext; extrec_t *extp = (extrec_t *) ext;
union { INT32 i32[EXT_HEADER_LEN]; INT64 i64[EXT_HEADER_LEN]; } tempheader; union EXT_HEADER tempheader;
int byteswap = extp->byteswap; const int byteswap = extp->byteswap;
int rprec = extp->prec; const int rprec = extp->prec;
int number = extp->number; int number = extp->number;
int *header = extp->header; int *header = extp->header;
/* write header record */ // write header record
size_t blocklen = EXT_HEADER_LEN * (size_t)rprec; size_t blocklen = EXT_HEADER_LEN * (size_t)rprec;
binWriteF77Block(fileID, byteswap, blocklen); binWriteF77Block(fileID, byteswap, blocklen);
...@@ -487,20 +474,14 @@ int extWrite(int fileID, void *ext) ...@@ -487,20 +474,14 @@ int extWrite(int fileID, void *ext)
{ {
case EXSE_SINGLE_PRECISION: case EXSE_SINGLE_PRECISION:
{ {
for (size_t i = 0; i < EXT_HEADER_LEN; i++) for (int i = 0; i < EXT_HEADER_LEN; i++) tempheader.i32[i] = (INT32) header[i];
tempheader.i32[i] = (INT32) header[i];
binWriteInt32(fileID, byteswap, EXT_HEADER_LEN, tempheader.i32); binWriteInt32(fileID, byteswap, EXT_HEADER_LEN, tempheader.i32);
break; break;
} }
case EXSE_DOUBLE_PRECISION: case EXSE_DOUBLE_PRECISION:
{ {
for (size_t i = 0; i < EXT_HEADER_LEN; i++) for (int i = 0; i < EXT_HEADER_LEN; i++) tempheader.i64[i] = (INT64) header[i];
tempheader.i64[i] = (INT64) header[i];
binWriteInt64(fileID, byteswap, EXT_HEADER_LEN, tempheader.i64); binWriteInt64(fileID, byteswap, EXT_HEADER_LEN, tempheader.i64);
break; break;
} }
default: default:
...@@ -512,26 +493,22 @@ int extWrite(int fileID, void *ext) ...@@ -512,26 +493,22 @@ int extWrite(int fileID, void *ext)
binWriteF77Block(fileID, byteswap, blocklen); binWriteF77Block(fileID, byteswap, blocklen);
size_t datasize = (size_t)header[3]; extp->datasize = (size_t)header[3];
if ( number == EXT_COMP ) datasize *= 2; if ( number == EXT_COMP ) extp->datasize *= 2;
blocklen = datasize * (size_t)rprec; blocklen = extp->datasize * (size_t)rprec;
binWriteF77Block(fileID, byteswap, blocklen); binWriteF77Block(fileID, byteswap, blocklen);
extp->datasize = datasize;
void *buffer = extp->buffer;
switch ( rprec ) switch ( rprec )
{ {
case EXSE_SINGLE_PRECISION: case EXSE_SINGLE_PRECISION:
{ {
binWriteFlt32(fileID, byteswap, datasize, (FLT32 *) buffer); binWriteFlt32(fileID, byteswap, extp->datasize, (FLT32 *) extp->buffer);
break; break;
} }
case EXSE_DOUBLE_PRECISION: case EXSE_DOUBLE_PRECISION:
{ {
binWriteFlt64(fileID, byteswap, datasize, (FLT64 *) buffer); binWriteFlt64(fileID, byteswap, extp->datasize, (FLT64 *) extp->buffer);
break; break;
} }
default: default:
......
...@@ -16,11 +16,9 @@ static int initIegLib = 0; ...@@ -16,11 +16,9 @@ static int initIegLib = 0;
static int iegDefaultDprec = 0; static int iegDefaultDprec = 0;
/* // A version string.
* A version string.
*/
#undef LIBVERSION #undef LIBVERSION
#define LIBVERSION 1.4.1 #define LIBVERSION 1.4.2
#define XSTRING(x) #x #define XSTRING(x) #x
#define STRING(x) XSTRING(x) #define STRING(x) XSTRING(x)
static const char ieg_libvers[] = STRING(LIBVERSION); static const char ieg_libvers[] = STRING(LIBVERSION);
...@@ -31,7 +29,7 @@ const char *iegLibraryVersion(void) ...@@ -31,7 +29,7 @@ const char *iegLibraryVersion(void)
} }
static int IEG_Debug = 0; /* If set to 1, debugging */ static int IEG_Debug = 0; // If set to 1, debugging
static static
void iegLibInit(void) void iegLibInit(void)
...@@ -42,7 +40,6 @@ void iegLibInit(void) ...@@ -42,7 +40,6 @@ void iegLibInit(void)
if ( envString ) if ( envString )
{ {
int nrun = (strlen(envString) == 2) ? 1 : 2; int nrun = (strlen(envString) == 2) ? 1 : 2;
int pos = 0; int pos = 0;
while ( nrun-- ) while ( nrun-- )
{ {
...@@ -54,8 +51,7 @@ void iegLibInit(void) ...@@ -54,8 +51,7 @@ void iegLibInit(void)
{ {
case '4': iegDefaultDprec = EXSE_SINGLE_PRECISION; break; case '4': iegDefaultDprec = EXSE_SINGLE_PRECISION; break;
case '8': iegDefaultDprec = EXSE_DOUBLE_PRECISION; break; case '8': iegDefaultDprec = EXSE_DOUBLE_PRECISION; break;
default: default: Message("Invalid digit in %s: %s", envName, envString);
Message("Invalid digit in %s: %s", envName, envString);
} }
break; break;
} }
...@@ -75,8 +71,8 @@ void iegLibInit(void) ...@@ -75,8 +71,8 @@ void iegLibInit(void)
void iegDebug(int debug) void iegDebug(int debug)
{ {
if (debug) Message("debug level %d", debug);
IEG_Debug = debug; IEG_Debug = debug;
if (IEG_Debug) Message("debug level %d", debug);
} }
static static
...@@ -95,7 +91,6 @@ void iegInit(iegrec_t *iegp) ...@@ -95,7 +91,6 @@ void iegInit(iegrec_t *iegp)
void iegInitMem(void *ieg) void iegInitMem(void *ieg)
{ {
iegrec_t *iegp = (iegrec_t *) ieg; iegrec_t *iegp = (iegrec_t *) ieg;
memset(iegp->ipdb, 0, sizeof(iegp->ipdb)); memset(iegp->ipdb, 0, sizeof(iegp->ipdb));
memset(iegp->igdb, 0, sizeof(iegp->igdb)); memset(iegp->igdb, 0, sizeof(iegp->igdb));
memset(iegp->vct, 0, sizeof(iegp->vct));