diff --git a/gcc/m2/gm2-libs-iso/IOChanUtils.def b/gcc/m2/gm2-libs-iso/IOChanUtils.def new file mode 100644 index 000000000000..e38f83a34834 --- /dev/null +++ b/gcc/m2/gm2-libs-iso/IOChanUtils.def @@ -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. diff --git a/gcc/m2/gm2-libs-iso/IOChanUtils.mod b/gcc/m2/gm2-libs-iso/IOChanUtils.mod new file mode 100644 index 000000000000..5cbb2a96192f --- /dev/null +++ b/gcc/m2/gm2-libs-iso/IOChanUtils.mod @@ -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. diff --git a/gcc/m2/gm2-libs-log/FileSystem.def b/gcc/m2/gm2-libs-log/FileSystem.def index 3a887205e44a..42e1399689dd 100644 --- a/gcc/m2/gm2-libs-log/FileSystem.def +++ b/gcc/m2/gm2-libs-log/FileSystem.def @@ -33,14 +33,6 @@ FROM SYSTEM IMPORT WORD, BYTE, ADDRESS ; 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 @@ -272,4 +264,21 @@ PROCEDURE Doio (VAR f: File) ; 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. diff --git a/gcc/m2/gm2-libs-log/FileSystem.mod b/gcc/m2/gm2-libs-log/FileSystem.mod index fbbc42229adb..4b06b5b4d884 100644 --- a/gcc/m2/gm2-libs-log/FileSystem.mod +++ b/gcc/m2/gm2-libs-log/FileSystem.mod @@ -29,8 +29,11 @@ IMPLEMENTATION MODULE 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 @@ -594,6 +597,37 @@ BEGIN 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. *) diff --git a/gcc/m2/gm2-libs/SFIO.def b/gcc/m2/gm2-libs/SFIO.def index 81adf8ace13d..a39043756d26 100644 --- a/gcc/m2/gm2-libs/SFIO.def +++ b/gcc/m2/gm2-libs/SFIO.def @@ -29,8 +29,6 @@ DEFINITION MODULE SFIO ; 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. @@ -91,4 +89,12 @@ PROCEDURE WriteS (file: File; s: String) : 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. diff --git a/gcc/m2/gm2-libs/SFIO.mod b/gcc/m2/gm2-libs/SFIO.mod index a4834b67a216..7feb1120b425 100644 --- a/gcc/m2/gm2-libs/SFIO.mod +++ b/gcc/m2/gm2-libs/SFIO.mod @@ -29,10 +29,12 @@ IMPLEMENTATION MODULE 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 ; (* @@ -144,4 +146,15 @@ BEGIN 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. diff --git a/gcc/testsuite/gm2/isolib/run/pass/testdelete2.mod b/gcc/testsuite/gm2/isolib/run/pass/testdelete2.mod new file mode 100644 index 000000000000..386b49d51a86 --- /dev/null +++ b/gcc/testsuite/gm2/isolib/run/pass/testdelete2.mod @@ -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. diff --git a/gcc/testsuite/gm2/pimlib/logitech/run/pass/testdelete2.mod b/gcc/testsuite/gm2/pimlib/logitech/run/pass/testdelete2.mod new file mode 100644 index 000000000000..977d498e656c --- /dev/null +++ b/gcc/testsuite/gm2/pimlib/logitech/run/pass/testdelete2.mod @@ -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. diff --git a/gcc/testsuite/gm2/pimlib/run/pass/testdelete.mod b/gcc/testsuite/gm2/pimlib/run/pass/testdelete.mod new file mode 100644 index 000000000000..8afdc445cae2 --- /dev/null +++ b/gcc/testsuite/gm2/pimlib/run/pass/testdelete.mod @@ -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. diff --git a/libgm2/libm2iso/Makefile.am b/libgm2/libm2iso/Makefile.am index 12ea38f43a84..bd8af623222f 100644 --- a/libgm2/libm2iso/Makefile.am +++ b/libgm2/libm2iso/Makefile.am @@ -104,6 +104,7 @@ M2DEFS = ChanConsts.def CharClass.def \ 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 \ @@ -149,7 +150,8 @@ M2MODS = ChanConsts.mod CharClass.mod \ 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 \ diff --git a/libgm2/libm2iso/Makefile.in b/libgm2/libm2iso/Makefile.in index 628d94240149..1e48ad06ac49 100644 --- a/libgm2/libm2iso/Makefile.in +++ b/libgm2/libm2iso/Makefile.in @@ -163,16 +163,17 @@ libm2iso_la_LIBADD = @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 \ @@ -492,6 +493,7 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS) @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 \ @@ -537,7 +539,8 @@ FLAGS_TO_PASS = $(AM_MAKEFLAGS) @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 \