Skip to content

Commit

Permalink
fix: with read_file_lines and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
neverbot committed May 1, 2024
1 parent 6a147f4 commit 9625ec1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
11 changes: 7 additions & 4 deletions mudlib/lib/cmds/coder/more.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static int cmd(string str, object me, string verb)

int ex_spool(string yn, string fil, int linum)
{
string s1;
string s1, err;
string * lines;
int i;
mixed tnum;
Expand Down Expand Up @@ -123,10 +123,13 @@ int ex_spool(string yn, string fil, int linum)
{
i++;

catch (s1 = read_file_line(fil, linum, 1));

if (!strlen(s1))
err = catch(s1 = read_file_line(fil, linum, 1));

if (err)
{
write("\n");
break;
}

write(sprintf("%4d: %s", linum, s1));
}
Expand Down
33 changes: 14 additions & 19 deletions mudlib/lib/core/efuns/files/read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,25 @@ static string read_file_line(string file, varargs int start_line, int number_of_
if (!number_of_lines)
number_of_lines = 1;

// catch
// {
if (start_line)
while ((read = ::read_file(file, where, 1)) && (current_line < start_line) && (where < size))
{
where++;
if (read == "\n")
current_line++;
}

while ((read = ::read_file(file, where, 1)) && (line_count < number_of_lines) && (where < size))
if (start_line)
while ((read = ::read_file(file, where, 1)) && (current_line < start_line))
{
ret += read;
where++;

if (read == "\n")
line_count++;
current_line++;
}
// }

// haven't reach the amount of lines desired, the file is smaller
// +1 because lines 0 and 1 are the first line
if (start_line + number_of_lines > current_line + line_count + 1)
error("read_file_line: Attempt to read past end of file.\n");
while ((read = ::read_file(file, where, 1)) && (line_count < number_of_lines))
{
ret += read;
where++;

// if we read a new line, increment counter
// or, if we are reading the last needed line and we reach the end of the file
if (read == "\n" ||
((where == size) && (line_count + 1 == number_of_lines)))
line_count++;
}

return ret;
}
Expand Down

0 comments on commit 9625ec1

Please sign in to comment.