Skip to content

Commit

Permalink
Add TextInputFileRaw and TextOutputFileRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJefferson committed Oct 10, 2020
1 parent 6674a19 commit f73982b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 39 deletions.
9 changes: 8 additions & 1 deletion lib/streams.gd
Original file line number Diff line number Diff line change
Expand Up @@ -623,12 +623,15 @@ DeclareOperation( "InputTextString", [ IsString ] );
## <#GAPDoc Label="InputTextFile">
## <ManSection>
## <Oper Name="InputTextFile" Arg='filename'/>
## <Oper Name="InputTextFileRaw" Arg='filename'/>
##
## <Description>
## <C>InputTextFile( <A>filename</A> )</C> returns an input stream in the category
## <Ref Filt="IsInputTextStream"/> that delivers the characters from the file
## <A>filename</A>. If <A>filename</A> ends in <C>.gz</C> and the file is
## a valid gzipped file, then the file will be transparently uncompressed.
## <C>InputTextFileRaw</C> acts identically to <C>InputTextFile</C>, except it
## disables automatic decompression.
## <P/>
## <C>InputTextFile</C> is designed for use with text files and automatically
## handles windows-style line endings. This means it should <E>not</E> be used for
Expand All @@ -639,6 +642,7 @@ DeclareOperation( "InputTextString", [ IsString ] );
## <#/GAPDoc>
##
DeclareOperation( "InputTextFile", [ IsString ] );
DeclareOperation( "InputTextFileRaw", [ IsString ] );


#############################################################################
Expand Down Expand Up @@ -726,14 +730,16 @@ DeclareOperation( "OutputTextString", [ IsList, IsBool ] );
## <#GAPDoc Label="OutputTextFile">
## <ManSection>
## <Oper Name="OutputTextFile" Arg='filename, append'/>
## <Oper Name="OutputTextFileRaw" Arg='filename, append'/>
##
## <Description>
## <C>OutputTextFile( <A>filename</A>, <A>append</A> )</C> returns an output stream in the
## category <C>IsOutputTextFile</C> that writes received characters to the file
## <A>filename</A>. If <A>append</A> is <K>false</K>, then the file is emptied first,
## otherwise received characters are added at the end of the file.
## If <A>filename</A> ends in <C>.gz</C> then the file will be
## written with gzip compression.
## written with gzip compression. <C>OutputTextFileRaw</C> acts identically to
## <C>OutputTextFile</C>, except it disables automatic compression.
## <P/>
## <Example><![CDATA[
## gap> # use a temporary directory
Expand Down Expand Up @@ -766,6 +772,7 @@ DeclareOperation( "OutputTextString", [ IsList, IsBool ] );
## <#/GAPDoc>
##
DeclareOperation( "OutputTextFile", [ IsString, IsBool ] );
DeclareOperation( "OutputTextFileRaw", [ IsString, IsBool ] );


#############################################################################
Expand Down
49 changes: 49 additions & 0 deletions lib/streams.gi
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,28 @@ function( str )
fi;
end );

#############################################################################
##
#M InputTextFileRaw( <str> ) . . . . . create a raw input text file stream
##
InstallMethod( InputTextFileRaw,
"input text stream from file without compression",
[ IsString ],
function( str )
local fid;
str := UserHomeExpand(str);

fid := INPUT_TEXT_FILE_RAW(str);
if fid = fail then
return fail;
else
atomic InputTextFileStillOpen do
AddSet( InputTextFileStillOpen, fid );
od;
return Objectify( InputTextFileType, [fid, Immutable(str)] );
fi;
end );


#############################################################################
##
Expand Down Expand Up @@ -1102,6 +1124,25 @@ function( str, append )
fi;
end );

InstallMethod( OutputTextFileRaw,
"output raw text stream from file",
[ IsString,
IsBool ],
function( str, append )
local fid;
str := UserHomeExpand(str);

fid := OUTPUT_TEXT_FILE_RAW( str, append );
if fid = fail then
return fail;
else
atomic OutputTextFileStillOpen do
AddSet( OutputTextFileStillOpen, fid );
od;
return Objectify( OutputTextFileType, [fid, Immutable(str), true] );
fi;
end );

