$include (..\lib\compStch.ext) /* *============================================================================ * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE * * Permission to use for any purpose, modify, copy, and make enhancements * and derivative works of the software is granted if attribution is given to * R.M. Gillmore, dba the ACME Software Deli, as the author * * While the ACME Software Deli does not work for money, there is nonetheless * a significant amount of work involved. The ACME Software Deli maintains the * rights to all code written, though it may be used and distributed as long as * the following conditions are maintained. * * 1. The copyright statement at the top of each code block is maintained in * your distribution. * 2. You do not identify yourself as the ACME Software Deli * 3. Any changes made to the software are sent to the ACME Software Deli *============================================================================ */ fgetsModule: do; $if not noID declare IDString (*) byte data ( '@(#)fgets.p86 $Author: rmgillmore $ $Date:: 2025-05-04 19:35:39#$:', 0 ); $endif $include (..\lib\comnDefs.ext) $set( fgetsSource ) $include (..\lib\fileIO.ext) $include (..\lib\string.ext) $include (..\lib\ptrMath.ext) /* * fgets() reads characters from stream and stores them as a string into charArray * until (charArrayLength-1) characters have been read, a newline has been read, * or the end-of-file is reached, whichever happens first. * * A newline character makes fgets() stop reading, but it is considered a valid * character by the function and included in the string copied to charArray. */ fgets: procedure ( fileHandle, charArrayPtr, charArrayLength ) pointer reentrant public; declare fileHandle word, charArrayPtr pointer, charArray based charArrayPtr (*) char, charArrayLength word; declare filePosition dword, localCharArray ( MAX_LINE ) char, numberCharRead word, localCharPtr pointer, localChar based localCharPtr char, readStringLength word, desiredFilePosition dword; /* * in order to ensure that the read after this one is correct, we need to * keep track of where we are now. */ filePosition = ftell( fileHandle ); /* * now we want to read from the specified file the maximum buffer which * can exist for a line. after the read, NUL terminate the string */ numberCharRead = fread( fileHandle, @localCharArray, length( localCharArray ) ); localCharArray( numberCharRead ) = NUL; if ( 0 < numberCharRead ) then do; /* * we read something, so look for a new-line */ localCharPtr = strchr( @localCharArray, CR ); if ( NULL <> localCharPtr ) then do; if ( CR = localChar ) then do; localCharPtr = incPtr( localCharPtr, size( localChar ), 1 ); if ( LF = localChar ) then do; localCharPtr = incPtr( localCharPtr, size( localChar ), 1 ); end; end; /* * NUL terminate the string */ localChar = NUL; end; /* * adjust the file pointer so that the next read gets the beginning of * the next line */ readStringLength = strlen( @localCharArray ); desiredFilePosition = filePosition + double( readStringLength ); call fseek( fileHandle, FROM_BEGINNING_OF_FILE, desiredFilePosition ); filePosition = ftell( fileHandle ); end; if ( ( readStringLength < charArrayLength ) and ( 0 < numberCharRead ) ) then do; call strcpy( charArrayPtr, @localCharArray ); localCharPtr = charArrayPtr; end; else localCharPtr = NULL; return ( localCharPtr ); end fgets; end fgetsModule;