diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py index 55c93dede..79f7af021 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py @@ -14,6 +14,7 @@ import code_object.constructor_function as constructor_function import code_object.function_call as function_call import code_object_converter.conversion_pack.arg_utils as arg_utils import code_object.cast_expression as cast_expression +import code_object.macro_instantation as macro_instantation from grib_accessor.grib_accessor_conversion_pack.grib_accessor_special_function_call_conversion import special_function_name_mapping from code_object.code_interface import NONE_VALUE @@ -193,3 +194,11 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv return super().validate_struct_member_access(cstruct_member_access, cppstruct_member_access) + def validate_macro_instantiation(self, cmacro_instantiation, cppmacro_instantiation): + + if cppmacro_instantiation.name == "STR_EQUAL_NOCASE": + updated_cppmacro_instantiation = macro_instantation.MacroInstantation("strcmpNoCase", cppmacro_instantiation.expression) + debug.line("validate_macro_instantiation", f"Updated macro, from=[{debug.as_debug_string(cppmacro_instantiation)}] to=[{debug.as_debug_string(updated_cppmacro_instantiation)}]") + return updated_cppmacro_instantiation + + return super().validate_macro_instantiation(cmacro_instantiation, cppmacro_instantiation) diff --git a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.cc b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.cc index 51ce46ea6..43a8a1d7e 100644 --- a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.cc +++ b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.cc @@ -58,4 +58,19 @@ long strAtoL(std::string input) return atol(input.c_str()); } +int strcmpNoCase(std::string const& s1, std::string const& s2) +{ + // For now, just using the (pointer) version from the C function: + // int strcmp_nocase(const char* s1, const char* s2); + + auto ps1 = (const unsigned char*)s1.data(); + auto ps2 = (const unsigned char*)s2.data(); + + while (tolower(*ps1) == tolower(*ps2++)) { + if (*ps1++ == '\0') + return (0); + } + return (tolower(*ps1) - tolower(*--ps2)); +} + } diff --git a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h index f7e96e914..7d2f83ed8 100644 --- a/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h +++ b/src/cpp/eccodes/accessor/AccessorUtils/ConversionHelper.h @@ -78,4 +78,6 @@ void gribLog(Args... args) } +int strcmpNoCase(std::string const& s1, std::string const& s2); + } \ No newline at end of file