diff --git a/c/interactor_c_api.cxx b/c/interactor_c_api.cxx index 082482b12e..fbd27f8f22 100644 --- a/c/interactor_c_api.cxx +++ b/c/interactor_c_api.cxx @@ -359,7 +359,15 @@ void f3d_interactor_init_commands(f3d_interactor_t* interactor) } f3d::interactor* cpp_interactor = reinterpret_cast(interactor); - cpp_interactor->initCommands(); + + try + { + cpp_interactor->initCommands(); + } + catch (const std::exception& e) + { + f3d::log::warn("Failed to add some of the default commands to the interactor: ", e.what()); + } } //---------------------------------------------------------------------------- @@ -382,7 +390,14 @@ void f3d_interactor_add_command(f3d_interactor_t* interactor, const char* action callback(c_args.data(), static_cast(c_args.size()), user_data); }; - cpp_interactor->addCommand(action, cpp_callback); + try + { + cpp_interactor->addCommand(action, cpp_callback); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -406,15 +421,24 @@ char** f3d_interactor_get_command_actions(f3d_interactor_t* interactor, int* cou return nullptr; } - char** result = new char*[actions.size()]; + try + { + char** result = new char*[actions.size()]; + + for (size_t i = 0; i < actions.size(); ++i) + { + result[i] = new char[actions[i].length() + 1]; + std::strcpy(result[i], actions[i].c_str()); + } - for (size_t i = 0; i < actions.size(); ++i) + return result; + } + catch (const std::bad_alloc& e) { - result[i] = new char[actions[i].length() + 1]; - std::strcpy(result[i], actions[i].c_str()); + f3d::log::error("Failed to allocate memory for char**: ", e.what()); } - return result; + return nullptr; } //---------------------------------------------------------------------------- @@ -439,7 +463,17 @@ int f3d_interactor_trigger_command( } f3d::interactor* cpp_interactor = reinterpret_cast(interactor); - return cpp_interactor->triggerCommand(command, keep_comments != 0) ? 1 : 0; + + try + { + return cpp_interactor->triggerCommand(command, keep_comments != 0) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -451,7 +485,15 @@ void f3d_interactor_init_bindings(f3d_interactor_t* interactor) } f3d::interactor* cpp_interactor = reinterpret_cast(interactor); - cpp_interactor->initBindings(); + + try + { + cpp_interactor->initBindings(); + } + catch (const std::exception& e) + { + f3d::log::warn("Failed to add some default bindings to the interactor: ", e.what()); + } } //---------------------------------------------------------------------------- @@ -479,8 +521,16 @@ void f3d_interactor_add_binding(f3d_interactor_t* interactor, const f3d_interact std::string cpp_group = group ? group : ""; - cpp_interactor->addBinding(cpp_bind, cpp_commands, cpp_group, nullptr, - static_cast(type), notify != 0); + try + { + + cpp_interactor->addBinding(cpp_bind, cpp_commands, cpp_group, nullptr, + static_cast(type), notify != 0); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -519,15 +569,24 @@ char** f3d_interactor_get_bind_groups(f3d_interactor_t* interactor, int* count) return nullptr; } - char** result = new char*[groups.size()]; + try + { + char** result = new char*[groups.size()]; + + for (size_t i = 0; i < groups.size(); ++i) + { + result[i] = new char[groups[i].length() + 1]; + std::strcpy(result[i], groups[i].c_str()); + } - for (size_t i = 0; i < groups.size(); ++i) + return result; + } + catch (const std::bad_alloc& e) { - result[i] = new char[groups[i].length() + 1]; - std::strcpy(result[i], groups[i].c_str()); + f3d::log::error("Failed to allocate memory for char**: ", e.what()); } - return result; + return nullptr; } //---------------------------------------------------------------------------- @@ -543,25 +602,39 @@ f3d_interaction_bind_t* f3d_interactor_get_binds_for_group( return nullptr; } - const f3d::interactor* cpp_interactor = reinterpret_cast(interactor); - std::vector binds = cpp_interactor->getBindsForGroup(group); - - *count = static_cast(binds.size()); - if (binds.empty()) + try { - return nullptr; - } + const f3d::interactor* cpp_interactor = reinterpret_cast(interactor); + std::vector binds = cpp_interactor->getBindsForGroup(group); + + *count = static_cast(binds.size()); + if (binds.empty()) + { + return nullptr; + } + + f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()]; - f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()]; + for (size_t i = 0; i < binds.size(); ++i) + { + result[i].mod = static_cast(binds[i].mod); + std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1); + result[i].inter[sizeof(result[i].inter) - 1] = '\0'; + } - for (size_t i = 0; i < binds.size(); ++i) + return result; + } + catch (const f3d::interactor::does_not_exists_exception& e) { - result[i].mod = static_cast(binds[i].mod); - std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1); - result[i].inter[sizeof(result[i].inter) - 1] = '\0'; + f3d::log::warn(e.what()); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for f3d_interaction_bind_t*: ", e.what()); } - return result; + *count = 0; + return nullptr; } //---------------------------------------------------------------------------- @@ -585,16 +658,25 @@ f3d_interaction_bind_t* f3d_interactor_get_binds(f3d_interactor_t* interactor, i return nullptr; } - f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()]; + try + { + f3d_interaction_bind_t* result = new f3d_interaction_bind_t[binds.size()]; + + for (size_t i = 0; i < binds.size(); ++i) + { + result[i].mod = static_cast(binds[i].mod); + std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1); + result[i].inter[sizeof(result[i].inter) - 1] = '\0'; + } - for (size_t i = 0; i < binds.size(); ++i) + return result; + } + catch (const std::bad_alloc& e) { - result[i].mod = static_cast(binds[i].mod); - std::strncpy(result[i].inter, binds[i].inter.c_str(), sizeof(result[i].inter) - 1); - result[i].inter[sizeof(result[i].inter) - 1] = '\0'; + f3d::log::error("Failed to allocate memory for f3d_interaction_bind_t[]: ", e.what()); } - return result; + return nullptr; } //---------------------------------------------------------------------------- @@ -611,13 +693,20 @@ void f3d_interactor_get_binding_documentation(f3d_interactor_t* interactor, cpp_bind.mod = static_cast(bind->mod); cpp_bind.inter = bind->inter; - auto [doc_str, value_str] = cpp_interactor->getBindingDocumentation(cpp_bind); + try + { + auto [doc_str, value_str] = cpp_interactor->getBindingDocumentation(cpp_bind); - std::strncpy(doc->doc, doc_str.c_str(), sizeof(doc->doc) - 1); - doc->doc[sizeof(doc->doc) - 1] = '\0'; + std::strncpy(doc->doc, doc_str.c_str(), sizeof(doc->doc) - 1); + doc->doc[sizeof(doc->doc) - 1] = '\0'; - std::strncpy(doc->value, value_str.c_str(), sizeof(doc->value) - 1); - doc->value[sizeof(doc->value) - 1] = '\0'; + std::strncpy(doc->value, value_str.c_str(), sizeof(doc->value) - 1); + doc->value[sizeof(doc->value) - 1] = '\0'; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -633,8 +722,18 @@ f3d_interactor_binding_type_t f3d_interactor_get_binding_type( f3d::interaction_bind_t cpp_bind; cpp_bind.mod = static_cast(bind->mod); cpp_bind.inter = bind->inter; - f3d::interactor::BindingType cpp_type = cpp_interactor->getBindingType(cpp_bind); - return static_cast(cpp_type); + + try + { + f3d::interactor::BindingType cpp_type = cpp_interactor->getBindingType(cpp_bind); + return static_cast(cpp_type); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return f3d_interactor_binding_type_t::F3D_INTERACTOR_BINDING_OTHER; } //---------------------------------------------------------------------------- diff --git a/c/interactor_c_api.h b/c/interactor_c_api.h index ba80b2a968..f463d0735c 100644 --- a/c/interactor_c_api.h +++ b/c/interactor_c_api.h @@ -181,7 +181,7 @@ extern "C" * * @param interactor Interactor handle. * @param count Output parameter for number of actions. - * @return Array of action strings. Caller must free the array with + * @return Array of action strings or NULL on failure. Caller must free the array with * f3d_interactor_free_string_array(). */ F3D_EXPORT char** f3d_interactor_get_command_actions(f3d_interactor_t* interactor, int* count); @@ -234,8 +234,8 @@ extern "C" * @brief Get all bind groups. * * @param interactor Interactor handle. - * @param count Output parameter for number of groups. - * @return Array of group strings. Caller must free the array with + * @param count Output parameter for number of groups. It will be set to 0 on failure. + * @return Array of group strings or NULL on failure. Caller must free the array with * f3d_interactor_free_string_array(). */ F3D_EXPORT char** f3d_interactor_get_bind_groups(f3d_interactor_t* interactor, int* count); @@ -245,8 +245,8 @@ extern "C" * * @param interactor Interactor handle. * @param group Group name. - * @param count Output parameter for number of binds. - * @return Array of binds. Caller must free the array with + * @param count Output parameter for number of binds. It will be set to 0 on failure. + * @return Array of binds or NULL on failure. Caller must free the array with * f3d_interactor_free_bind_array(). */ F3D_EXPORT f3d_interaction_bind_t* f3d_interactor_get_binds_for_group( @@ -257,7 +257,7 @@ extern "C" * * @param interactor Interactor handle. * @param count Output parameter for number of binds. - * @return Array of binds. Caller must free the array with + * @return Array of binds or NULL on failure. Caller must free the array with * f3d_interactor_free_bind_array(). */ F3D_EXPORT f3d_interaction_bind_t* f3d_interactor_get_binds( @@ -287,7 +287,8 @@ extern "C" * * @param interactor Interactor handle. * @param bind Interaction bind. - * @return Binding type. + * @return Binding type. Will return F3D_INTERACTOR_BINDING_OTHER on failure or when one of + * the parameters are NULL. */ F3D_EXPORT f3d_interactor_binding_type_t f3d_interactor_get_binding_type( f3d_interactor_t* interactor, const f3d_interaction_bind_t* bind); diff --git a/c/options_c_api.cxx b/c/options_c_api.cxx index 3642401d74..5c14695dc1 100644 --- a/c/options_c_api.cxx +++ b/c/options_c_api.cxx @@ -1,6 +1,6 @@ #include "options_c_api.h" #include "options.h" - +#include "log.h" #include #include #include @@ -8,8 +8,17 @@ //---------------------------------------------------------------------------- f3d_options_t* f3d_options_create() { - f3d::options* cpp_options = new f3d::options(); - return reinterpret_cast(cpp_options); + try + { + f3d::options* cpp_options = new f3d::options(); + return reinterpret_cast(cpp_options); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for f3d_options_t: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -33,7 +42,15 @@ void f3d_options_set_as_bool(f3d_options_t* options, const char* name, int value } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->set(name, static_cast(value)); + + try + { + cpp_options->set(name, static_cast(value)); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -45,7 +62,15 @@ void f3d_options_set_as_int(f3d_options_t* options, const char* name, int value) } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->set(name, value); + + try + { + cpp_options->set(name, value); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -57,7 +82,15 @@ void f3d_options_set_as_double(f3d_options_t* options, const char* name, double } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->set(name, value); + + try + { + cpp_options->set(name, value); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -69,7 +102,15 @@ void f3d_options_set_as_string(f3d_options_t* options, const char* name, const c } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->set(name, std::string(value)); + + try + { + cpp_options->set(name, std::string(value)); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -83,7 +124,15 @@ void f3d_options_set_as_double_vector( f3d::options* cpp_options = reinterpret_cast(options); std::vector vec(values, values + count); - cpp_options->set(name, vec); + + try + { + cpp_options->set(name, vec); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -97,7 +146,15 @@ void f3d_options_set_as_int_vector( f3d::options* cpp_options = reinterpret_cast(options); std::vector vec(values, values + count); - cpp_options->set(name, vec); + + try + { + cpp_options->set(name, vec); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -109,7 +166,17 @@ int f3d_options_get_as_bool(const f3d_options_t* options, const char* name) } const f3d::options* cpp_options = reinterpret_cast(options); - return std::get(cpp_options->get(name)) ? 1 : 0; + + try + { + return std::get(cpp_options->get(name)) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -121,7 +188,17 @@ int f3d_options_get_as_int(const f3d_options_t* options, const char* name) } const f3d::options* cpp_options = reinterpret_cast(options); - return std::get(cpp_options->get(name)); + + try + { + return std::get(cpp_options->get(name)); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -133,7 +210,17 @@ double f3d_options_get_as_double(const f3d_options_t* options, const char* name) } const f3d::options* cpp_options = reinterpret_cast(options); - return std::get(cpp_options->get(name)); + + try + { + return std::get(cpp_options->get(name)); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0.0; } //---------------------------------------------------------------------------- @@ -145,10 +232,28 @@ const char* f3d_options_get_as_string(const f3d_options_t* options, const char* } const f3d::options* cpp_options = reinterpret_cast(options); - const std::string str = std::get(cpp_options->get(name)); - char* result = new char[str.length() + 1]; - std::strcpy(result, str.c_str()); - return result; + + try + { + const std::string str = std::get(cpp_options->get(name)); + char* result = new char[str.length() + 1]; + std::strcpy(result, str.c_str()); + return result; + } + catch (const f3d::options::no_value_exception& e) + { + f3d::log::warn(e.what()); + } + catch (const f3d::options::inexistent_exception& e) + { + f3d::log::warn(e.what()); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char[]: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -161,9 +266,18 @@ void f3d_options_get_as_double_vector( } const f3d::options* cpp_options = reinterpret_cast(options); - const std::vector vec = std::get>(cpp_options->get(name)); - *count = vec.size(); - std::copy(vec.begin(), vec.end(), values); + + try + { + const std::vector vec = std::get>(cpp_options->get(name)); + *count = vec.size(); + std::copy(vec.begin(), vec.end(), values); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + *count = 0; + } } //---------------------------------------------------------------------------- @@ -176,9 +290,18 @@ void f3d_options_get_as_int_vector( } const f3d::options* cpp_options = reinterpret_cast(options); - const std::vector vec = std::get>(cpp_options->get(name)); - *count = vec.size(); - std::copy(vec.begin(), vec.end(), values); + + try + { + const std::vector vec = std::get>(cpp_options->get(name)); + *count = vec.size(); + std::copy(vec.begin(), vec.end(), values); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + *count = 0; + } } //---------------------------------------------------------------------------- @@ -190,7 +313,15 @@ void f3d_options_toggle(f3d_options_t* options, const char* name) } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->toggle(name); + + try + { + cpp_options->toggle(name); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -203,7 +334,17 @@ int f3d_options_is_same(const f3d_options_t* options, const f3d_options_t* other const f3d::options* cpp_options = reinterpret_cast(options); const f3d::options* cpp_other = reinterpret_cast(other); - return cpp_options->isSame(*cpp_other, name) ? 1 : 0; + + try + { + return cpp_options->isSame(*cpp_other, name) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -215,7 +356,17 @@ int f3d_options_has_value(const f3d_options_t* options, const char* name) } const f3d::options* cpp_options = reinterpret_cast(options); - return cpp_options->hasValue(name) ? 1 : 0; + + try + { + return cpp_options->hasValue(name) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -228,7 +379,15 @@ void f3d_options_copy(f3d_options_t* options, const f3d_options_t* other, const f3d::options* cpp_options = reinterpret_cast(options); const f3d::options* cpp_other = reinterpret_cast(other); - cpp_options->copy(*cpp_other, name); + + try + { + cpp_options->copy(*cpp_other, name); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -242,14 +401,23 @@ char** f3d_options_get_all_names(size_t* count) std::vector names = f3d::options::getAllNames(); *count = names.size(); - char** result = new char*[names.size()]; - for (size_t i = 0; i < names.size(); i++) + try + { + char** result = new char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + result[i] = new char[names[i].length() + 1]; + std::strcpy(result[i], names[i].c_str()); + } + + return result; + } + catch (const std::bad_alloc& e) { - result[i] = new char[names[i].length() + 1]; - std::strcpy(result[i], names[i].c_str()); + f3d::log::error("Failed to allocate memory for char**: ", e.what()); } - return result; + return nullptr; } //---------------------------------------------------------------------------- @@ -264,14 +432,23 @@ char** f3d_options_get_names(const f3d_options_t* options, size_t* count) std::vector names = cpp_options->getNames(); *count = names.size(); - char** result = new char*[names.size()]; - for (size_t i = 0; i < names.size(); i++) + try { - result[i] = new char[names[i].length() + 1]; - std::strcpy(result[i], names[i].c_str()); - } + char** result = new char*[names.size()]; + for (size_t i = 0; i < names.size(); i++) + { + result[i] = new char[names[i].length() + 1]; + std::strcpy(result[i], names[i].c_str()); + } - return result; + return result; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char**: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -298,7 +475,17 @@ int f3d_options_is_optional(const f3d_options_t* options, const char* name) } const f3d::options* cpp_options = reinterpret_cast(options); - return cpp_options->isOptional(name) ? 1 : 0; + + try + { + return cpp_options->isOptional(name) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -310,7 +497,15 @@ void f3d_options_reset(f3d_options_t* options, const char* name) } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->reset(name); + + try + { + cpp_options->reset(name); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -322,7 +517,15 @@ void f3d_options_remove_value(f3d_options_t* options, const char* name) } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->removeValue(name); + + try + { + cpp_options->removeValue(name); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -334,10 +537,28 @@ const char* f3d_options_get_as_string_representation(const f3d_options_t* option } const f3d::options* cpp_options = reinterpret_cast(options); - std::string str = cpp_options->getAsString(name); - char* result = new char[str.length() + 1]; - std::strcpy(result, str.c_str()); - return result; + + try + { + std::string str = cpp_options->getAsString(name); + char* result = new char[str.length() + 1]; + std::strcpy(result, str.c_str()); + return result; + } + catch (const f3d::options::no_value_exception& e) + { + f3d::log::warn(e.what()); + } + catch (const f3d::options::inexistent_exception& e) + { + f3d::log::warn(e.what()); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -350,7 +571,15 @@ void f3d_options_set_as_string_representation( } f3d::options* cpp_options = reinterpret_cast(options); - cpp_options->setAsString(name, str); + + try + { + cpp_options->setAsString(name, str); + } + catch (const std::exception& e) + { + f3d::log::warn(e.what()); + } } //---------------------------------------------------------------------------- @@ -376,10 +605,19 @@ void f3d_options_get_closest_option( const f3d::options* cpp_options = reinterpret_cast(options); auto result = cpp_options->getClosestOption(option); - char* result_str = new char[result.first.length() + 1]; - std::strcpy(result_str, result.first.c_str()); - *closest = result_str; - *distance = result.second; + try + { + char* result_str = new char[result.first.length() + 1]; + std::strcpy(result_str, result.first.c_str()); + *closest = result_str; + *distance = result.second; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + *closest = nullptr; + *distance = 0U; + } } //---------------------------------------------------------------------------- @@ -390,7 +628,16 @@ int f3d_options_parse_bool(const char* str) return 0; } - return f3d::options::parse(str) ? 1 : 0; + try + { + return f3d::options::parse(str) ? 1 : 0; + } + catch (const std::exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -401,7 +648,16 @@ int f3d_options_parse_int(const char* str) return 0; } - return f3d::options::parse(str); + try + { + return f3d::options::parse(str); + } + catch (const std::exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + } + + return 0; } //---------------------------------------------------------------------------- @@ -412,7 +668,16 @@ double f3d_options_parse_double(const char* str) return 0.0; } - return f3d::options::parse(str); + try + { + return f3d::options::parse(str); + } + catch (const std::exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + } + + return 0.0; } //---------------------------------------------------------------------------- @@ -423,10 +688,24 @@ const char* f3d_options_parse_string(const char* str) return nullptr; } - std::string result = f3d::options::parse(str); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + try + { + + std::string result = f3d::options::parse(str); + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const f3d::options::parsing_exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -437,9 +716,17 @@ void f3d_options_parse_double_vector(const char* str, double* values, size_t* co return; } - std::vector vec = f3d::options::parse>(str); - *count = vec.size(); - std::copy(vec.begin(), vec.end(), values); + try + { + std::vector vec = f3d::options::parse>(str); + *count = vec.size(); + std::copy(vec.begin(), vec.end(), values); + } + catch (const std::exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + *count = 0U; + } } //---------------------------------------------------------------------------- @@ -450,36 +737,73 @@ void f3d_options_parse_int_vector(const char* str, int* values, size_t* count) return; } - std::vector vec = f3d::options::parse>(str); - *count = vec.size(); - std::copy(vec.begin(), vec.end(), values); + try + { + std::vector vec = f3d::options::parse>(str); + *count = vec.size(); + std::copy(vec.begin(), vec.end(), values); + } + catch (const std::exception& e) + { + f3d::log::error("Parsing failed: ", e.what()); + } } //---------------------------------------------------------------------------- const char* f3d_options_format_bool(int value) { std::string result = f3d::options::format(static_cast(value)); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- const char* f3d_options_format_int(int value) { std::string result = f3d::options::format(value); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- const char* f3d_options_format_double(double value) { std::string result = f3d::options::format(value); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -491,9 +815,19 @@ const char* f3d_options_format_string(const char* value) } std::string result = f3d::options::format(std::string(value)); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -506,9 +840,19 @@ const char* f3d_options_format_double_vector(const double* values, size_t count) std::vector vec(values, values + count); std::string result = f3d::options::format(vec); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -521,7 +865,17 @@ const char* f3d_options_format_int_vector(const int* values, size_t count) std::vector vec(values, values + count); std::string result = f3d::options::format(vec); - char* result_str = new char[result.length() + 1]; - std::strcpy(result_str, result.c_str()); - return result_str; + + try + { + char* result_str = new char[result.length() + 1]; + std::strcpy(result_str, result.c_str()); + return result_str; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } diff --git a/c/options_c_api.h b/c/options_c_api.h index 23c244eb70..8c52f54521 100644 --- a/c/options_c_api.h +++ b/c/options_c_api.h @@ -17,7 +17,7 @@ extern "C" * * The returned options object must be freed with f3d_options_delete(). * - * @return Options handle. + * @return Options handle or NULL on failure. */ F3D_EXPORT f3d_options_t* f3d_options_create(); @@ -96,7 +96,7 @@ extern "C" * * @param options Options handle. * @param name Option name. - * @return Boolean value (0 for false, non-zero for true). + * @return Boolean value (0 for false/failure, non-zero for true). */ F3D_EXPORT int f3d_options_get_as_bool(const f3d_options_t* options, const char* name); @@ -105,7 +105,7 @@ extern "C" * * @param options Options handle. * @param name Option name. - * @return Integer value. + * @return Integer value. Returns 0 in case of failure. */ F3D_EXPORT int f3d_options_get_as_int(const f3d_options_t* options, const char* name); @@ -114,7 +114,7 @@ extern "C" * * @param options Options handle. * @param name Option name. - * @return Double value. + * @return Double value. Returns 0 in case of failure. */ F3D_EXPORT double f3d_options_get_as_double(const f3d_options_t* options, const char* name); @@ -125,7 +125,7 @@ extern "C" * * @param options Options handle. * @param name Option name. - * @return String value. + * @return String value. Returns NULL in case of failure. */ F3D_EXPORT const char* f3d_options_get_as_string(const f3d_options_t* options, const char* name); @@ -136,7 +136,7 @@ extern "C" * * @param options Options handle. * @param name Option name. - * @return String representation of the option value. + * @return String representation of the option value or NULL on failure. */ F3D_EXPORT const char* f3d_options_get_as_string_representation( const f3d_options_t* options, const char* name); @@ -301,7 +301,7 @@ extern "C" * @brief Parse a string as a boolean. * * @param str String to parse. - * @return 1 for true, 0 for false. + * @return 1 for true, 0 for false/failure. */ F3D_EXPORT int f3d_options_parse_bool(const char* str); @@ -309,7 +309,7 @@ extern "C" * @brief Parse a string as an integer. * * @param str String to parse. - * @return Parsed integer value. + * @return Parsed integer value. It will return 0 if it fails. */ F3D_EXPORT int f3d_options_parse_int(const char* str); @@ -317,7 +317,7 @@ extern "C" * @brief Parse a string as a double. * * @param str String to parse. - * @return Parsed double value. + * @return Parsed double value. It will return 0 if it fails. */ F3D_EXPORT double f3d_options_parse_double(const char* str); @@ -325,7 +325,7 @@ extern "C" * @brief Parse a string as a string (returns a copy). * * @param str String to parse. - * @return Parsed string. Caller must free with f3d_options_free_string(). + * @return Parsed string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_parse_string(const char* str); @@ -351,7 +351,7 @@ extern "C" * @brief Format a boolean as a string. * * @param value Boolean value. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_bool(int value); @@ -359,7 +359,7 @@ extern "C" * @brief Format an integer as a string. * * @param value Integer value. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_int(int value); @@ -367,7 +367,7 @@ extern "C" * @brief Format a double as a string. * * @param value Double value. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_double(double value); @@ -375,7 +375,7 @@ extern "C" * @brief Format a string (returns a copy). * * @param value String value. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_string(const char* value); @@ -384,7 +384,7 @@ extern "C" * * @param values Array of double values. * @param count Number of values in the array. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_double_vector(const double* values, size_t count); @@ -393,7 +393,7 @@ extern "C" * * @param values Array of integer values. * @param count Number of values in the array. - * @return Formatted string. Caller must free with f3d_options_free_string(). + * @return Formatted string or NULL on failure. Caller must free with f3d_options_free_string(). */ F3D_EXPORT const char* f3d_options_format_int_vector(const int* values, size_t count); ///@} diff --git a/c/types_c_api.cxx b/c/types_c_api.cxx index 659c60f199..69cf7e42c1 100644 --- a/c/types_c_api.cxx +++ b/c/types_c_api.cxx @@ -1,5 +1,6 @@ #include "types_c_api.h" #include "types.h" +#include "log.h" #include #include @@ -110,8 +111,10 @@ void f3d_transform2d_create(f3d_transform2d_t* transform, double scale_x, double transform->data[i] = cpp_transform[i]; } } - catch (...) + catch (const std::exception& e) { + f3d::log::warn(e.what()); + // Initialize to identity on error for (int i = 0; i < 9; ++i) { @@ -142,8 +145,15 @@ int f3d_mesh_is_valid(const f3d_mesh_t* mesh, char** error_message) if (error_message) { const char* msg = "Mesh pointer is NULL"; - *error_message = new char[std::strlen(msg) + 1]; - std::strcpy(*error_message, msg); + try + { + *error_message = new char[std::strlen(msg) + 1]; + std::strcpy(*error_message, msg); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for error message: ", e.what()); + } } return 0; } @@ -184,8 +194,15 @@ int f3d_mesh_is_valid(const f3d_mesh_t* mesh, char** error_message) if (!valid && error_message) { - *error_message = new char[msg.size() + 1]; - std::strcpy(*error_message, msg.c_str()); + try + { + *error_message = new char[msg.size() + 1]; + std::strcpy(*error_message, msg.c_str()); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for error message: ", e.what()); + } } return valid ? 1 : 0; @@ -195,8 +212,15 @@ int f3d_mesh_is_valid(const f3d_mesh_t* mesh, char** error_message) if (error_message) { const char* msg = "Exception during mesh validation"; - *error_message = new char[std::strlen(msg) + 1]; - std::strcpy(*error_message, msg); + try + { + *error_message = new char[std::strlen(msg) + 1]; + std::strcpy(*error_message, msg); + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for error message: ", e.what()); + } } return 0; } diff --git a/c/utils_c_api.cxx b/c/utils_c_api.cxx index fb15e33e83..7d40fa495f 100644 --- a/c/utils_c_api.cxx +++ b/c/utils_c_api.cxx @@ -1,5 +1,6 @@ #include "utils_c_api.h" #include "utils.h" +#include "log.h" #include #include #include @@ -10,9 +11,18 @@ namespace //---------------------------------------------------------------------------- char* f3d_utils_strdup(const std::string& s) { - char* r = new char[s.size() + 1]; - std::memcpy(r, s.c_str(), s.size() + 1); - return r; + try + { + char* r = new char[s.size() + 1]; + std::memcpy(r, s.c_str(), s.size() + 1); + return r; + } + catch (const std::bad_alloc& e) + { + f3d::log::error("Failed to allocate memory for char*: ", e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- @@ -50,21 +60,35 @@ char** f3d_utils_tokenize(const char* str, int keep_comments, size_t* out_count) return nullptr; } - std::vector vec = f3d::utils::tokenize(str, keep_comments != 0); + try + { + std::vector vec = f3d::utils::tokenize(str, keep_comments != 0); + + size_t n = vec.size(); + char** out = new char*[n]; + for (size_t i = 0; i < n; ++i) + { + out[i] = f3d_utils_strdup(vec[i]); + } + + if (out_count) + { + *out_count = n; + } - size_t n = vec.size(); - char** out = new char*[n]; - for (size_t i = 0; i < n; ++i) + return out; + } + catch (const f3d::utils::tokenize_exception& e) { - out[i] = f3d_utils_strdup(vec[i]); + f3d::log::error("Failed to tokenize: ", e.what()); } - - if (out_count) + catch (const std::bad_alloc& e) { - *out_count = n; + f3d::log::error("Failed to allocate memory for char**: ", e.what()); } - return out; + *out_count = 0; + return nullptr; } //---------------------------------------------------------------------------- @@ -76,17 +100,35 @@ void f3d_utils_tokens_free(char** tokens, size_t count) //---------------------------------------------------------------------------- char* f3d_utils_collapse_path(const char* path, const char* base_directory) { - std::filesystem::path p(path); - std::filesystem::path base = base_directory ? base_directory : ""; - auto collapsed = f3d::utils::collapsePath(p, base); - return f3d_utils_strdup(collapsed.string()); + try + { + std::filesystem::path p(path); + std::filesystem::path base = base_directory ? base_directory : ""; + auto collapsed = f3d::utils::collapsePath(p, base); + return f3d_utils_strdup(collapsed.string()); + } + catch (...) + { + f3d::log::error("Failed to initialize file path."); + } + + return nullptr; } //---------------------------------------------------------------------------- char* f3d_utils_glob_to_regex(const char* glob, char path_separator) { - std::string regex = f3d::utils::globToRegex(glob, path_separator); - return f3d_utils_strdup(regex); + try + { + std::string regex = f3d::utils::globToRegex(glob, path_separator); + return f3d_utils_strdup(regex); + } + catch (const std::exception& e) + { + f3d::log::error(e.what()); + } + + return nullptr; } //---------------------------------------------------------------------------- diff --git a/c/utils_c_api.h b/c/utils_c_api.h index 2c2933a4f0..55b1d113cf 100644 --- a/c/utils_c_api.h +++ b/c/utils_c_api.h @@ -38,7 +38,7 @@ extern "C" * @param str Input string to tokenize. * @param keep_comments Non-zero to keep comments, zero to treat '#' as a normal character. * @param out_count Pointer to receive the number of tokens. - * @return Array of C strings. + * @return Array of C strings or NULL on failure. */ F3D_EXPORT char** f3d_utils_tokenize(const char* str, int keep_comments, size_t* out_count); @@ -60,7 +60,7 @@ extern "C" * * @param path Input path. * @param base_directory Base directory for relative paths. - * @return Collapsed absolute path string. + * @return Collapsed absolute path string or NULL on failure. */ F3D_EXPORT char* f3d_utils_collapse_path(const char* path, const char* base_directory);