Reads a whole file into an array of lines. Returns whether the file was read and the lines.
file_to_array :: (
filename: string,
do_trim := true,
skip_blank_lines := true,
do_version_number := false,
use_data_from_memory := false,
data_from_memory := "",
loc := #caller_location
) -> (success: bool, array: [..] string)
Comments are removed, and with do_trim each line is trimmed. With skip_blank_lines empty lines
are left out. A version header is expected only if do_version_number is set, and then it is not
among the lines. With use_data_from_memory the text in data_from_memory is read instead of the file
(filename is then only a label).
When the file cannot be read, or the header is bad, the result is false and an empty array, and an
error is logged. Otherwise the array and every line in it are allocated with the context allocator and
the caller frees them.
Parameters
- filename: string
- do_trim := true
- skip_blank_lines := true
- do_version_number := false
- use_data_from_memory := false
- data_from_memory := ""
- loc := #caller_location
Returns
- success: bool
- array: [..] string
ok, lines := file_to_array("list.txt", use_data_from_memory = true, data_from_memory = "a # one\n\nb\n");
assert(ok && lines.count == 2 && lines[0] == "a" && lines[1] == "b");
for line: lines free(line);
array_free(lines);