Refactoring: move generic functions to codes_util.c

This commit is contained in:
Shahram Najm 2019-12-14 21:57:35 +00:00
parent f7c372f6ce
commit 23c5685c5b
5 changed files with 85 additions and 67 deletions

View File

@ -358,6 +358,7 @@ list( APPEND grib_api_srcs
grib_iterator_class_regular.c
grib_iterator_class_space_view.c
grib_expression.c
codes_util.c
grib_util.c
bufr_util.c
string_util.c

View File

@ -373,6 +373,7 @@ libeccodes_la_prototypes= \
grib_iterator_class_regular.c \
grib_iterator_class_space_view.c \
grib_expression.c \
codes_util.c \
grib_util.c \
bufr_util.c \
string_util.c \

79
src/codes_util.c Normal file
View File

@ -0,0 +1,79 @@
/*
* Copyright 2005-2019 ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_api_internal.h"
#ifdef ECCODES_ON_WINDOWS
/* Replace C99/Unix rint() for Windows Visual C++ (only before VC++ 2013 versions) */
#if defined _MSC_VER && _MSC_VER < 1800
double rint(double x)
{
char * buf = 0;
int decimal=0, sign=0, err = 0;
double result = 0;
buf = (char*) malloc(_CVTBUFSIZE);
err = _fcvt_s(buf, _CVTBUFSIZE, x, 0, &decimal, &sign);
Assert(err == 0);
result = atof(buf);
if(sign == 1) {
result = result * -1;
}
free(buf);
return result;
}
#endif
#endif
char get_dir_separator_char(void)
{
#ifdef ECCODES_ON_WINDOWS
# define DIR_SEPARATOR_CHAR '\\'
#else
# define DIR_SEPARATOR_CHAR '/'
#endif
return DIR_SEPARATOR_CHAR;
}
char* codes_getenv(const char* name)
{
/* Look for the new ecCodes environment variable names */
/* if not found, then look for old grib_api ones for backward compatibility */
char* result = getenv(name);
if (result == NULL) {
const char* old_name = name;
/* Test the most commonly used variables first */
if (STR_EQ(name, "ECCODES_SAMPLES_PATH")) old_name="GRIB_SAMPLES_PATH";
else if (STR_EQ(name, "ECCODES_DEFINITION_PATH")) old_name="GRIB_DEFINITION_PATH";
else if (STR_EQ(name, "ECCODES_DEBUG")) old_name="GRIB_API_DEBUG";
else if (STR_EQ(name, "ECCODES_FAIL_IF_LOG_MESSAGE")) old_name="GRIB_API_FAIL_IF_LOG_MESSAGE";
else if (STR_EQ(name, "ECCODES_GRIB_WRITE_ON_FAIL")) old_name="GRIB_API_WRITE_ON_FAIL";
else if (STR_EQ(name, "ECCODES_GRIB_LARGE_CONSTANT_FIELDS")) old_name="GRIB_API_LARGE_CONSTANT_FIELDS";
else if (STR_EQ(name, "ECCODES_NO_ABORT")) old_name="GRIB_API_NO_ABORT";
else if (STR_EQ(name, "ECCODES_GRIBEX_MODE_ON")) old_name="GRIB_GRIBEX_MODE_ON";
else if (STR_EQ(name, "ECCODES_GRIB_IEEE_PACKING")) old_name="GRIB_IEEE_PACKING";
else if (STR_EQ(name, "ECCODES_IO_BUFFER_SIZE")) old_name="GRIB_API_IO_BUFFER_SIZE";
else if (STR_EQ(name, "ECCODES_LOG_STREAM")) old_name="GRIB_API_LOG_STREAM";
else if (STR_EQ(name, "ECCODES_GRIB_NO_BIG_GROUP_SPLIT")) old_name="GRIB_API_NO_BIG_GROUP_SPLIT";
else if (STR_EQ(name, "ECCODES_GRIB_NO_SPD")) old_name="GRIB_API_NO_SPD";
else if (STR_EQ(name, "ECCODES_GRIB_KEEP_MATRIX")) old_name="GRIB_API_KEEP_MATRIX";
else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_DEFINITION_PATH")) old_name="_GRIB_API_ECMWF_TEST_DEFINITION_PATH";
else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_SAMPLES_PATH")) old_name="_GRIB_API_ECMWF_TEST_SAMPLES_PATH";
else if (STR_EQ(name, "ECCODES_GRIB_JPEG")) old_name="GRIB_JPEG";
else if (STR_EQ(name, "ECCODES_GRIB_DUMP_JPG_FILE")) old_name="GRIB_DUMP_JPG_FILE";
else if (STR_EQ(name, "ECCODES_PRINT_MISSING")) old_name="GRIB_PRINT_MISSING";
result = getenv(old_name);
}
return result;
}

View File

@ -1462,6 +1462,10 @@ long grib_arguments_get_long(grib_handle *h, grib_arguments *args, int n);
double grib_arguments_get_double(grib_handle *h, grib_arguments *args, int n);
grib_expression *grib_arguments_get_expression(grib_handle *h, grib_arguments *args, int n);
/* codes_util.c */
char get_dir_separator_char(void);
char *codes_getenv(const char *name);
/* grib_util.c */
grib_handle *grib_util_sections_copy(grib_handle *hfrom, grib_handle *hto, int what, int *err);
grib_string_list *grib_util_get_param_id(const char *mars_param);
@ -1477,8 +1481,6 @@ int grib2_is_PDTN_Aerosol(long productDefinitionTemplateNumber);
int grib2_is_PDTN_AerosolOptical(long productDefinitionTemplateNumber);
int grib2_select_PDTN(int is_eps, int is_instant, int is_chemical, int is_chemical_distfn, int is_aerosol, int is_aerosol_optical);
int is_index_file(const char *filename);
char get_dir_separator_char(void);
char *codes_getenv(const char *name);
size_t sum_of_pl_array(const long* pl, size_t plsize);
/* bufr_util.c */

View File

@ -11,26 +11,6 @@
#include "grib_api_internal.h"
#include <float.h>
#ifdef ECCODES_ON_WINDOWS
/* Replace C99/Unix rint() for Windows Visual C++ (only before VC++ 2013 versions) */
#if defined _MSC_VER && _MSC_VER < 1800
double rint(double x)
{
char * buf = 0;
int decimal=0, sign=0, err = 0;
double result = 0;
buf = (char*) malloc(_CVTBUFSIZE);
err = _fcvt_s(buf, _CVTBUFSIZE, x, 0, &decimal, &sign);
Assert(err == 0);
result = atof(buf);
if(sign == 1) {
result = result * -1;
}
free(buf);
return result;
}
#endif
#endif
typedef enum {eROUND_ANGLE_UP, eROUND_ANGLE_DOWN} RoundingPolicy;
@ -2065,51 +2045,6 @@ int is_index_file(const char* filename)
return ret;
}
char get_dir_separator_char(void)
{
#ifdef ECCODES_ON_WINDOWS
# define DIR_SEPARATOR_CHAR '\\'
#else
# define DIR_SEPARATOR_CHAR '/'
#endif
return DIR_SEPARATOR_CHAR;
}
char* codes_getenv(const char* name)
{
/* Look for the new ecCodes environment variable names */
/* if not found, then look for old grib_api ones for backward compatibility */
char* result = getenv(name);
if (result == NULL) {
const char* old_name = name;
/* Test the most commonly used variables first */
if (STR_EQ(name, "ECCODES_SAMPLES_PATH")) old_name="GRIB_SAMPLES_PATH";
else if (STR_EQ(name, "ECCODES_DEFINITION_PATH")) old_name="GRIB_DEFINITION_PATH";
else if (STR_EQ(name, "ECCODES_DEBUG")) old_name="GRIB_API_DEBUG";
else if (STR_EQ(name, "ECCODES_FAIL_IF_LOG_MESSAGE")) old_name="GRIB_API_FAIL_IF_LOG_MESSAGE";
else if (STR_EQ(name, "ECCODES_GRIB_WRITE_ON_FAIL")) old_name="GRIB_API_WRITE_ON_FAIL";
else if (STR_EQ(name, "ECCODES_GRIB_LARGE_CONSTANT_FIELDS")) old_name="GRIB_API_LARGE_CONSTANT_FIELDS";
else if (STR_EQ(name, "ECCODES_NO_ABORT")) old_name="GRIB_API_NO_ABORT";
else if (STR_EQ(name, "ECCODES_GRIBEX_MODE_ON")) old_name="GRIB_GRIBEX_MODE_ON";
else if (STR_EQ(name, "ECCODES_GRIB_IEEE_PACKING")) old_name="GRIB_IEEE_PACKING";
else if (STR_EQ(name, "ECCODES_IO_BUFFER_SIZE")) old_name="GRIB_API_IO_BUFFER_SIZE";
else if (STR_EQ(name, "ECCODES_LOG_STREAM")) old_name="GRIB_API_LOG_STREAM";
else if (STR_EQ(name, "ECCODES_GRIB_NO_BIG_GROUP_SPLIT")) old_name="GRIB_API_NO_BIG_GROUP_SPLIT";
else if (STR_EQ(name, "ECCODES_GRIB_NO_SPD")) old_name="GRIB_API_NO_SPD";
else if (STR_EQ(name, "ECCODES_GRIB_KEEP_MATRIX")) old_name="GRIB_API_KEEP_MATRIX";
else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_DEFINITION_PATH")) old_name="_GRIB_API_ECMWF_TEST_DEFINITION_PATH";
else if (STR_EQ(name, "_ECCODES_ECMWF_TEST_SAMPLES_PATH")) old_name="_GRIB_API_ECMWF_TEST_SAMPLES_PATH";
else if (STR_EQ(name, "ECCODES_GRIB_JPEG")) old_name="GRIB_JPEG";
else if (STR_EQ(name, "ECCODES_GRIB_DUMP_JPG_FILE")) old_name="GRIB_DUMP_JPG_FILE";
else if (STR_EQ(name, "ECCODES_PRINT_MISSING")) old_name="GRIB_PRINT_MISSING";
result = getenv(old_name);
}
return result;
}
size_t sum_of_pl_array(const long* pl, size_t plsize)
{
long i, count=0;