From ccdd815841756208be718a45ede9af63c82396fb Mon Sep 17 00:00:00 2001 From: shahramn Date: Tue, 27 Aug 2024 17:48:39 +0100 Subject: [PATCH] Features: List of enabled ones --- src/codes_util.cc | 48 ++++++++++++++++++++++++++++------------ src/eccodes.h | 3 ++- src/eccodes_prototypes.h | 2 ++ tests/unit_tests.cc | 12 ++++++++++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/codes_util.cc b/src/codes_util.cc index 791fab3ff..78f0ca6fd 100644 --- a/src/codes_util.cc +++ b/src/codes_util.cc @@ -405,6 +405,18 @@ int compute_scaled_value_and_scale_factor( return err; } +static const char* known_features[] = { + "AEC", + "MEMFS", + "JPG", + "PNG", + "ECCODES_THREADS", + "ECCODES_OMP_THREADS", + "NETCDF", + "FORTRAN", + "GEOGRAPHY" +}; + #define NUMBER(x) (sizeof(x) / sizeof(x[0])) int codes_is_feature_enabled(const char* feature) { @@ -418,19 +430,9 @@ int codes_is_feature_enabled(const char* feature) int fortran_enabled = 0; int geography_enabled = 0; - const char* known_features[] = { - "AEC", - "MEMFS", - "JPG", - "PNG", - "ECCODES_THREADS", - "ECCODES_OMP_THREADS", - "NETCDF", - "FORTRAN", - "GEOGRAPHY" - }; int found_feature = 0; - for (size_t i = 0; i < NUMBER(known_features); ++i) { + const size_t num = NUMBER(known_features); + for (size_t i = 0; i < num; ++i) { if (STR_EQUAL(feature, known_features[i])) { found_feature = 1; break; @@ -439,7 +441,7 @@ int codes_is_feature_enabled(const char* feature) if (!found_feature) { grib_context* c = grib_context_get_default(); grib_context_log(c, GRIB_LOG_ERROR, "Unknown feature '%s'. Select one of:", feature); - for (size_t i = 0; i < NUMBER(known_features); ++i) { + for (size_t i = 0; i < num; ++i) { grib_context_log(c, GRIB_LOG_ERROR, "\t%s", known_features[i]); } return 0; @@ -456,7 +458,7 @@ int codes_is_feature_enabled(const char* feature) jpg_enabled = 1; #endif #endif -#if defined(HAVE_LIBPNG) +#if HAVE_LIBPNG png_enabled = 1; #endif #if defined(HAVE_MEMFS) @@ -508,3 +510,21 @@ int codes_is_feature_enabled(const char* feature) return 0; } + +int codes_enabled_features(char* result, size_t* length) +{ + const size_t num = NUMBER(known_features); + for (size_t i = 0; i < num; ++i) { + if (codes_is_feature_enabled(known_features[i])) { + strcat(result, known_features[i]); + strcat(result, " "); + } + } + + const size_t actual_length = strlen(result); + if (result[actual_length - 1] == ' ') + result[actual_length - 1] = '\0'; + Assert(*length >= actual_length); + *length = actual_length; + return GRIB_SUCCESS; +} diff --git a/src/eccodes.h b/src/eccodes.h index ecca55f14..18d8b4ce8 100644 --- a/src/eccodes.h +++ b/src/eccodes.h @@ -1402,7 +1402,8 @@ int codes_check_message_footer(const void* bytes, size_t length, ProductKind pro /* Features enabled */ int codes_is_feature_enabled(const char* feature); - +/* result must be allocated by the caller and its length must be large enough */ +int codes_enabled_features(char* result, size_t* length); /* --------------------------------------- */ #define CODES_UTIL_GRID_SPEC_REGULAR_LL GRIB_UTIL_GRID_SPEC_REGULAR_LL diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index 38de12e4b..8e7d1e9ab 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -858,6 +858,8 @@ bool is_sorted_ascending(double arr[], size_t n); bool is_sorted_descending(double arr[], size_t n); int compute_scaled_value_and_scale_factor(double input, int64_t scaled_value_max, int64_t scale_factor_max, int64_t* ret_value, int64_t* ret_factor); int codes_is_feature_enabled(const char* feature); +int codes_enabled_features(char* result, size_t* length); + /* grib_util.cc */ grib_handle* grib_util_sections_copy(grib_handle* hfrom, grib_handle* hto, int what, int* err); diff --git a/tests/unit_tests.cc b/tests/unit_tests.cc index 537595678..f78b39c19 100644 --- a/tests/unit_tests.cc +++ b/tests/unit_tests.cc @@ -807,6 +807,17 @@ void test_is_feature_enabled() Assert( 0 == codes_is_feature_enabled("total rubbish") ); } +void test_codes_enabled_features() +{ + printf("Running %s ...\n", __func__); + + size_t len = 512; + char* features = (char*)malloc(len * sizeof(char)); + int err = codes_enabled_features(features, &len); + Assert(!err); + printf("\tEnabled features are: '%s'\n", features); + free(features); +} int main(int argc, char** argv) { @@ -874,6 +885,7 @@ int main(int argc, char** argv) test_grib2_select_PDTN(); test_grib2_choose_PDTN(); test_is_feature_enabled(); + test_codes_enabled_features(); return 0; }