summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/GodotGlobal.cpp58
-rw-r--r--src/core/String.cpp16
2 files changed, 36 insertions, 38 deletions
diff --git a/src/core/GodotGlobal.cpp b/src/core/GodotGlobal.cpp
index 598fdc8..8566481 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';
-
- 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';
-
- 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);
+ char * c_desc = description.alloc_c_string();
+ char * c_func = function.alloc_c_string();
+ char * c_file = file.alloc_c_string();
+
+ if (c_desc != NULL && c_func !=NULL && c_file != NULL) {
+ godot::api->godot_print_warning(c_desc, c_func, c_file, line);
+ };
+
+ 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';
-
- 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);
+ if (c_desc != NULL && c_func !=NULL && c_file != NULL) {
+ 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 9a7b96e..494d976 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);
}