[PATCH] [PR modula2/117203] Followup add Delete procedure function

This patch provides GetFileName procedure function for
FIO.File, FileSystem.File and IOChan.ChanId.  The
return result from these procedures can be passed into
StringFileSysOp.Unlink to complete the required delete.

gcc/m2/ChangeLog:

	PR modula2/117203
	* gm2-libs-log/FileSystem.def (GetFileName): New
	procedure function.
	(WriteString): New procedure.
	* gm2-libs-log/FileSystem.mod (GetFileName): New
	procedure function.
	(WriteString): New procedure.
	* gm2-libs/SFIO.def (GetFileName): New procedure function.
	* gm2-libs/SFIO.mod (GetFileName): New procedure function.
	* gm2-libs-iso/IOChanUtils.def: New file.
	* gm2-libs-iso/IOChanUtils.mod: New file.

libgm2/ChangeLog:

	PR modula2/117203
	* libm2iso/Makefile.am (M2DEFS): Add IOChanUtils.def.
	(M2MODS): Add IOChanUtils.mod.
	* libm2iso/Makefile.in: Regenerate.

gcc/testsuite/ChangeLog:

	PR modula2/117203
	* gm2/isolib/run/pass/testdelete2.mod: New test.
	* gm2/pimlib/logitech/run/pass/testdelete2.mod: New test.
	* gm2/pimlib/run/pass/testdelete.mod: New test.

(cherry picked from commit 620a40fa88)

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
This commit is contained in:
Gaius Mulley 2025-07-18 08:48:22 +01:00
parent 5a5e72590c
commit f0a86bbc82
11 changed files with 445 additions and 25 deletions

View File

@ -0,0 +1,27 @@
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.

View File

@ -0,0 +1,18 @@
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.

View File

@ -33,14 +33,6 @@ FROM SYSTEM IMPORT WORD, BYTE, ADDRESS ;
IMPORT FIO ; IMPORT FIO ;
FROM DynamicStrings IMPORT String ; 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 TYPE
File = RECORD File = RECORD
@ -272,4 +264,21 @@ PROCEDURE Doio (VAR f: File) ;
PROCEDURE FileNameChar (ch: CHAR) : CHAR ; 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. END FileSystem.

View File

@ -29,8 +29,11 @@ IMPLEMENTATION MODULE FileSystem ;
FROM M2RTS IMPORT InstallTerminationProcedure ; FROM M2RTS IMPORT InstallTerminationProcedure ;
FROM Storage IMPORT ALLOCATE ; FROM Storage IMPORT ALLOCATE ;
FROM SYSTEM IMPORT ADR, COFF_T ; FROM SYSTEM IMPORT ADR, COFF_T ;
IMPORT SFIO, libc, wrapc ; IMPORT SFIO, libc, wrapc, StrLib ;
FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar, KillString, string ;
FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar,
KillString, string, Dup ;
FROM FormatStrings IMPORT Sprintf2 ; FROM FormatStrings IMPORT Sprintf2 ;
CONST CONST
@ -594,6 +597,37 @@ BEGIN
END FileNameChar ; 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. MakeTemporary - creates a temporary file and returns its name.
*) *)

View File

