--- /dev/null
+DEFINITION MODULE IOChanUtils ;
+
+(*
+ Title : IOChanUtils
+ Author : Gaius Mulley
+ System : GNU Modula-2
+ Date : Sat Jun 28 23:33:06 2025
+ Revision : $Version$
+ Description: provides additional procedures to work on
+ ChanIds.
+*)
+
+FROM DynamicStrings IMPORT String ;
+
+IMPORT IOChan ;
+
+
+(*
+ GetFileName - returns the filename as a new string associated
+ with chanid c. This string should be killed by
+ the caller.
+*)
+
+PROCEDURE GetFileName (c: IOChan.ChanId) : String ;
+
+
+END IOChanUtils.
--- /dev/null
+IMPLEMENTATION MODULE IOChanUtils ;
+
+IMPORT IOChan, SFIO, RTio ;
+
+
+(*
+ GetFileName - returns the filename as a new string associated
+ with chanid c. This string should be killed by
+ the caller.
+*)
+
+PROCEDURE GetFileName (c: IOChan.ChanId) : String ;
+BEGIN
+ RETURN SFIO.GetFileName (RTio.GetFile (c))
+END GetFileName ;
+
+
+END IOChanUtils.
IMPORT FIO ;
FROM DynamicStrings IMPORT String ;
-EXPORT QUALIFIED File, Response, Flag, FlagSet,
-
- Create, Close, Lookup, Rename, Delete,
- SetRead, SetWrite, SetModify, SetOpen,
- Doio, SetPos, GetPos, Length, Reset,
-
- ReadWord, ReadChar, ReadByte, ReadNBytes,
- WriteWord, WriteChar, WriteByte, WriteNBytes ;
TYPE
File = RECORD
PROCEDURE FileNameChar (ch: CHAR) : CHAR ;
+(*
+ GetFileName - return a new string containing the name of the file.
+ The string should be killed by the caller.
+*)
+
+PROCEDURE GetFileName (file: File) : String ;
+
+
+(*
+ WriteString - writes contents to file. The nul char
+ will terminate the contents string otherwise
+ all characters 0..HIGH (contents) are written.
+*)
+
+PROCEDURE WriteString (file: File; contents: ARRAY OF CHAR) ;
+
+
END FileSystem.
FROM M2RTS IMPORT InstallTerminationProcedure ;
FROM Storage IMPORT ALLOCATE ;
FROM SYSTEM IMPORT ADR, COFF_T ;
-IMPORT SFIO, libc, wrapc ;
-FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar, KillString, string ;
+IMPORT SFIO, libc, wrapc, StrLib ;
+
+FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar,
+ KillString, string, Dup ;
+
FROM FormatStrings IMPORT Sprintf2 ;
CONST
END FileNameChar ;
+(*
+ GetFileName - return a new string containing the name of the file.
+ The string should be killed by the caller.
+*)
+
+PROCEDURE GetFileName (file: File) : String ;
+BEGIN
+ RETURN Dup (file.name)
+END GetFileName ;
+
+
+(*
+ WriteString - writes contents to file. The nul char
+ will terminate the contents string otherwise
+ all characters 0..HIGH (contents) are written.
+*)
+
+PROCEDURE WriteString (file: File; contents: ARRAY OF CHAR) ;
+VAR
+ ch : CHAR ;
+ i, high: CARDINAL ;
+BEGIN
+ i := 0 ;
+ high := StrLib.StrLen (contents) ;
+ WHILE i <= high DO
+ WriteChar (file, contents[i]) ;
+ INC (i)
+ END
+END WriteString ;
+
+
(*
MakeTemporary - creates a temporary file and returns its name.
*)
FROM DynamicStrings IMPORT String ;
FROM FIO IMPORT File ;
-EXPORT QUALIFIED OpenToRead, OpenToWrite, OpenForRandom, Exists, WriteS, ReadS ;
-
(*
Exists - returns TRUE if a file named, fname exists for reading.
PROCEDURE ReadS (file: File) : String ;
+(*
+ GetFileName - return a new string containing the name of the file.
+ The string should be killed by the caller.
+*)
+
+PROCEDURE GetFileName (file: File) : String ;
+
+
END SFIO.
FROM ASCII IMPORT nul ;
FROM DynamicStrings IMPORT string, Length, InitString, ConCatChar,
+ InitStringCharStar,
InitStringDB, InitStringCharStarDB,
InitStringCharDB, MultDB, DupDB, SliceDB ;
-FROM FIO IMPORT exists, openToRead, openToWrite, openForRandom, WriteNBytes, ReadChar,
+FROM FIO IMPORT exists, openToRead, openToWrite, openForRandom,
+ WriteNBytes, ReadChar, getFileName,
EOLN, EOF, IsNoError ;
(*
END ReadS ;
+(*
+ GetFileName - return a new string containing the name of the file.
+ The string should be killed by the caller.
+*)
+
+PROCEDURE GetFileName (file: File) : String ;
+BEGIN
+ RETURN InitStringCharStar (getFileName (file))
+END GetFileName ;
+
+
END SFIO.
--- /dev/null
+MODULE testdelete2 ;
+
+(* A test module to test file creation and deletion using ISO
+ libraries. *)
+
+
+IMPORT DynamicStrings, StringFileSysOp,
+ FileSysOp, SeqFile, TextIO, Strings,
+ IOChanUtils ;
+
+FROM libc IMPORT printf, exit ;
+FROM FormatStrings IMPORT Sprintf1 ;
+
+
+CONST
+ MaxFile = 10 ;
+
+VAR
+ files: ARRAY [0..MaxFile] OF SeqFile.ChanId ;
+
+
+PROCEDURE Assert (condition: BOOLEAN; line: CARDINAL) ;
+BEGIN
+ IF NOT condition
+ THEN
+ printf ("%s:%d: assert failed\n", __FILE__, line) ;
+ exit (1)
+ END
+END Assert ;
+
+
+(*
+ CreateFiles - create MaxFile files saving the file handle
+ into files.
+*)
+
+PROCEDURE CreateFiles ;
+VAR
+ i : CARDINAL ;
+ name: ARRAY [0..10] OF CHAR ;
+ ch : CHAR ;
+ res : SeqFile.OpenResults ;
+BEGIN
+ FOR i := 1 TO HIGH (files) DO
+ Strings.Assign ('file', name) ;
+ ch := CHR (ORD ('0')+i-1) ;
+ name[4] := ch ;
+ name[5] := 0C ;
+ SeqFile.OpenWrite (files[i], name,
+ SeqFile.text+SeqFile.write, res) ;
+ TextIO.WriteString (files[i], "some text inside file ") ;
+ TextIO.WriteLn (files[i]) ;
+ SeqFile.Close (files[i])
+ END
+END CreateFiles ;
+
+
+(*
+ DeleteFiles - delete every file in files.
+*)
+
+PROCEDURE DeleteFiles ;
+VAR
+ i : CARDINAL ;
+ name: ARRAY [0..10] OF CHAR ;
+ s : DynamicStrings.String ;
+ ch : CHAR ;
+ res : SeqFile.OpenResults ;
+BEGIN
+ (* Open the files first. *)
+ FOR i := 1 TO HIGH (files) DO
+ Strings.Assign ('file', name) ;
+ ch := CHR (ORD ('0')+i-1) ;
+ name[4] := ch ;
+ name[5] := 0C ;
+ SeqFile.OpenRead (files[i], name, SeqFile.text, res) ;
+ Assert (FileSysOp.Exists (name), __LINE__) ;
+ Assert (FileSysOp.IsFile (name), __LINE__)
+ END ;
+ (* Now delete them. *)
+ FOR i := 1 TO HIGH (files) DO
+ s := IOChanUtils.GetFileName (files[i]) ;
+ Assert (StringFileSysOp.Exists (s), __LINE__) ;
+ Assert (StringFileSysOp.IsFile (s), __LINE__) ;
+ Assert (StringFileSysOp.Unlink (s), __LINE__) ;
+ Assert (NOT StringFileSysOp.Exists (s), __LINE__) ;
+ SeqFile.Close (files[i]) ;
+ s := DynamicStrings.KillString (s)
+ END
+END DeleteFiles ;
+
+
+(*
+ Init -
+*)
+
+PROCEDURE Init ;
+BEGIN
+ CreateFiles ;
+ DeleteFiles ;
+ printf ("all tests passed\n")
+END Init ;
+
+
+BEGIN
+ Init
+END testdelete2.
--- /dev/null
+MODULE testdelete2 ;
+
+(* A test module to test file creation and deletion using log
+ libraries. *)
+
+
+IMPORT FIO, SFIO, DynamicStrings, StringFileSysOp,
+ FileSysOp, FileSystem, StrLib ;
+
+FROM libc IMPORT printf, exit ;
+FROM FormatStrings IMPORT Sprintf1 ;
+
+
+CONST
+ MaxFile = 10 ;
+
+VAR
+ files: ARRAY [0..MaxFile] OF FileSystem.File ;
+
+
+PROCEDURE Assert (condition: BOOLEAN; line: CARDINAL) ;
+BEGIN
+ IF NOT condition
+ THEN
+ printf ("%s:%d: assert failed\n", __FILE__, line) ;
+ exit (1)
+ END
+END Assert ;
+
+
+(*
+ CreateFiles - create MaxFile files saving the file handle
+ into files.
+*)
+
+PROCEDURE CreateFiles ;
+VAR
+ i : CARDINAL ;
+ name: ARRAY [0..10] OF CHAR ;
+ ch : CHAR ;
+BEGIN
+ FOR i := 1 TO HIGH (files) DO
+ StrLib.StrCopy ('file', name) ;
+ ch := CHR (ORD ('0')+i-1) ;
+ name[4] := ch ;
+ name[5] := 0C ;
+ FileSystem.Lookup (files[i], name, TRUE) ;
+ FileSystem.WriteString (files[i], "some text inside file ") ;
+ FileSystem.WriteChar (files[i], ch) ;
+ FileSystem.WriteString (files[i], "\n") ;
+ FileSystem.Close (files[i])
+ END
+END CreateFiles ;
+
+
+(*
+ DeleteFiles - delete every file in files.
+*)
+
+PROCEDURE DeleteFiles ;
+VAR
+ i : CARDINAL ;
+ name: ARRAY [0..10] OF CHAR ;
+ s : DynamicStrings.String ;
+ ch : CHAR ;
+BEGIN
+ (* Open the files first. *)
+ FOR i := 1 TO HIGH (files) DO
+ StrLib.StrCopy ('file', name) ;
+ ch := CHR (ORD ('0')+i-1) ;
+ name[4] := ch ;
+ name[5] := 0C ;
+ FileSystem.Lookup (files[i], name, FALSE) ;
+ Assert (FileSysOp.Exists (name), __LINE__) ;
+ Assert (FileSysOp.IsFile (name), __LINE__)
+ END ;
+ (* Now delete them. *)
+ FOR i := 1 TO HIGH (files) DO
+ s := FileSystem.GetFileName (files[i]) ;
+ Assert (StringFileSysOp.Exists (s), __LINE__) ;
+ Assert (StringFileSysOp.IsFile (s), __LINE__) ;
+ Assert (StringFileSysOp.Unlink (s), __LINE__) ;
+ Assert (NOT StringFileSysOp.Exists (s), __LINE__) ;
+ FileSystem.Close (files[i]) ;
+ s := DynamicStrings.KillString (s)
+ END
+END DeleteFiles ;
+
+
+(*
+ Init -
+*)
+
+PROCEDURE Init ;
+BEGIN
+ CreateFiles ;
+ DeleteFiles ;
+ printf ("all tests passed\n")
+END Init ;
+
+
+BEGIN
+ Init
+END testdelete2.
--- /dev/null
+MODULE testdelete ;
+
+(* A test module to test file creation and deletion using base
+ PIM libraries. *)
+
+
+IMPORT FIO, SFIO, DynamicStrings, StringFileSysOp ;
+FROM libc IMPORT printf, exit ;
+FROM FormatStrings IMPORT Sprintf1 ;
+
+
+CONST
+ MaxFile = 10 ;
+
+VAR
+ files: ARRAY [0..MaxFile] OF FIO.File ;
+
+
+PROCEDURE Assert (condition: BOOLEAN; line: CARDINAL) ;
+BEGIN
+ IF NOT condition
+ THEN
+ printf ("%s:%d: assert failed\n", __FILE__, line) ;
+ exit (1)
+ END
+END Assert ;
+
+
+(*
+ CreateFiles - create MaxFile files saving the file handle
+ into files.
+*)
+
+PROCEDURE CreateFiles ;
+VAR
+ i: CARDINAL ;
+ s: DynamicStrings.String ;
+BEGIN
+ FOR i := 1 TO HIGH (files) DO
+ s := DynamicStrings.InitString ("file%03d") ;
+ s := Sprintf1 (s, i) ;
+ files[i] := SFIO.OpenToWrite (s) ;
+ s := DynamicStrings.KillString (s) ;
+ s := DynamicStrings.InitString ("some text inside file %d\n") ;
+ s := Sprintf1 (s, i) ;
+ s := DynamicStrings.KillString (SFIO.WriteS (files[i], s)) ;
+ FIO.Close (files[i])
+ END
+END CreateFiles ;
+
+
+(*
+ DeleteFiles - delete every file in files.
+*)
+
+PROCEDURE DeleteFiles ;
+VAR
+ i: CARDINAL ;
+ s: DynamicStrings.String ;
+BEGIN
+ (* Open the files first. *)
+ FOR i := 1 TO HIGH (files) DO
+ s := DynamicStrings.InitString ("file%03d") ;
+ s := Sprintf1 (s, i) ;
+ files[i] := SFIO.OpenToRead (s) ;
+ Assert (StringFileSysOp.Exists (s), __LINE__) ;
+ Assert (StringFileSysOp.IsFile (s), __LINE__) ;
+ s := DynamicStrings.KillString (s)
+ END ;
+ (* Now delete them. *)
+ FOR i := 1 TO HIGH (files) DO
+ s := SFIO.GetFileName (files[i]) ;
+ Assert (StringFileSysOp.Exists (s), __LINE__) ;
+ Assert (StringFileSysOp.IsFile (s), __LINE__) ;
+ Assert (StringFileSysOp.Unlink (s), __LINE__) ;
+ Assert (NOT StringFileSysOp.Exists (s), __LINE__) ;
+ FIO.Close (files[i]) ;
+ s := DynamicStrings.KillString (s)
+ END
+END DeleteFiles ;
+
+
+(*
+ Init -
+*)
+
+PROCEDURE Init ;
+BEGIN
+ CreateFiles ;
+ DeleteFiles ;
+ printf ("all tests passed\n")
+END Init ;
+
+
+BEGIN
+ Init
+END testdelete.
ConvTypes.def COROUTINES.def \
ErrnoCategory.def EXCEPTIONS.def \
GeneralUserExceptions.def IOChan.def \
+ IOChanUtils.def \
IOConsts.def IOLink.def \
IOResult.def LongComplexMath.def \
LongConv.def LongIO.def \
ConvStringShort.mod \
ConvTypes.mod COROUTINES.mod \
EXCEPTIONS.mod GeneralUserExceptions.mod \
- IOChan.mod IOConsts.mod \
+ IOChan.mod IOChanUtils.mod \
+ IOConsts.mod \
IOLink.mod IOResult.mod \
LongComplexMath.mod LongConv.mod \
LongIO.mod LongMath.mod \
@BUILD_ISOLIB_TRUE@ ConvStringShort.lo ConvTypes.lo \
@BUILD_ISOLIB_TRUE@ COROUTINES.lo EXCEPTIONS.lo \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.lo IOChan.lo \
-@BUILD_ISOLIB_TRUE@ IOConsts.lo IOLink.lo IOResult.lo \
-@BUILD_ISOLIB_TRUE@ LongComplexMath.lo LongConv.lo LongIO.lo \
-@BUILD_ISOLIB_TRUE@ LongMath.lo LongStr.lo LongWholeIO.lo \
-@BUILD_ISOLIB_TRUE@ LowLong.lo LowReal.lo LowShort.lo \
-@BUILD_ISOLIB_TRUE@ M2EXCEPTION.lo M2RTS.lo MemStream.lo \
-@BUILD_ISOLIB_TRUE@ Preemptive.lo Processes.lo ProgramArgs.lo \
-@BUILD_ISOLIB_TRUE@ RandomNumber.lo RawIO.lo RealConv.lo \
-@BUILD_ISOLIB_TRUE@ RealIO.lo RealMath.lo RealStr.lo RndFile.lo \
-@BUILD_ISOLIB_TRUE@ RTdata.lo RTentity.lo RTfio.lo RTgenif.lo \
-@BUILD_ISOLIB_TRUE@ RTgen.lo RTio.lo Semaphores.lo SeqFile.lo \
+@BUILD_ISOLIB_TRUE@ IOChanUtils.lo IOConsts.lo IOLink.lo \
+@BUILD_ISOLIB_TRUE@ IOResult.lo LongComplexMath.lo LongConv.lo \
+@BUILD_ISOLIB_TRUE@ LongIO.lo LongMath.lo LongStr.lo \
+@BUILD_ISOLIB_TRUE@ LongWholeIO.lo LowLong.lo LowReal.lo \
+@BUILD_ISOLIB_TRUE@ LowShort.lo M2EXCEPTION.lo M2RTS.lo \
+@BUILD_ISOLIB_TRUE@ MemStream.lo Preemptive.lo Processes.lo \
+@BUILD_ISOLIB_TRUE@ ProgramArgs.lo RandomNumber.lo RawIO.lo \
+@BUILD_ISOLIB_TRUE@ RealConv.lo RealIO.lo RealMath.lo \
+@BUILD_ISOLIB_TRUE@ RealStr.lo RndFile.lo RTdata.lo RTentity.lo \
+@BUILD_ISOLIB_TRUE@ RTfio.lo RTgenif.lo RTgen.lo RTio.lo \
+@BUILD_ISOLIB_TRUE@ Semaphores.lo SeqFile.lo \
@BUILD_ISOLIB_TRUE@ ShortComplexMath.lo ShortConv.lo ShortIO.lo \
@BUILD_ISOLIB_TRUE@ ShortMath.lo ShortStr.lo ShortWholeIO.lo \
@BUILD_ISOLIB_TRUE@ SimpleCipher.lo SIOResult.lo SLongIO.lo \
@BUILD_ISOLIB_TRUE@ ConvTypes.def COROUTINES.def \
@BUILD_ISOLIB_TRUE@ ErrnoCategory.def EXCEPTIONS.def \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.def IOChan.def \
+@BUILD_ISOLIB_TRUE@ IOChanUtils.def \
@BUILD_ISOLIB_TRUE@ IOConsts.def IOLink.def \
@BUILD_ISOLIB_TRUE@ IOResult.def LongComplexMath.def \
@BUILD_ISOLIB_TRUE@ LongConv.def LongIO.def \
@BUILD_ISOLIB_TRUE@ ConvStringShort.mod \
@BUILD_ISOLIB_TRUE@ ConvTypes.mod COROUTINES.mod \
@BUILD_ISOLIB_TRUE@ EXCEPTIONS.mod GeneralUserExceptions.mod \
-@BUILD_ISOLIB_TRUE@ IOChan.mod IOConsts.mod \
+@BUILD_ISOLIB_TRUE@ IOChan.mod IOChanUtils.mod \
+@BUILD_ISOLIB_TRUE@ IOConsts.mod \
@BUILD_ISOLIB_TRUE@ IOLink.mod IOResult.mod \
@BUILD_ISOLIB_TRUE@ LongComplexMath.mod LongConv.mod \
@BUILD_ISOLIB_TRUE@ LongIO.mod LongMath.mod \