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
|
#define XERR
#include "builtin.ih"
// The list has 4 values (output):
// [0] - the read line
// [1] - a '\n' if a newline was read
// [2] - input: "OK": proceed; output: "OK" or "FAIL"
// [3] - input: starting offset, output: next offset
//
// or empty: start reading from the file's beginning
void Builtin::fgets()
{
enum
{
LINE,
NL,
OK,
OFFSET
};
string const &fileName = d_stack[2].str();
vector<string>list = d_stack[3].list();
if (list.empty())
list = { "", "\n", "OK", "0" };
if (list[OK] == "OK")
{
ifstream in{ fileName };
in.seekg(stoull(list[OFFSET])); // go to the stream's offset
if (not getline(in, list[LINE])) // no more lines
list.clear();
else
{
list[OFFSET] = to_string(in.tellg());
if (in.eof())
list[OK] = "FAIL";
}
}
d_reg = move(list);
}
|