From 44307aadac380cf9af01f8b8db6e04cc2ae4a0da Mon Sep 17 00:00:00 2001
From: Sergey Durmanov <15746-sergundo@users.noreply.gitlab.inf.ethz.ch>
Date: Mon, 18 Sep 2023 16:28:23 +0700
Subject: [PATCH] Files.Mod moved NewReader/NewWriter(from prev commits) into
 File type and extend args for reuse instanses. Usage: var f1 :=
 Files.Old(...); var w := f1.NewReader(); var f2 := Files.Old(...); w :=
 f2.NewRedaer(w, 1024); --- Reader/Wtiter -- added Base*():File; method.
 Usage: var w := Codecs.OpenOutputStream(name); ... Files.Register(w.Base())

---
 source/Files.Mod | 71 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/source/Files.Mod b/source/Files.Mod
index 3571123..ce08389 100644
--- a/source/Files.Mod
+++ b/source/Files.Mod
@@ -105,6 +105,11 @@ TYPE
 			received := pos; (* this effects that Streams.Reader.Pos() returns the correct location in the file *)
 		END SetPos;
 
+		PROCEDURE Base*(): File;
+		BEGIN
+			RETURN file;
+		END Base;
+
 		PROCEDURE &InitFileReader*(file : File; pos: Position);
 		BEGIN
 			ASSERT(file # NIL);
@@ -149,6 +154,11 @@ TYPE
 			RETURN file.Pos(r)
 		END Pos;
 
+		PROCEDURE Base*(): File;
+		BEGIN
+			RETURN file;
+		END Base;
+
 		PROCEDURE &InitFileWriter*(file: File; pos: Position);
 		BEGIN
 			ASSERT(file # NIL);
@@ -462,6 +472,29 @@ TYPE
 			(* abstract, potentially empty *)
 		END Close;
 
+		PROCEDURE NewReader*(old := NIL: Reader; pos := 0: Position): Reader;
+		BEGIN
+			IF (old # NIL) THEN
+				RESULT := old;
+				RESULT.InitFileReader(SELF, pos);
+			ELSE
+				NEW(RESULT, SELF, pos);
+			END;
+			RETURN RESULT;
+		END NewReader;
+
+		PROCEDURE NewWriter*(old := NIL: Writer; pos := 0: Position): Writer;
+		BEGIN
+			ASSERT(~(ReadOnly IN flags));
+			IF (old # NIL) THEN
+				RESULT := old;
+				RESULT.InitFileWriter(SELF, pos);
+			ELSE
+				NEW(RESULT, SELF, pos);
+			END;
+			RETURN RESULT;
+		END NewWriter;
+
 	END File;
 
 TYPE
@@ -629,22 +662,12 @@ BEGIN
 	END;
 END OpenReader;
 
-PROCEDURE NewReader*(f: File; pos: Position): Reader;
-BEGIN
-	RETURN NEW Reader(f, pos)
-END NewReader;
-
 (** Open a writer on a file at the specified position.  Remember to call Streams.Update before registering or closing the file! *)
 PROCEDURE OpenWriter*(VAR b: Writer; f: File; pos: Position);
 BEGIN
 	NEW(b, f, pos)
 END OpenWriter;
 
-PROCEDURE NewWriter*(f: File; pos: Position): Writer;
-BEGIN
-	RETURN NEW Writer(f, pos)
-END NewWriter;
-
 (** File name prefix support. *)
 
 (** Split fullname = ( prefix ":" name ) into prefix and name *)
@@ -1191,21 +1214,21 @@ PROCEDURE Enumerate(CONST mask: ARRAY OF CHAR; flags: SET; enum: Enumerator);
 VAR
 	fs: FileSystem; ft: FileSystemTable; i: SIZE;
 	prefix: Prefix; fmask: FileName;
-BEGIN
+	BEGIN
 	SplitName(mask, prefix, fmask);
-	IF prefix = "" THEN
-		GetList(ft);
-		IF ft # NIL THEN
-				(* FIX: deadlock possible if fs containing anonymous file does not allow concurrent Enumerate and Write *)
-			FOR i := 0 TO LEN(ft^)-1 DO
-				IF ~(NeedsPrefix IN ft[i].flags) THEN
-					ft[i].Enumerate0(fmask, flags, enum)
-				END;
-			END
-		END
-	ELSE
-		fs := This(prefix);
-		IF fs # NIL THEN fs.Enumerate0(fmask, flags, enum) END
+			IF prefix = "" THEN
+				GetList(ft);
+				IF ft # NIL THEN
+						(* FIX: deadlock possible if fs containing anonymous file does not allow concurrent Enumerate and Write *)
+					FOR i := 0 TO LEN(ft^)-1 DO
+						IF ~(NeedsPrefix IN ft[i].flags) THEN
+							ft[i].Enumerate0(fmask, flags, enum)
+						END;
+					END
+				END
+			ELSE
+				fs := This(prefix);
+				IF fs # NIL THEN fs.Enumerate0(fmask, flags, enum) END
 	END
 END Enumerate;
 
-- 
GitLab