Features: List of enabled ones

This commit is contained in:
shahramn 2024-08-27 17:48:39 +01:00
parent c4a8ab1be2
commit ccdd815841
4 changed files with 50 additions and 15 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);

View File

@ -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;
}