From d1f839f32dba4dad56654a245d759664ed5087f5 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Thu, 9 Sep 2021 15:18:30 +0200
Subject: [PATCH] cgribexlib update.

---
 ChangeLog        |   4 +
 src/calendar.c   |  96 +++++++++---------
 src/cgribex.h    |   6 +-
 src/cgribexlib.c |  56 +++++------
 src/dmemory.c    | 253 +++++++++++++++++++++--------------------------
 src/dmemory.h    |   2 +-
 src/timebase.c   |  12 +--
 src/timebase.h   |  14 +--
 8 files changed, 201 insertions(+), 242 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a251cbab8..20dc76da3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,10 @@
         * using CGRIBEX library version 2.0.0
 	* Version 2.0.0 released
 
+2021-08-13  Uwe Schulzweida
+
+	* Add FDB support (experimental)
+
 2021-07-19  Uwe Schulzweida
 
 	* instituteCompareKernel(): compare full lenght of longnames
diff --git a/src/calendar.c b/src/calendar.c
index 575eff595..41d5e30cb 100644
--- a/src/calendar.c
+++ b/src/calendar.c
@@ -1,4 +1,5 @@
 #include <limits.h>
+#include <stdio.h>
 
 #include "cdi.h"  		/* CALENDAR_ */
 #include "calendar.h"
