-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_to_string.gml
64 lines (55 loc) · 1.63 KB
/
file_to_string.gml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @function file_to_string()
/// @argument path {string:path}
/// @returns {string}
/// @description Load the specified text file and return it as a string.
// @author Mtax (github.com/Mtax-Development/GML-Development-Toolbox)
function file_to_string(_path)
{
var _result = "";
if ((is_string(_path)) and (file_exists(_path)))
{
try
{
var _stream = file_text_open_read(_path);
if (_stream >= 0)
{
while (!file_text_eof(_stream))
{
_result += file_text_readln(_stream);
}
file_text_close(_stream);
}
_result = string_replace_all(_result, "\u000d", "");
return _result;
}
catch (_exception)
{
var _path_string = string(_path);
var _error_reason = (_exception.message + " " +
((string_count(_path_string, _exception.message) > 0)
? "" :"File path: " + "\"" + _path_string + "\"."));
}
}
else
{
var _error_reason = ("Failed opening file at path: " + "\"" + string(_path) + "\".");
}
var _callstack_raw = debug_get_callstack();
var _callstack = [];
array_copy(_callstack, 0, _callstack_raw, 1, (array_length(_callstack_raw) - 2));
var _tabulation = string_repeat(" ", 4);
var _callstack_formatted = "";
var _i = 0;
repeat (array_length(_callstack))
{
_callstack_formatted += ((((_i != 0) ? "\n" : "") + _tabulation + string(_callstack[_i])));
++_i;
}
var _separator = string_repeat("#", 92);
var _error_message = (_separator + "\n" +
"file_to_string(): " + _error_reason + "\n\n" +
"Callstack: " + "\n" + _callstack_formatted + "\n" +
_separator);
show_debug_message(_error_message);
return _result;
}