diff --git a/src/metkit/api/metkit_c.cc b/src/metkit/api/metkit_c.cc index 09d4168..85bbe1e 100644 --- a/src/metkit/api/metkit_c.cc +++ b/src/metkit/api/metkit_c.cc @@ -244,6 +244,21 @@ int metkit_request_value(const metkit_request_t* request, const char* param, int }); } +int metkit_request_values(const metkit_request_t* request, const char* param, const char** values[], size_t* numValues) { + return tryCatch([request, param, values, numValues] { + ASSERT(request); + ASSERT(param); + ASSERT(values); + ASSERT(numValues); + const std::vector& v = request->values(param); + *numValues = v.size(); + *values = new const char*[v.size()]; + for (size_t i = 0; i < v.size(); i++) { + (*values)[i] = v[i].c_str(); + } + }); +} + int metkit_request_expand(const metkit_request_t* request, metkit_request_t* expandedRequest, bool inherit, bool strict) { return tryCatch([request, expandedRequest, inherit, strict] { ASSERT(request); diff --git a/src/metkit/api/metkit_c.h b/src/metkit/api/metkit_c.h index 9bc7bba..6ccb75c 100644 --- a/src/metkit/api/metkit_c.h +++ b/src/metkit/api/metkit_c.h @@ -152,6 +152,15 @@ int metkit_request_count_values(const metkit_request_t* request, const char* par */ int metkit_request_value(const metkit_request_t* request, const char* param, int index, const char** value); +/** Returns values for specific parameter Request object + * @param request Request instance + * @param param parameter name in request + * @param values list of values for param in request + * @param numValues number of values for param in request + * @return int Error code + */ +int metkit_request_values(const metkit_request_t* request, const char* param, const char** values[], size_t* numValues); + /** Populates empty Request object by expanding existing request * @param request Request instance to be expanded * @param expandedRequest empty Request instance to be populated diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e59524a..c175786 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -71,7 +71,7 @@ ecbuild_add_test( TARGET metkit_test_gribhandle NO_AS_NEEDED ENVIRONMENT "${metkit_env}") -list(APPEND testFileSuffixes typesfactory expand request param_axis steprange_axis time hypercube type_levelist ) +list(APPEND testFileSuffixes typesfactory expand request param_axis steprange_axis time hypercube type_levelist c_api ) foreach(test IN LISTS testFileSuffixes) ecbuild_add_test( TARGET "metkit_test_${test}" diff --git a/tests/test_c_api.cc b/tests/test_c_api.cc new file mode 100644 index 0000000..f97db4e --- /dev/null +++ b/tests/test_c_api.cc @@ -0,0 +1,81 @@ +/* + * (C) Copyright 1996- 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. + */ + +/// @file test_c_api.cc +/// @date Dec 2024 +/// @author Christopher Bradley + +#include "metkit/api/metkit_c.h" +#include "eckit/testing/Test.h" + +using namespace eckit::testing; +namespace metkit::test { + +CASE( "Request get/set" ) { + + metkit_request_t* request = nullptr; + + metkit_new_request(&request); + EXPECT(request); + + // set/get verb + metkit_request_set_verb(request, "retrieve"); + const char* verb = nullptr; + metkit_request_verb(request, &verb); + EXPECT(strcmp(verb, "retrieve") == 0); + + // set array of values + const char* dates[] = {"20200101", "20200102", "20200103"}; + metkit_request_add(request, "date", dates, 3); + + // set single value + const char* expver = "xxxx"; + metkit_request_add(request, "expver", &expver, 1); + + // check values + bool has = false; + metkit_request_has_param(request, "date", &has); + EXPECT(has); + + metkit_request_has_param(request, "random", &has); + EXPECT(!has); + + size_t count = 0; + metkit_request_count_values(request, "date", &count); + EXPECT_EQUAL(count, 3); + + for (size_t i = 0; i < count; i++) { + const char* value = nullptr; + metkit_request_value(request, "date", i, &value); + EXPECT(strcmp(value, dates[i]) == 0); + } + + // all values + const char** values = nullptr; + count = 0; + metkit_request_values(request, "date", &values, &count); + EXPECT_EQUAL(count, 3); + + for (size_t i = 0; i < count; i++) { + EXPECT(strcmp(values[i], dates[i]) == 0); + } + + // done + metkit_free_request(request); +} + +//----------------------------------------------------------------------------- + +} // namespace metkit::test + +int main(int argc, char **argv) +{ + return run_tests ( argc, argv ); +}