From d8faa4ec76bc73ca17b8e5dd72220d16996423c3 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Sat, 25 Nov 2017 09:54:35 +1100 Subject: [PATCH] Added alloc_c_string --- include/core/String.hpp | 1 + src/core/GodotGlobal.cpp | 54 ++++++++++++++-------------------------- src/core/String.cpp | 16 ++++++++++++ 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index 63e2119e..77881956 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -41,6 +41,7 @@ public: int length() const; const wchar_t *unicode_str() const; + char *alloc_c_string() const; void get_c_string(char *p_dest, int *p_size) const; int64_t find(String p_what) const; diff --git a/src/core/GodotGlobal.cpp b/src/core/GodotGlobal.cpp index 598fdc8f..85664814 100644 --- a/src/core/GodotGlobal.cpp +++ b/src/core/GodotGlobal.cpp @@ -17,52 +17,34 @@ void Godot::print_warning(const String& description, const String& function, con { int len; - description.get_c_string(NULL, &len); - char * c_desc = (char *)godot::api->godot_alloc(len + 1); - description.get_c_string(c_desc, &len); - c_desc[len] = '\0'; + char * c_desc = description.alloc_c_string(); + char * c_func = function.alloc_c_string(); + char * c_file = file.alloc_c_string(); - function.get_c_string(NULL, &len); - char * c_func = (char *)godot::api->godot_alloc(len + 1); - function.get_c_string(c_func, &len); - c_func[len] = '\0'; + if (c_desc != NULL && c_func !=NULL && c_file != NULL) { + godot::api->godot_print_warning(c_desc, c_func, c_file, line); + }; - file.get_c_string(NULL, &len); - char * c_file = (char *)godot::api->godot_alloc(len + 1); - file.get_c_string(c_file, &len); - c_file[len] = '\0'; - - godot::api->godot_print_warning(c_desc, c_func, c_file, line); - - godot::api->godot_free(c_desc); - godot::api->godot_free(c_func); - godot::api->godot_free(c_file); + if (c_desc != NULL) godot::api->godot_free(c_desc); + if (c_func != NULL) godot::api->godot_free(c_func); + if (c_file != NULL) godot::api->godot_free(c_file); } void Godot::print_error(const String& description, const String& function, const String& file, int line) { int len; - description.get_c_string(NULL, &len); - char * c_desc = (char *)godot::api->godot_alloc(len + 1); - description.get_c_string(c_desc, &len); - c_desc[len] = '\0'; + char * c_desc = description.alloc_c_string(); + char * c_func = function.alloc_c_string(); + char * c_file = file.alloc_c_string(); - function.get_c_string(NULL, &len); - char * c_func = (char *)godot::api->godot_alloc(len + 1); - function.get_c_string(c_func, &len); - c_func[len] = '\0'; + if (c_desc != NULL && c_func !=NULL && c_file != NULL) { + godot::api->godot_print_error(c_desc, c_func, c_file, line); + }; - file.get_c_string(NULL, &len); - char * c_file = (char *)godot::api->godot_alloc(len + 1); - file.get_c_string(c_file, &len); - c_file[len] = '\0'; - - godot::api->godot_print_error(c_desc, c_func, c_file, line); - - godot::api->godot_free(c_desc); - godot::api->godot_free(c_func); - godot::api->godot_free(c_file); + if (c_desc != NULL) godot::api->godot_free(c_desc); + if (c_func != NULL) godot::api->godot_free(c_func); + if (c_file != NULL) godot::api->godot_free(c_file); } }; diff --git a/src/core/String.cpp b/src/core/String.cpp index 9a7b96e4..494d9761 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -107,6 +107,22 @@ const wchar_t *String::unicode_str() const { return godot::api->godot_string_unicode_str(&_godot_string); } +char *String::alloc_c_string() const { + int len; + + // figure out the lenght of our string + get_c_string(NULL, &len); + + // allocate our buffer + char * result = (char *)godot::api->godot_alloc(len + 1); + if (result != NULL) { + get_c_string(result, &len); + result[len] = '\0'; + } + + return result; +} + void String::get_c_string(char *p_dest, int *p_size) const { godot::api->godot_string_get_data(&_godot_string, p_dest, p_size); }