Added alloc_c_string

pull/52/head
Bastiaan Olij 2017-11-25 09:54:35 +11:00
parent 0f773c2d72
commit d8faa4ec76
3 changed files with 35 additions and 36 deletions

View File

@ -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;

View File

@ -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 = description.alloc_c_string();
char * c_desc = (char *)godot::api->godot_alloc(len + 1); char * c_func = function.alloc_c_string();
description.get_c_string(c_desc, &len); char * c_file = file.alloc_c_string();
c_desc[len] = '\0';
function.get_c_string(NULL, &len); if (c_desc != NULL && c_func !=NULL && c_file != NULL) {
char * c_func = (char *)godot::api->godot_alloc(len + 1); godot::api->godot_print_warning(c_desc, c_func, c_file, line);
function.get_c_string(c_func, &len); };
c_func[len] = '\0';
file.get_c_string(NULL, &len); if (c_desc != NULL) godot::api->godot_free(c_desc);
char * c_file = (char *)godot::api->godot_alloc(len + 1); if (c_func != NULL) godot::api->godot_free(c_func);
file.get_c_string(c_file, &len); if (c_file != NULL) godot::api->godot_free(c_file);
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);
} }
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 = description.alloc_c_string();
char * c_desc = (char *)godot::api->godot_alloc(len + 1); char * c_func = function.alloc_c_string();
description.get_c_string(c_desc, &len); char * c_file = file.alloc_c_string();
c_desc[len] = '\0';
function.get_c_string(NULL, &len); if (c_desc != NULL && c_func !=NULL && c_file != NULL) {
char * c_func = (char *)godot::api->godot_alloc(len + 1); godot::api->godot_print_error(c_desc, c_func, c_file, line);
function.get_c_string(c_func, &len); };
c_func[len] = '\0';
file.get_c_string(NULL, &len); if (c_desc != NULL) godot::api->godot_free(c_desc);
char * c_file = (char *)godot::api->godot_alloc(len + 1); if (c_func != NULL) godot::api->godot_free(c_func);
file.get_c_string(c_file, &len); if (c_file != NULL) godot::api->godot_free(c_file);
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);
} }
}; };

View File

@ -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);
} }