From 9625ec1ecf1d572035c4499f35b150bc54e08bf0 Mon Sep 17 00:00:00 2001 From: neverbot Date: Thu, 2 May 2024 00:02:23 +0200 Subject: [PATCH] fix: with read_file_lines and errors --- mudlib/lib/cmds/coder/more.c | 11 ++++++--- mudlib/lib/core/efuns/files/read_file.c | 33 +++++++++++-------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/mudlib/lib/cmds/coder/more.c b/mudlib/lib/cmds/coder/more.c index e61b2492..f810c745 100644 --- a/mudlib/lib/cmds/coder/more.c +++ b/mudlib/lib/cmds/coder/more.c @@ -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; @@ -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)); } diff --git a/mudlib/lib/core/efuns/files/read_file.c b/mudlib/lib/core/efuns/files/read_file.c index a256faa4..c0304afb 100644 --- a/mudlib/lib/core/efuns/files/read_file.c +++ b/mudlib/lib/core/efuns/files/read_file.c @@ -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; }