Browse Source

Added alloc_c_string

Bastiaan Olij 7 years ago
parent
commit
d8faa4ec76
3 changed files with 37 additions and 38 deletions
  1. 1 0
      include/core/String.hpp
  2. 20 38
      src/core/GodotGlobal.cpp
  3. 16 0
      src/core/String.cpp

+ 1 - 0
include/core/String.hpp

@@ -41,6 +41,7 @@ public:
 
 
 	int length() const;
 	int length() const;
 	const wchar_t *unicode_str() const;
 	const wchar_t *unicode_str() const;
+	char *alloc_c_string() const;
 	void get_c_string(char *p_dest, int *p_size) const;
 	void get_c_string(char *p_dest, int *p_size) const;
 
 
 	int64_t find(String p_what) const;
 	int64_t find(String p_what) const;

+ 20 - 38
src/core/GodotGlobal.cpp

@@ -17,52 +17,34 @@ void Godot::print_warning(const String& description, const String& function, con
 {
 {
 	int len;
 	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)
 void Godot::print_error(const String& description, const String& function, const String& file, int line)
 {
 {
 	int len;
 	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);
 }
 }
 
 
 };
 };

+ 16 - 0
src/core/String.cpp

@@ -107,6 +107,22 @@ const wchar_t *String::unicode_str() const {
 	return godot::api->godot_string_unicode_str(&_godot_string);
 	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 {
 void String::get_c_string(char *p_dest, int *p_size) const {
 	godot::api->godot_string_get_data(&_godot_string, p_dest, p_size);
 	godot::api->godot_string_get_data(&_godot_string, p_dest, p_size);
 }
 }