If you want the get the *entire* file in one shot, you could use a function like this:
_str get_file_contents(_str filename)
{
status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
if (status) return null;
result := get_text(p_buf_size,0);
_delete_temp_view(temp_wid);
p_window_id=orig_wid;
return(result);
}
If you want to get all the lines in a file, and stuff them into an array, you could do something like this:
STRARRAY get_file_lines(_str filename)
{
status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
if (status) return null;
_str lineContents;
_str lineArray[];
top();
loop {
get_line(lineContents);
lineArray :+= lineContents;
if (down()) break;
}
_delete_temp_view(temp_wid);
p_window_id=orig_wid;
return lineArray;
}
If you want a function that behaves more incrementally like fgets(), you could use a function like this to get each line, until it hits the end of the file and returns null.
_str get_one_line(bool startAtFirstLine=false)
{
if (startAtFirstLine) {
top();
} else if (down()) {
return null;
}
get_line(auto lineContents);
return lineContents;
}
void print_file_contents(_str filename)
{
status := _open_temp_view(filename,auto temp_wid,auto orig_wid);
if (status) return null;
lineContents := get_one_line(startAtFirstLine:true);
for (i := 1; lineContents != null; i++) {
say("line["i": "lineContents);
lineContents = get_one_line();
}
_delete_temp_view(temp_wid);
p_window_id=orig_wid;
}