InstallOtherMethod( OutputTextFile,
"error catching method, append not given",
[ IsString ],
Expand All @@ -1110,6 +1151,14 @@ InstallOtherMethod( OutputTextFile,
Error("Usage OutputTextFile( <fname>, <append> )");
end );

InstallOtherMethod( OutputTextFileRaw,
"error catching method, append not given",
[ IsString ],
-SUM_FLAGS, # as low as possible
function( str )
Error("Usage OutputTextFileRaw( <fname>, <append> )");
end );

#############################################################################
##
#M CloseStream( <output-text-file> )
Expand Down
12 changes: 6 additions & 6 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ UInt OpenInput (
#endif

/* try to open the input file */
file = SyFopen( filename, "r" );
file = SyFopen(filename, "r", TRUE);
if ( file == -1 )
return 0;

Expand Down Expand Up @@ -635,7 +635,7 @@ UInt OpenLog (
return 0;

/* try to open the file */
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w");
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w", TRUE);
IO()->OutputLogFileOrStream.isstream = FALSE;
if (IO()->OutputLogFileOrStream.file == -1)
return 0;
Expand Down Expand Up @@ -729,7 +729,7 @@ UInt OpenInputLog (
return 0;

/* try to open the file */
IO()->InputLogFileOrStream.file = SyFopen(filename, "w");
IO()->InputLogFileOrStream.file = SyFopen(filename, "w", TRUE);
IO()->InputLogFileOrStream.isstream = FALSE;
if (IO()->InputLogFileOrStream.file == -1)
return 0;
Expand Down Expand Up @@ -826,7 +826,7 @@ UInt OpenOutputLog (
/* try to open the file */
memset(&IO()->OutputLogFileOrStream, 0, sizeof(TypOutputFile));
IO()->OutputLogFileOrStream.isstream = FALSE;
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w");
IO()->OutputLogFileOrStream.file = SyFopen(filename, "w", TRUE);
if (IO()->OutputLogFileOrStream.file == -1)
return 0;

Expand Down Expand Up @@ -947,7 +947,7 @@ UInt OpenOutput (
#endif

/* try to open the file */
file = SyFopen( filename, "w" );
file = SyFopen(filename, "w", TRUE);
if ( file == -1 )
return 0;

Expand Down Expand Up @@ -1078,7 +1078,7 @@ UInt OpenAppend (
#endif

/* try to open the file */
file = SyFopen( filename, "a" );
file = SyFopen(filename, "a", TRUE);
if ( file == -1 )
return 0;

Expand Down
20 changes: 9 additions & 11 deletions src/saveload.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ static Int OpenForSave( Obj fname )
Pr("Already saving\n", 0, 0);
return 1;
}
SaveFile = SyFopen(CONST_CSTR_STRING(fname), "wb");
if (SaveFile == -1)
{
Pr("Couldn't open file %s to save workspace\n",
(UInt)CONST_CSTR_STRING(fname), 0);
return 1;
SaveFile = SyFopen(CONST_CSTR_STRING(fname), "wb", TRUE);
if (SaveFile == -1) {
Pr("Couldn't open file %s to save workspace\n",
(UInt)CONST_CSTR_STRING(fname), 0);
return 1;
}
LBPointer = LoadBuffer;
LBEnd = LBPointer+sizeof(LoadBuffer);
Expand All @@ -89,11 +88,10 @@ static void OpenForLoad( const Char *fname )
{
Panic("Internal error -- this should never happen");
}
LoadFile = SyFopen(fname, "rb");
if (LoadFile == -1)
{
Pr("Couldn't open saved workspace %s\n",(Int)fname, 0);
SyExit(1);
LoadFile = SyFopen(fname, "rb", TRUE);
if (LoadFile == -1) {
Pr("Couldn't open saved workspace %s\n", (Int)fname, 0);
SyExit(1);
}
}

Expand Down
50 changes: 47 additions & 3 deletions src/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,25 @@ static Obj FuncINPUT_TEXT_FILE(Obj self, Obj filename)

/* call the system dependent function */
SyClearErrorNo();
fid = SyFopen( CONST_CSTR_STRING(filename), "r" );
fid = SyFopen(CONST_CSTR_STRING(filename), "r", TRUE);
if (fid == -1)
SySetErrorNo();
return fid == -1 ? Fail : INTOBJ_INT(fid);
}

/****************************************************************************
**
*F FuncINPUT_TEXT_FILE_RAW( <self>, <name> ) open without compress
*/
static Obj FuncINPUT_TEXT_FILE_RAW(Obj self, Obj filename)
{
Int fid;

RequireStringRep(SELF_NAME, filename);

/* call the system dependent function */
SyClearErrorNo();
fid = SyFopen(CONST_CSTR_STRING(filename), "r", FALSE);
if ( fid == - 1)
SySetErrorNo();
return fid == -1 ? Fail : INTOBJ_INT(fid);
Expand Down Expand Up @@ -1288,10 +1306,34 @@ static Obj FuncOUTPUT_TEXT_FILE(Obj self, Obj filename, Obj append)
/* call the system dependent function */
SyClearErrorNo();
if ( append == True ) {
fid = SyFopen( CONST_CSTR_STRING(filename), "a" );
fid = SyFopen(CONST_CSTR_STRING(filename), "a", TRUE);
}
else {
fid = SyFopen(CONST_CSTR_STRING(filename), "w", TRUE);
}
if (fid == -1)
SySetErrorNo();
return fid == -1 ? Fail : INTOBJ_INT(fid);
}

/****************************************************************************
**
*F FuncOUTPUT_TEXT_FILE_RAW( <self>, <name>, <append> )
*/
static Obj FuncOUTPUT_TEXT_FILE_RAW(Obj self, Obj filename, Obj append)
{
Int fid;

RequireStringRep(SELF_NAME, filename);
RequireTrueOrFalse(SELF_NAME, append);

/* call the system dependent function */
SyClearErrorNo();
if (append == True) {
fid = SyFopen(CONST_CSTR_STRING(filename), "a", FALSE);
}
else {
fid = SyFopen( CONST_CSTR_STRING(filename), "w" );
fid = SyFopen(CONST_CSTR_STRING(filename), "w", FALSE);
}
if ( fid == - 1)
SySetErrorNo();
Expand Down Expand Up @@ -1763,7 +1805,9 @@ static StructGVarFunc GVarFuncs[] = {
GVAR_FUNC_1ARGS(LIST_DIR, dirname),
GVAR_FUNC_1ARGS(CLOSE_FILE, fid),
GVAR_FUNC_1ARGS(INPUT_TEXT_FILE, filename),
GVAR_FUNC_1ARGS(INPUT_TEXT_FILE_RAW, filename),
GVAR_FUNC_2ARGS(OUTPUT_TEXT_FILE, filename, append),
GVAR_FUNC_2ARGS(OUTPUT_TEXT_FILE_RAW, filename, append),
GVAR_FUNC_1ARGS(IS_END_OF_FILE, fid),
GVAR_FUNC_1ARGS(POSITION_FILE, fid),
GVAR_FUNC_1ARGS(READ_BYTE_FILE, fid),
Expand Down
19 changes: 11 additions & 8 deletions src/sysfiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Int4 SyGAPCRC( const Char * name )
Int seen_nl;

/* the CRC of a non existing file is 0 */
fid = SyFopen( name, "r" );
fid = SyFopen(name, "r", TRUE);
if ( fid == -1 ) {
return 0;
}
Expand Down Expand Up @@ -585,7 +585,7 @@ void SyBufSetEOF(Int fid)

/****************************************************************************
**
*F SyFopen( <name>, <mode> ) . . . . . . . . open the file with name <name>
*F SyFopen( <name>, <mode>, <transcompress> ) open the file with name <name>
**
** The function 'SyFopen' is called to open the file with the name <name>.
** If <mode> is "r" it is opened for reading, in this case it must exist.
Expand All @@ -606,11 +606,12 @@ void SyBufSetEOF(Int fid)
**
** Right now GAP does not read nonascii files, but if this changes sometimes
** 'SyFopen' must adjust the mode argument to open the file in binary mode.
**
** If <transcompress> is TRUE, files with names ending '.gz' will be
** automatically compressed/decompressed using gzip.
*/

Int SyFopen (
const Char * name,
const Char * mode )
Int SyFopen(const Char * name, const Char * mode, BOOL transcompress)
{
Int fid;
Char namegz [1024];
Expand Down Expand Up @@ -666,16 +667,18 @@ Int SyFopen (
#endif

/* try to open the file */
if (endsgz && (syBuf[fid].gzfp = gzopen(name, mode))) {
if (endsgz && transcompress && (syBuf[fid].gzfp = gzopen(name, mode))) {
syBuf[fid].type = gzip_socket;
syBuf[fid].fp = -1;
syBuf[fid].bufno = -1;
} else if (0 <= (syBuf[fid].fp = open(name, flags, 0644))) {
}
else if (0 <= (syBuf[fid].fp = open(name, flags, 0644))) {
syBuf[fid].type = raw_socket;
syBuf[fid].echo = syBuf[fid].fp;
syBuf[fid].bufno = -1;
}
else if (strncmp(mode, "r", 1) == 0 && SyIsReadableFile(namegz) == 0 &&
else if (strncmp(mode, "r", 1) == 0 && transcompress &&
SyIsReadableFile(namegz) == 0 &&
(syBuf[fid].gzfp = gzopen(namegz, mode))) {
syBuf[fid].type = gzip_socket;
syBuf[fid].fp = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/sysfiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void SyBufSetEOF(Int fid);
** Right now GAP does not read nonascii files, but if this changes sometimes
** 'SyFopen' must adjust the mode argument to open the file in binary mode.
*/
Int SyFopen(const Char * name, const Char * mode);
Int SyFopen(const Char * name, const Char * mode, BOOL transcompress);


/****************************************************************************
Expand Down
Loading

0 comments on commit f73982b

Please sign in to comment.