@@ -13,65 +14,67 @@ static const int month_366[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
 
 int calendar_dpy(int calendar)
 {
-  int daysperyear = 0;
+  int daysPerYear = 0;
 
-  if      ( calendar == CALENDAR_360DAYS ) daysperyear = 360;
-  else if ( calendar == CALENDAR_365DAYS ) daysperyear = 365;
-  else if ( calendar == CALENDAR_366DAYS ) daysperyear = 366;
+  if      (calendar == CALENDAR_360DAYS) daysPerYear = 360;
+  else if (calendar == CALENDAR_365DAYS) daysPerYear = 365;
+  else if (calendar == CALENDAR_366DAYS) daysPerYear = 366;
 
-  return daysperyear;
+  return daysPerYear;
 }
 
+static const int *
+get_dayspermonth_array(int daysPerYear)
+{
+  return  (daysPerYear == 360) ? month_360 :
+          (daysPerYear == 365) ? month_365 :
+          (daysPerYear == 366) ? month_366 : NULL;
+}
 
 int days_per_month(int calendar, int year, int month)
 {
-  int daysperyear = calendar_dpy(calendar);
+  const int daysPerYear = calendar_dpy(calendar);
+  const int *daysPerMonthArray = get_dayspermonth_array(daysPerYear);
 
-  const int *dpm = (daysperyear == 360) ? month_360 : ((daysperyear == 365) ? month_365 : month_366);
+  int daysPerMonth = (daysPerMonthArray && month >= 1 && month <= 12 ) ? daysPerMonthArray[month-1] : 0;
 
-  int dayspermonth = ( month >= 1 && month <= 12 ) ? dpm[month-1] : 0;
+  if (daysPerYear == 0 && month == 2)
+    daysPerMonth = ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 29 : 28;
 
-  if (daysperyear == 0 && month == 2)
-    dayspermonth = ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 29 : 28;
-
-  return dayspermonth;
+  return daysPerMonth;
 }
 
 
 int days_per_year(int calendar, int year)
 {
-  int daysperyear = calendar_dpy(calendar);
-
-  if (daysperyear == 0)
+  int daysPerYear = calendar_dpy(calendar);
+  if (daysPerYear == 0)
     {
       if (year == 1582 && (calendar == CALENDAR_STANDARD || calendar == CALENDAR_GREGORIAN))
-        daysperyear = 355;
+        daysPerYear = 355;
       else if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
-        daysperyear = 366;
+        daysPerYear = 366;
       else
-        daysperyear = 365;
+        daysPerYear = 365;
     }
 
-  return daysperyear;
+  return daysPerYear;
 }
 
 
-static void decode_day(int dpy, int days, int *year, int *month, int *day)
+static void decode_day(int daysPerYear, int days, int *year, int *month, int *day)
 {
-  *year = (days-1) / dpy;
-  days -= (*year*dpy);
+  *year = (days-1) / daysPerYear;
+  days -= (*year * daysPerYear);
 
-  const int *dpm = NULL;
-  if      ( dpy == 360 ) dpm = month_360;
-  else if ( dpy == 365 ) dpm = month_365;
-  else if ( dpy == 366 ) dpm = month_366;
+  const int *daysPerMonthArray = get_dayspermonth_array(daysPerYear);
 
   int i = 0;
-  if ( dpm )
-    for ( i = 0; i < 12; i++ )
+  if (daysPerMonthArray)
+    for (i = 0; i < 12; i++)
       {
-	if ( days > dpm[i] ) days -= dpm[i];
-	else break;
+        if (days > daysPerMonthArray[i]) days -= daysPerMonthArray[i];
+        else break;
       }
 
   *month = i + 1;
@@ -79,17 +82,14 @@ static void decode_day(int dpy, int days, int *year, int *month, int *day)
 }
 
 
-static int64_t encode_day(int dpy, int year, int month, int day)
+static int64_t encode_day(int daysPerYear, int year, int month, int day)
 {
-  int64_t rval = (int64_t)dpy * year + day;
+  int64_t rval = (int64_t)daysPerYear * year + day;
 
-  const int *dpm = NULL;
-  if      ( dpy == 360 ) dpm = month_360;
-  else if ( dpy == 365 ) dpm = month_365;
-  else if ( dpy == 366 ) dpm = month_366;
+  const int *daysPerMonthArray = get_dayspermonth_array(daysPerYear);
 
-  if ( dpm ) for ( int i = 0; i < month-1; i++ ) rval += dpm[i];
-  if ( rval > LONG_MAX || rval < LONG_MIN ) Error("Unhandled date: %lld", rval);
+  if (daysPerMonthArray) for (int i = 0; i < month-1; i++) rval += daysPerMonthArray[i];
+  if (rval > LONG_MAX || rval < LONG_MIN) Error("Unhandled date: %lld", rval);
 
   return rval;
 }
@@ -100,7 +100,7 @@ void encode_caldaysec(int calendar, int year, int month, int day, int hour, int
 {
   const int dpy = calendar_dpy(calendar);
 
-  if ( dpy == 360 || dpy == 365 || dpy == 366 )
+  if (dpy == 360 || dpy == 365 || dpy == 366)
     *julday = encode_day(dpy, year, month, day);
   else
     *julday = encode_julday(calendar, year, month, day);
@@ -114,7 +114,7 @@ void decode_caldaysec(int calendar, int64_t julday, int secofday,
 {
   const int dpy = calendar_dpy(calendar);
 
-  if ( dpy == 360 || dpy == 365 || dpy == 366 )
+  if (dpy == 360 || dpy == 365 || dpy == 366)
     decode_day(dpy, julday, year, month, day);
   else
     decode_julday(calendar, julday, year, month, day);
@@ -166,7 +166,7 @@ int main(void)
   int64_t vdate0, vdate;
   int vtime0, vtime;
   int ijulinc;
-  int i, j = 0;
+  int j = 0;
   int year, mon, day, hour, minute, second;
   int calday, secofday;
 
@@ -178,7 +178,7 @@ int main(void)
 
   printf("start time: %8ld %4d\n", vdate0, vtime0);
 
-  for ( i = 0; i < nmin; i++ )
+  for (int i = 0; i < nmin; i++)
     {
       cdiDecodeDate(vdate0, &year, &mon, &day);
       cdiDecodeTime(vtime0, &hour, &minute, &second);
@@ -190,8 +190,7 @@ int main(void)
       vtime = sec_to_time(secofday);
 
       if ( vdate0 != vdate || vtime0 != vtime )
-	printf("%4d %8ld %4d %8ld %4d %9d %9d\n",
-	       ++j, vdate0, vtime0, vdate, vtime, calday, secofday);
+	printf("%4d %8ld %4d %8ld %4d %9d %9d\n", ++j, vdate0, vtime0, vdate, vtime, calday, secofday);
 
       year++;
       vdate0 = cdiEncodeDate(year, mon, day);
@@ -211,7 +210,7 @@ int main(void)
 
   calday = date_to_calday(calendar, vdate0);
   secofday = time_to_sec(vtime0);
-  for ( i = 0; i < nmin; i++ )
+  for (int i = 0; i < nmin; i++)
     {
       cdiDecodeDate(vdate0, &year, &mon, &day);
       cdiDecodeTime(vtime0, &hour, &minute, &second);
@@ -242,8 +241,7 @@ int main(void)
       vdate = calday_to_date(calendar, calday);
       vtime = sec_to_time(secofday);
       if ( vdate0 != vdate || vtime0 != vtime )
-	printf("%4d %8ld %4d %8ld %4d %9d %9d\n",
-	       ++j, vdate0, vtime0, vdate, vtime, calday, secofday);
+	printf("%4d %8ld %4d %8ld %4d %9d %9d\n", ++j, vdate0, vtime0, vdate, vtime, calday, secofday);
     }
 
   printf("stop time: %8ld %4d\n", vdate0, vtime0);
@@ -257,7 +255,6 @@ int main(void)
 int main(void)
 {
   int calendar = CALENDAR_STANDARD;
-  int i;
   int calday, secofday;
   int year, month, day, hour, minute, second;
   int value = 30;
@@ -275,9 +272,8 @@ int main(void)
   decode_caldaysec(calendar, calday, secofday, &year, &month, &day, &hour, &minute, &second);
   printf("%d/%02d/%02d %02d:%02d:%02d   %d %d\n", year, month, day, hour, minute, second, calday, secofday);
 
-  for ( i = 0; i < 420; i++ )
+  for (int i = 0; i < 420; i++)
     {
-
       decode_caldaysec(calendar, calday, secofday, &year, &month, &day, &hour, &minute, &second);
       printf("%2d %d/%02d/%02d %02d:%02d:%02d\n", i, year, month, day, hour, minute, second);
       julday_add_seconds(value*factor, &calday, &secofday);
diff --git a/src/cgribex.h b/src/cgribex.h
index a66eed116..dee1929d2 100644
--- a/src/cgribex.h
+++ b/src/cgribex.h
@@ -199,9 +199,9 @@ void  gribSetCalendar(int calendar);
 
 void  gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startTime);
 void  gribDateTime(int *isec1, int *date, int *time);
-int   gribRefDate(int *isec1);
-int   gribRefTime(int *isec1);
-bool  gribTimeIsFC(int *isec1);
+int   gribRefDate(const int *isec1);
+int   gribRefTime(const int *isec1);
+bool  gribTimeIsFC(const int *isec1);
 
 void  gribPrintSec0(int *isec0);
 void  gribPrintSec1(int *isec0, int *isec1);
diff --git a/src/cgribexlib.c b/src/cgribexlib.c
index 0f15fad92..d331f8a08 100644
--- a/src/cgribexlib.c
+++ b/src/cgribexlib.c
@@ -1,5 +1,5 @@
 
-/* Automatically generated by m214003 at 2021-06-30, do not edit */
+/* Automatically generated by m214003 at 2021-09-09, do not edit */
 
 /* CGRIBEXLIB_VERSION="2.0.0" */
 
@@ -43,7 +43,7 @@
 #ifndef GRIB_INT_H
 #define GRIB_INT_H
 
-#if defined (HAVE_CONFIG_H)
+#ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
@@ -55,18 +55,18 @@
 #include <float.h>
 
 
-#if ! defined   (CGRIBEX_H)
+#ifndef  CGRIBEX_H
 #include "cgribex.h"
 #endif
-#if ! defined   (ERROR_H)
+#ifndef  ERROR_H
 #include "error.h"
 #endif
-#if ! defined   (DTYPES_H)
+#ifndef TYPES_H
 #include "dtypes.h"
 #endif
 
 
-#if ! defined   (UCHAR)
+#ifndef UCHAR
 #define  UCHAR  unsigned char
 #endif
 
@@ -148,7 +148,7 @@ gribrec_len(unsigned b1, unsigned b2, unsigned b3)
     due to the count being only 24 bits. It is only possible because
     the (default) rounding for GRIB products is 120 bytes.
   */
-  int needRescaling = b1 & (1 << 7);
+  const int needRescaling = b1 & (1 << 7);
 
   int gribsize = (int)((((b1&127) << 16)+(b2<<8) + b3));
 
@@ -1973,28 +1973,26 @@ double decfp2(int kexp, int kmant)
 
   return pval;
 } /* decfp2 */
-#include <stdint.h>
-#include <string.h>
 #include <stdarg.h>
+#include <stdint.h>
 
 
 static
-void gribDecodeRefDate(int *isec1, int *year, int *month, int *day)
+void gribDecodeRefDate(const int *isec1, int *year, int *month, int *day)
 {
-  int century = ISEC1_Century;
-  int ryear   = ISEC1_Year;
+  int ryear = ISEC1_Year;
 
-  if ( century < 0 ) century = -century;
+  int century = ISEC1_Century;
+  if (century < 0) century = -century;
   century -= 1;
 
-  if ( century == -255 && ryear == 127 )
+  if (century == -255 && ryear == 127)
     {
-      century = 0;
       ryear = 0;
     }
   else
     {
-      /* if ( century != 0 ) */
+      // if ( century != 0 )
       {
         if ( ryear == 100 )
           {
@@ -2008,7 +2006,9 @@ void gribDecodeRefDate(int *isec1, int *year, int *month, int *day)
             if ( ISEC1_Century < 0 ) ryear = -ryear;
           }
         else
-          ryear = 1;
+          {
+            ryear = 1;
+          }
       }
     }
 
@@ -2018,7 +2018,7 @@ void gribDecodeRefDate(int *isec1, int *year, int *month, int *day)
 }
 
 
-int gribRefDate(int *isec1)
+int gribRefDate(const int *isec1)
 {
   int ryear, rmonth, rday;
   gribDecodeRefDate(isec1, &ryear, &rmonth, &rday);
@@ -2026,7 +2026,7 @@ int gribRefDate(int *isec1)
 }
 
 static
-void gribDecodeRefTime(int *isec1, int *hour, int *minute, int *second)
+void gribDecodeRefTime(const int *isec1, int *hour, int *minute, int *second)
 {
   *hour = ISEC1_Hour;
   *minute = ISEC1_Minute;
@@ -2034,7 +2034,7 @@ void gribDecodeRefTime(int *isec1, int *hour, int *minute, int *second)
 }
 
 
-int gribRefTime(int *isec1)
+int gribRefTime(const int *isec1)
 {
   int rhour, rminute, rsecond;
   gribDecodeRefTime(isec1, &rhour, &rminute, &rsecond);
@@ -2042,15 +2042,15 @@ int gribRefTime(int *isec1)
 }
 
 
-bool gribTimeIsFC(int *isec1)
+bool gribTimeIsFC(const int *isec1)
 {
   bool isFC = false;
 
-  int time_period = (ISEC1_TimeRange == 10) ? (ISEC1_TimePeriod1<<8) + ISEC1_TimePeriod2 : ISEC1_TimePeriod1;
+  const int time_period = (ISEC1_TimeRange == 10) ? (ISEC1_TimePeriod1<<8) + ISEC1_TimePeriod2 : ISEC1_TimePeriod1;
 
-  if ( time_period > 0 && ISEC1_Day > 0 )
+  if (time_period > 0 && ISEC1_Day > 0)
     {
-      if ( ISEC1_TimeRange == 0 || ISEC1_TimeRange == 10 ) isFC = true;
+      isFC = (ISEC1_TimeRange == 0 || ISEC1_TimeRange == 10);
     }
 
   return isFC;
@@ -2109,8 +2109,8 @@ void gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startT
 
   if ( time_period > 0 && rday > 0 )
     {
-      int64_t julday;
-      int secofday;
+      int64_t julday = 0;
+      int secofday = 0;
       encode_caldaysec(CGRIBEX_grib_calendar, ryear, rmonth, rday, rhour, rminute, rsecond, &julday, &secofday);
 
       int time_unit_factor = getTimeUnitFactor(ISEC1_TimeUnit);
@@ -2130,10 +2130,8 @@ void gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startT
     }
 
   // printf("new %d/%d/%d %d:%d\n", ryear, rmonth, rday, rhour, rminute);
-  *date = cdiEncodeDate(ryear, rmonth, rday);
+  *date = (int)cdiEncodeDate(ryear, rmonth, rday);
   *time = cdiEncodeTime(rhour, rminute, 0);
-
-  return;
 }
 
 
diff --git a/src/dmemory.c b/src/dmemory.c
index 107587010..f1d172323 100644
--- a/src/dmemory.c
+++ b/src/dmemory.c
@@ -10,17 +10,17 @@
 #include <errno.h>
 
 #if !defined(HAVE_CONFIG_H) && !defined(HAVE_MALLOC_H) && defined(SX)
-#  define  HAVE_MALLOC_H
+#define  HAVE_MALLOC_H
 #endif
 
 #if  defined(HAVE_MALLOC_H)
-#  include <malloc.h>
+#include <malloc.h>
 #endif
 
 #include "dmemory.h"
 
 enum             {MALLOC_FUNC=0, CALLOC_FUNC, REALLOC_FUNC, FREE_FUNC};
-static const char *memfunc[] = {"Malloc", "Calloc", "Realloc", "Free"};
+static const char *const memfunc[] = {"Malloc", "Calloc", "Realloc", "Free"};
 
 #undef   MEM_UNDEFID
 #define  MEM_UNDEFID  -1
@@ -58,8 +58,8 @@ static
 const char *get_filename(const char *file)
 {
   const char *fnptr = strrchr(file, '/');
-  if ( fnptr ) fnptr++;
-  else         fnptr = (char *) file;
+  if (fnptr) fnptr++;
+  else       fnptr = (char *) file;
 
   return fnptr;
 }
@@ -70,27 +70,28 @@ void memDebug(int debug)
   MEM_Debug = debug;
 }
 
-/* If we're not using GNU C, elide __attribute__ */
+// If we're not using GNU C, elide __attribute__
 #if ! defined __GNUC__ && ! defined __attribute__
-#  define  __attribute__(x)  /*NOTHING*/
+#define  __attribute__(x)  /*NOTHING*/
 #endif
 
 static
 void memInternalProblem(const char *caller, const char *fmt, ...)
   __attribute__((noreturn));
+
 static
 void memError(const char *caller, const char *file, int line, size_t size)
   __attribute__((noreturn));
 
 static
-void memInternalProblem(const char *functionname, const char *fmt, ...)
+void memInternalProblem(const char *caller, const char *fmt, ...)
 {
   va_list args;
 
   va_start(args, fmt);
 
   printf("\n");
-   fprintf(stderr, "Internal problem (%s) : ", functionname);
+   fprintf(stderr, "Internal problem (%s) : ", caller);
   vfprintf(stderr, fmt, args);
    fprintf(stderr, "\n");
 
@@ -100,42 +101,41 @@ void memInternalProblem(const char *functionname, const char *fmt, ...)
 }
 
 static
-void memError(const char *functionname, const char *file, int line, size_t size)
+void memError(const char *caller, const char *file, int line, size_t size)
 {
   fputs("\n", stdout);
   fprintf(stderr, "Error (%s) : Allocation of %zu bytes failed. [ line %d file %s ]\n",
-	  functionname, size, line, get_filename(file));
+	  caller, size, line, get_filename(file));
 
-  if ( errno ) perror("System error message ");
+  if (errno) perror("System error message ");
 
   exit(EXIT_FAILURE);
 }
 
 static
 void memListPrintEntry(int mtype, int item, size_t size, void *ptr,
-		       const char *functionname, const char *file, int line)
+		       const char *caller, const char *file, int line)
 {
   fprintf(stderr, "[%-7s ", memfunc[mtype]);
 
   fprintf(stderr, "memory item %3d ", item);
   fprintf(stderr, "(%6zu byte) ", size);
   fprintf(stderr, "at %p", ptr);
-  if ( file != NULL )
+  if (file != NULL)
     {
       fprintf(stderr, " line %4d", line);
       fprintf(stderr, " file %s", get_filename(file));
     }
-  if ( functionname != NULL )
-    fprintf(stderr, " (%s)", functionname);
+  if (caller!= NULL) fprintf(stderr, " (%s)", caller);
   fprintf(stderr, "]\n");
 }
 
 static
 void memListPrintTable(void)
 {
-  if ( MemObjs ) fprintf(stderr, "\nMemory table:\n");
+  if (MemObjs) fprintf(stderr, "\nMemory table:\n");
 
-  for ( size_t memID = 0; memID < memTableSize; memID++ )
+  for (size_t memID = 0; memID < memTableSize; memID++)
     {
       if ( memTable[memID].item != MEM_UNDEFID )
         memListPrintEntry(memTable[memID].mtype, memTable[memID].item,
@@ -144,7 +144,7 @@ void memListPrintTable(void)
                           memTable[memID].filename, memTable[memID].line);
     }
 
-  if ( MemObjs )
+  if (MemObjs)
     {
       fprintf(stderr, "  Memory access             : %6u\n", (unsigned) memAccess);
       fprintf(stderr, "  Maximum objects           : %6zu\n", memTableSize);
@@ -178,17 +178,15 @@ void memListPrintTable(void)
 static
 void memGetDebugLevel(void)
 {
-  const char *envstr;
-
-  envstr = getenv("MEMORY_INFO");
-  if ( envstr && isdigit((int) envstr[0]) ) MEM_Info = atoi(envstr);
+  const char *envstr = getenv("MEMORY_INFO");
+  if (envstr && isdigit((int) envstr[0])) MEM_Info = atoi(envstr);
 
   envstr = getenv("MEMORY_DEBUG");
-  if ( envstr && isdigit((int) envstr[0]) ) MEM_Debug = atoi(envstr);
+  if (envstr && isdigit((int) envstr[0])) MEM_Debug = atoi(envstr);
 
-  if ( MEM_Debug && !MEM_Info ) MEM_Info = 1;
+  if (MEM_Debug && !MEM_Info) MEM_Info = 1;
 
-  if ( MEM_Info ) atexit(memListPrintTable);
+  if (MEM_Info) atexit(memListPrintTable);
 }
 
 static
@@ -196,7 +194,7 @@ void memInit(void)
 {
   static int initDebugLevel = 0;
 
-  if ( ! initDebugLevel )
+  if (! initDebugLevel)
     {
       memGetDebugLevel();
       initDebugLevel = 1;
@@ -207,15 +205,15 @@ static
 int memListDeleteEntry(void *ptr, size_t *size)
 {
   int item = MEM_UNDEFID;
-  size_t memID;
+  size_t memID = 0;
 
-  for ( memID = 0; memID < memTableSize; memID++ )
+  for (memID = 0; memID < memTableSize; memID++)
     {
-      if ( memTable[memID].item == MEM_UNDEFID ) continue;
-      if ( memTable[memID].ptr == ptr ) break;
+      if (memTable[memID].item == MEM_UNDEFID) continue;
+      if (memTable[memID].ptr == ptr) break;
     }
 
-  if ( memID != memTableSize )
+  if (memID != memTableSize)
     {
       MemObjs--;
       MemUsed -= memTable[memID].size * memTable[memID].nobj;
@@ -230,8 +228,7 @@ int memListDeleteEntry(void *ptr, size_t *size)
 static
 void memTableInitEntry(size_t memID)
 {
-  if ( memID >= memTableSize )
-    memInternalProblem(__func__, "memID %d undefined!", memID);
+  if (memID >= memTableSize) memInternalProblem(__func__, "memID %d undefined!", memID);
 
   memTable[memID].ptr    = NULL;
   memTable[memID].item   = MEM_UNDEFID;
@@ -241,6 +238,41 @@ void memTableInitEntry(size_t memID)
   memTable[memID].line   = MEM_UNDEFID;
 }
 
+static void
+set_filename(const char *file, char *memEntyFilename)
+{
+  if (file)
+    {
+      const char *filename = get_filename(file);
+      size_t len = strlen(filename);
+      if (len > MEM_MAXNAME-1) len = MEM_MAXNAME-1;
+
+      (void) memcpy(memEntyFilename, filename, len);
+      memEntyFilename[len] = '\0';
+    }
+  else
+    {
+      (void) strcpy(memEntyFilename, "unknown");
+    }
+}
+
+static void
+set_functionname(const char *functionname, char *memEntyFunctionname)
+{
+  if (functionname)
+    {
+      size_t len = strlen(functionname);
+      if (len > MEM_MAXNAME-1) len = MEM_MAXNAME-1;
+
+      (void) memcpy(memEntyFunctionname, functionname, len);
+      memEntyFunctionname[len] = '\0';
+    }
+  else
+    {
+      (void) strcpy(memEntyFunctionname, "unknown");
+    }
+}
+
 static
 int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
 		    const char *functionname, const char *file, int line)
@@ -248,40 +280,34 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
   static int item = 0;
   size_t memID = 0;
 
-  /*
-    Look for a free slot in memTable.
-    (Create the table the first time through).
-  */
-  if ( memTableSize == 0 )
+  // Look for a free slot in memTable (Create the table the first time through).
+  if (memTableSize == 0)
     {
       memTableSize = 8;
       size_t memSize = memTableSize * sizeof(MemTable_t);
       memTable = (MemTable_t *) malloc(memSize);
-      if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
+      if(memTable == NULL) memError(__func__, __FILE__, __LINE__, memSize);
 
-      for ( size_t i = 0; i < memTableSize; i++ )
-	memTableInitEntry(i);
+      for (size_t i = 0; i < memTableSize; i++) memTableInitEntry(i);
     }
   else
     {
-      while ( memID < memTableSize )
+      while (memID < memTableSize)
 	{
-	  if ( memTable[memID].item == MEM_UNDEFID ) break;
+	  if (memTable[memID].item == MEM_UNDEFID) break;
 	  memID++;
 	}
     }
-  /*
-    If the table overflows, double its size.
-  */
-  if ( memID == memTableSize )
+
+  // If the table overflows, double its size.
+  if (memID == memTableSize)
     {
       memTableSize = 2*memTableSize;
       size_t memSize = memTableSize*sizeof(MemTable_t);
       memTable = (MemTable_t*) realloc(memTable, memSize);
-      if ( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
+      if (memTable == NULL) memError(__func__, __FILE__, __LINE__, memSize);
 
-      for ( size_t i = memID; i < memTableSize; i++ )
-	memTableInitEntry(i);
+      for (size_t i = memID; i < memTableSize; i++) memTableInitEntry(i);
     }
 
   memTable[memID].item  = item;
@@ -291,37 +317,13 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
   memTable[memID].mtype = mtype;
   memTable[memID].line  = line;
 
-  if ( file )
-    {
-      const char *filename = get_filename(file);
-      size_t len = strlen(filename);
-      if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
-
-      (void) memcpy(memTable[memID].filename, filename, len);
-      memTable[memID].filename[len] = '\0';
-    }
-  else
-    {
-      (void) strcpy(memTable[memID].filename, "unknown");
-    }
-
-  if ( functionname )
-    {
-      size_t len = strlen(functionname);
-      if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
-
-      (void) memcpy(memTable[memID].functionname, functionname, len);
-      memTable[memID].functionname[len] = '\0';
-    }
-  else
-    {
-      (void) strcpy(memTable[memID].functionname, "unknown");
-    }
+  set_filename(file, memTable[memID].filename);
+  set_functionname(functionname, memTable[memID].functionname);
 
   MaxMemObjs++;
   MemObjs++;
   MemUsed += size*nobj;
-  if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;
+  if (MemUsed > MaxMemUsed) MaxMemUsed = MemUsed;
 
   return item++;
 }
@@ -333,23 +335,21 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size,
   int item = MEM_UNDEFID;
   size_t memID = 0;
 
-  while( memID < memTableSize )
+  while (memID < memTableSize)
     {
-      if ( memTable[memID].item != MEM_UNDEFID )
-	if ( memTable[memID].ptr == ptrold ) break;
+      if (memTable[memID].item != MEM_UNDEFID && memTable[memID].ptr == ptrold) break;
       memID++;
     }
 
-  if ( memID == memTableSize )
+  if (memID == memTableSize)
     {
-      if ( ptrold != NULL )
-	memInternalProblem(__func__, "Item at %p not found.", ptrold);
+      if (ptrold != NULL) memInternalProblem(__func__, "Item at %p not found.", ptrold);
     }
   else
     {
       item = memTable[memID].item;
 
-      size_t sizeold = memTable[memID].size*memTable[memID].nobj;
+      const size_t sizeold = memTable[memID].size*memTable[memID].nobj;
 
       memTable[memID].ptr   = ptr;
       memTable[memID].size  = size;
@@ -357,36 +357,12 @@ int memListChangeEntry(void *ptrold, void *ptr, size_t size,
       memTable[memID].mtype = REALLOC_FUNC;
       memTable[memID].line  = line;
 
-      if ( file )
-	{
-          const char *filename = get_filename(file);
-	  size_t len = strlen(filename);
-	  if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
-
-	  (void) memcpy(memTable[memID].filename, filename, len);
-	  memTable[memID].filename[len] = '\0';
-	}
-      else
-	{
-	  (void) strcpy(memTable[memID].filename, "unknown");
-	}
-
-      if ( functionname )
-	{
-	  size_t len = strlen(functionname);
-	  if ( len > MEM_MAXNAME-1 ) len = MEM_MAXNAME-1;
-
-	  (void) memcpy(memTable[memID].functionname, functionname, len);
-	  memTable[memID].functionname[len] = '\0';
-	}
-      else
-	{
-	  (void) strcpy(memTable[memID].functionname, "unknown");
-	}
+      set_filename(file, memTable[memID].filename);
+      set_functionname(functionname, memTable[memID].functionname);
 
       MemUsed -= sizeold;
       MemUsed += size;
-      if ( MemUsed > MaxMemUsed ) MaxMemUsed = MemUsed;
+      if (MemUsed > MaxMemUsed) MaxMemUsed = MemUsed;
     }
 
   return item;
@@ -399,22 +375,21 @@ void *memCalloc(size_t nobjs, size_t size, const char *file, const char *functio
 
   memInit();
 
-  if ( nobjs*size > 0 )
+  if (nobjs*size > 0)
     {
       ptr = calloc(nobjs, size);
 
-      if ( MEM_Info )
+      if (MEM_Info)
 	{
 	  memAccess++;
 
           int item = MEM_UNDEFID;
-	  if ( ptr ) item = memListNewEntry(CALLOC_FUNC, ptr, size, nobjs, functionname, file, line);
+	  if (ptr) item = memListNewEntry(CALLOC_FUNC, ptr, size, nobjs, functionname, file, line);
 
 	  if ( MEM_Debug ) memListPrintEntry(CALLOC_FUNC, item, size*nobjs, ptr, functionname, file, line);
 	}
 
-      if ( ptr == NULL && dmemory_ExitOnError )
-	memError(functionname, file, line, size*nobjs);
+      if (ptr == NULL && dmemory_ExitOnError) memError(functionname, file, line, size*nobjs);
     }
   else
     fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", functionname, line, file);
@@ -429,22 +404,21 @@ void *memMalloc(size_t size, const char *file, const char *functionname, int lin
 
   memInit();
 
-  if ( size > 0 )
+  if (size > 0)
     {
       ptr = malloc(size);
 
-      if ( MEM_Info )
+      if (MEM_Info)
 	{
 	  memAccess++;
 
           int item = MEM_UNDEFID;
-	  if ( ptr ) item = memListNewEntry(MALLOC_FUNC, ptr, size, 1, functionname, file, line);
+	  if (ptr )item = memListNewEntry(MALLOC_FUNC, ptr, size, 1, functionname, file, line);
 
-	  if ( MEM_Debug ) memListPrintEntry(MALLOC_FUNC, item, size, ptr, functionname, file, line);
+	  if (MEM_Debug) memListPrintEntry(MALLOC_FUNC, item, size, ptr, functionname, file, line);
 	}
 
-      if ( ptr == NULL && dmemory_ExitOnError )
-	memError(functionname, file, line, size);
+      if (ptr == NULL && dmemory_ExitOnError) memError(functionname, file, line, size);
     }
   else
     fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", functionname, line, file);
@@ -459,27 +433,25 @@ void *memRealloc(void *ptrold, size_t size, const char *file, const char *functi
 
   memInit();
 
-  if ( size > 0 )
+  if (size > 0)
     {
       ptr = realloc(ptrold, size);
 
-      if ( MEM_Info )
+      if (MEM_Info)
 	{
 	  memAccess++;
 
           int item = MEM_UNDEFID;
-	  if ( ptr )
+	  if (ptr)
 	    {
 	      item = memListChangeEntry(ptrold, ptr, size, functionname, file, line);
-
-	      if ( item == MEM_UNDEFID ) item = memListNewEntry(REALLOC_FUNC, ptr, size, 1, functionname, file, line);
+	      if (item == MEM_UNDEFID) item = memListNewEntry(REALLOC_FUNC, ptr, size, 1, functionname, file, line);
 	    }
 
-	  if ( MEM_Debug ) memListPrintEntry(REALLOC_FUNC, item, size, ptr, functionname, file, line);
+	  if (MEM_Debug) memListPrintEntry(REALLOC_FUNC, item, size, ptr, functionname, file, line);
 	}
 
-      if ( ptr == NULL && dmemory_ExitOnError )
-	memError(functionname, file, line, size);
+      if (ptr == NULL && dmemory_ExitOnError) memError(functionname, file, line, size);
     }
   else
     fprintf(stderr, "Warning (%s) : Allocation of 0 bytes! [ line %d file %s ]\n", functionname, line, get_filename(file));
@@ -492,18 +464,17 @@ void memFree(void *ptr, const char *file, const char *functionname, int line)
 {
   memInit();
 
-  if ( MEM_Info )
+  if (MEM_Info)
     {
-      int item;
-      size_t size;
-
-      if ( (item = memListDeleteEntry(ptr, &size)) >= 0 )
+      size_t size = 0;
+      const int item = memListDeleteEntry(ptr, &size);
+      if (item >= 0)
 	{
-	  if ( MEM_Debug ) memListPrintEntry(FREE_FUNC, item, size, ptr, functionname, file, line);
+	  if (MEM_Debug) memListPrintEntry(FREE_FUNC, item, size, ptr, functionname, file, line);
 	}
       else
 	{
-	  if ( ptr && MEM_Debug  )
+	  if (ptr && MEM_Debug)
 	    fprintf(stderr, "%s info: memory entry at %p not found. [line %4d file %s (%s)]\n",
 		    __func__, ptr, line, get_filename(file), functionname);
 	}
@@ -516,9 +487,9 @@ void memFree(void *ptr, const char *file, const char *functionname, int line)
 size_t memTotal(void)
 {
   size_t memtotal = 0;
-#if  defined  (HAVE_MALLINFO)
+#ifdef HAVE_MALLINFO
   struct mallinfo meminfo = mallinfo();
-  if ( MEM_Debug )
+  if (MEM_Debug)
     {
       fprintf(stderr, "arena      %8zu (non-mmapped space allocated from system)\n", (size_t)meminfo.arena);
       fprintf(stderr, "ordblks    %8zu (number of free chunks)\n", (size_t)meminfo.ordblks);
@@ -532,7 +503,7 @@ size_t memTotal(void)
       fprintf(stderr, "Memory in use:   %8zu bytes\n", (size_t) meminfo.usmblks + (size_t)meminfo.uordblks);
       fprintf(stderr, "Total heap size: %8zu bytes\n", (size_t) meminfo.arena);
 
-      /* malloc_stats(); */
+      // malloc_stats();
     }
   memtotal = (size_t)meminfo.arena;
 #endif
diff --git a/src/dmemory.h b/src/dmemory.h
index 55537408e..42a3eb43a 100644
--- a/src/dmemory.h
+++ b/src/dmemory.h
@@ -21,7 +21,7 @@ extern void    memExitOnError(void);
 #if  defined  DEBUG_MEMORY
 
 extern void   *memRealloc(void *ptr, size_t size, const char *file, const char *functionname, int line);
-extern void   *memCalloc (size_t nmemb, size_t size, const char *file, const char *functionname, int line);
+extern void   *memCalloc (size_t nobjs, size_t size, const char *file, const char *functionname, int line);
 extern void   *memMalloc (size_t size, const char *file, const char *functionname, int line);
 extern void    memFree   (void *ptr, const char *file, const char *functionname, int line);
 
diff --git a/src/timebase.c b/src/timebase.c
index 9c4509e11..0b6039ab2 100644
--- a/src/timebase.c
+++ b/src/timebase.c
@@ -3,12 +3,12 @@
 #include "cdi.h"
 #include "timebase.h"
 
-/* convert Julian date into year, months, day */
+// convert Julian date into year, months, day
 void decode_julday(int calendar,
 		   int64_t julday, /* Julian day number to convert */
-		   int *year,	   /* Gregorian year (out)         */
+                   int *year,      /* Gregorian year (out)         */
 		   int *mon,	   /* Gregorian month (1-12) (out) */
-		   int *day)       /* Gregorian day (1-31) (out)   */
+                   int *day)       /* Gregorian day (1-31) (out)   */
 {
   int64_t a = julday;
 
@@ -198,7 +198,7 @@ int main(void)
 
   printf("start time: %8ld %4d\n", vdate0, vtime0);
 
-  for ( int i = 0; i < nmin; i++ )
+  for (int i = 0; i < nmin; i++)
     {
       cdiDecodeDate(vdate0, &year, &mon, &day);
       cdiDecodeTime(vtime0, &hour, &minute, &second);
@@ -231,7 +231,7 @@ int main(void)
 
   julday = date_to_julday(calendar, vdate0);
   secofday = time_to_sec(vtime0);
-  for ( int i = 0; i < nmin; i++ )
+  for (int i = 0; i < nmin; i++)
     {
       cdiDecodeDate(vdate0, &year, &mon, &day);
       cdiDecodeTime(vtime0, &hour, &minute, &second);
@@ -291,7 +291,7 @@ int main(void)
   decode_juldaysec(calendar, julday, secofday, &year, &month, &day, &hour, &minute, &second);
   printf("%d/%02d/%02d %02d:%02d:%02d   %d %d\n", year, month, day, hour, minute, second, julday, secofday);
 
-  for ( int i = 0; i < 420; i++ )
+  for (int i = 0; i < 420; i++)
     {
 
       decode_juldaysec(calendar, julday, secofday, &year, &month, &day, &hour, &minute, &second);
diff --git a/src/timebase.h b/src/timebase.h
index b114ab0bb..64362eaa7 100644
--- a/src/timebase.h
+++ b/src/timebase.h
@@ -7,8 +7,8 @@
 extern "C" {
 #endif
 
-/* date format:  YYYYMMDD */
-/* time format:  hhmmss   */
+// date format:  YYYYMMDD
+// time format:  hhmmss
 
 void decode_julday(int calendar, int64_t julday, int *year, int *mon, int *day);
 int64_t encode_julday(int calendar, int year, int month, int day);
@@ -31,13 +31,3 @@ void decode_juldaysec(int calendar, int64_t julday, int secofday, int *year, int
 #endif
 
 #endif /* TIMEBASE_H */
-
-/*
- * Local Variables:
- * c-file-style: "Java"
- * c-basic-offset: 2
- * indent-tabs-mode: nil
- * show-trailing-whitespace: t
- * require-trailing-newline: t
- * End:
- */
-- 
GitLab