@ -29,8 +29,6 @@ DEFINITION MODULE SFIO ;
FROM DynamicStrings IMPORT String ; FROM DynamicStrings IMPORT String ;
FROM FIO IMPORT File ; FROM FIO IMPORT File ;
EXPORT QUALIFIED OpenToRead, OpenToWrite, OpenForRandom, Exists, WriteS, ReadS ;
(* (*
Exists - returns TRUE if a file named, fname exists for reading. Exists - returns TRUE if a file named, fname exists for reading.
@ -91,4 +89,12 @@ PROCEDURE WriteS (file: File; s: String) : String ;
PROCEDURE ReadS (file: File) : String ; 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. END SFIO.

View File

@ -29,10 +29,12 @@ IMPLEMENTATION MODULE SFIO ;
FROM ASCII IMPORT nul ; FROM ASCII IMPORT nul ;
FROM DynamicStrings IMPORT string, Length, InitString, ConCatChar, FROM DynamicStrings IMPORT string, Length, InitString, ConCatChar,
InitStringCharStar,
InitStringDB, InitStringCharStarDB, InitStringDB, InitStringCharStarDB,
InitStringCharDB, MultDB, DupDB, SliceDB ; 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 ; EOLN, EOF, IsNoError ;
(* (*
@ -144,4 +146,15 @@ BEGIN
END ReadS ; 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. END SFIO.

View File

@ -0,0 +1,107 @@
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.

View File

@ -0,0 +1,104 @@
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.

View File

@ -0,0 +1,97 @@
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.

View File

@ -104,6 +104,7 @@ M2DEFS = ChanConsts.def CharClass.def \
ConvTypes.def COROUTINES.def \ ConvTypes.def COROUTINES.def \
ErrnoCategory.def EXCEPTIONS.def \ ErrnoCategory.def EXCEPTIONS.def \
GeneralUserExceptions.def IOChan.def \ GeneralUserExceptions.def IOChan.def \
IOChanUtils.def \
IOConsts.def IOLink.def \ IOConsts.def IOLink.def \
IOResult.def LongComplexMath.def \ IOResult.def LongComplexMath.def \
LongConv.def LongIO.def \ LongConv.def LongIO.def \
@ -149,7 +150,8 @@ M2MODS = ChanConsts.mod CharClass.mod \
ConvStringShort.mod \ ConvStringShort.mod \
ConvTypes.mod COROUTINES.mod \ ConvTypes.mod COROUTINES.mod \
EXCEPTIONS.mod GeneralUserExceptions.mod \ EXCEPTIONS.mod GeneralUserExceptions.mod \
IOChan.mod IOConsts.mod \ IOChan.mod IOChanUtils.mod \
IOConsts.mod \
IOLink.mod IOResult.mod \ IOLink.mod IOResult.mod \
LongComplexMath.mod LongConv.mod \ LongComplexMath.mod LongConv.mod \
LongIO.mod LongMath.mod \ LongIO.mod LongMath.mod \

View File

@ -163,16 +163,17 @@ libm2iso_la_LIBADD =
@BUILD_ISOLIB_TRUE@ ConvStringShort.lo ConvTypes.lo \ @BUILD_ISOLIB_TRUE@ ConvStringShort.lo ConvTypes.lo \
@BUILD_ISOLIB_TRUE@ COROUTINES.lo EXCEPTIONS.lo \ @BUILD_ISOLIB_TRUE@ COROUTINES.lo EXCEPTIONS.lo \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.lo IOChan.lo \ @BUILD_ISOLIB_TRUE@ GeneralUserExceptions.lo IOChan.lo \
@BUILD_ISOLIB_TRUE@ IOConsts.lo IOLink.lo IOResult.lo \ @BUILD_ISOLIB_TRUE@ IOChanUtils.lo IOConsts.lo IOLink.lo \
@BUILD_ISOLIB_TRUE@ LongComplexMath.lo LongConv.lo LongIO.lo \ @BUILD_ISOLIB_TRUE@ IOResult.lo LongComplexMath.lo LongConv.lo \
@BUILD_ISOLIB_TRUE@ LongMath.lo LongStr.lo LongWholeIO.lo \ @BUILD_ISOLIB_TRUE@ LongIO.lo LongMath.lo LongStr.lo \
@BUILD_ISOLIB_TRUE@ LowLong.lo LowReal.lo LowShort.lo \ @BUILD_ISOLIB_TRUE@ LongWholeIO.lo LowLong.lo LowReal.lo \
@BUILD_ISOLIB_TRUE@ M2EXCEPTION.lo M2RTS.lo MemStream.lo \ @BUILD_ISOLIB_TRUE@ LowShort.lo M2EXCEPTION.lo M2RTS.lo \
@BUILD_ISOLIB_TRUE@ Preemptive.lo Processes.lo ProgramArgs.lo \ @BUILD_ISOLIB_TRUE@ MemStream.lo Preemptive.lo Processes.lo \
@BUILD_ISOLIB_TRUE@ RandomNumber.lo RawIO.lo RealConv.lo \ @BUILD_ISOLIB_TRUE@ ProgramArgs.lo RandomNumber.lo RawIO.lo \
@BUILD_ISOLIB_TRUE@ RealIO.lo RealMath.lo RealStr.lo RndFile.lo \ @BUILD_ISOLIB_TRUE@ RealConv.lo RealIO.lo RealMath.lo \
@BUILD_ISOLIB_TRUE@ RTdata.lo RTentity.lo RTfio.lo RTgenif.lo \ @BUILD_ISOLIB_TRUE@ RealStr.lo RndFile.lo RTdata.lo RTentity.lo \
@BUILD_ISOLIB_TRUE@ RTgen.lo RTio.lo Semaphores.lo SeqFile.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@ ShortComplexMath.lo ShortConv.lo ShortIO.lo \
@BUILD_ISOLIB_TRUE@ ShortMath.lo ShortStr.lo ShortWholeIO.lo \ @BUILD_ISOLIB_TRUE@ ShortMath.lo ShortStr.lo ShortWholeIO.lo \
@BUILD_ISOLIB_TRUE@ SimpleCipher.lo SIOResult.lo SLongIO.lo \ @BUILD_ISOLIB_TRUE@ SimpleCipher.lo SIOResult.lo SLongIO.lo \
@ -492,6 +493,7 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@ ConvTypes.def COROUTINES.def \ @BUILD_ISOLIB_TRUE@ ConvTypes.def COROUTINES.def \
@BUILD_ISOLIB_TRUE@ ErrnoCategory.def EXCEPTIONS.def \ @BUILD_ISOLIB_TRUE@ ErrnoCategory.def EXCEPTIONS.def \
@BUILD_ISOLIB_TRUE@ GeneralUserExceptions.def IOChan.def \ @BUILD_ISOLIB_TRUE@ GeneralUserExceptions.def IOChan.def \
@BUILD_ISOLIB_TRUE@ IOChanUtils.def \
@BUILD_ISOLIB_TRUE@ IOConsts.def IOLink.def \ @BUILD_ISOLIB_TRUE@ IOConsts.def IOLink.def \
@BUILD_ISOLIB_TRUE@ IOResult.def LongComplexMath.def \ @BUILD_ISOLIB_TRUE@ IOResult.def LongComplexMath.def \
@BUILD_ISOLIB_TRUE@ LongConv.def LongIO.def \ @BUILD_ISOLIB_TRUE@ LongConv.def LongIO.def \
@ -537,7 +539,8 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS)
@BUILD_ISOLIB_TRUE@ ConvStringShort.mod \ @BUILD_ISOLIB_TRUE@ ConvStringShort.mod \
@BUILD_ISOLIB_TRUE@ ConvTypes.mod COROUTINES.mod \ @BUILD_ISOLIB_TRUE@ ConvTypes.mod COROUTINES.mod \
@BUILD_ISOLIB_TRUE@ EXCEPTIONS.mod GeneralUserExceptions.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@ IOLink.mod IOResult.mod \
@BUILD_ISOLIB_TRUE@ LongComplexMath.mod LongConv.mod \ @BUILD_ISOLIB_TRUE@ LongComplexMath.mod LongConv.mod \
@BUILD_ISOLIB_TRUE@ LongIO.mod LongMath.mod \ @BUILD_ISOLIB_TRUE@ LongIO.mod LongMath.mod \