From a952eaabb7f33072d2b4e474903156266d8a3a29 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 5 Oct 2021 22:39:45 +0200 Subject: Refactoring --- src/field.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/field.nim b/src/field.nim index 8d609f7..e15d7b4 100644 --- a/src/field.nim +++ b/src/field.nim @@ -69,11 +69,13 @@ func Get*(f: Field, x, y: int): int = func IsIn*(f: Field, x, y: int): bool = return x >= f.x and y >= f.y and x < f.x+f.lx and y < f.y+f.ly -proc Load*(f: var Field, filename: string): bool = +proc Load*(filename: string): ref Field = var file: File if not open(file, filename): - return false + return nil defer: file.close() + var f: ref Field + new(f) f.lines.add(Line()) var l = addr f.lines[0] var trailingSpaces = 0 @@ -83,7 +85,7 @@ proc Load*(f: var Field, filename: string): bool = if n <= 0: if f.ly == 0: if l.l == 0: # we got en empty file! - return false + return nil f.x = l.x if l.l > 0: inc f.ly @@ -98,7 +100,7 @@ proc Load*(f: var Field, filename: string): bool = if data[i] == '\n' or data[i] == '\r': if f.ly == 0: if l.l == 0: - return false + return nil f.x = l.x inc f.ly if f.x > l.x: @@ -130,7 +132,7 @@ proc Load*(f: var Field, filename: string): bool = inc l.l inc i f.lines = f.lines[0..