home *** CD-ROM | disk | FTP | other *** search
- **
- ** PROGRAM TITLE : DOSFILE
- **
- ** WRITTEN BY : Paul Long
- **
- ** WRITTEN FOR : Copyright (C) 1992 All rights reserved. XL Systems, Inc.
- **
- ** PROGRAM INTENT: DOS file class
- **
- #include "oclip.ch"
- #include "fileio.ch"
-
- // Beginnings of a class for general-purpose access to DOS files
- CLASS DOSFile
- VAR nHandle // File handle to DOS input file
- VAR cRecord // Current record stored here
- VAR oFound // Boolean. Whether search succeeded
- METHOD New = DOSFileNew // Constructor
- METHOD Destruct = DOSFileDestruct // Destructor
- METHOD Read = DOSFileRead // Read record from input file
- METHOD Skip = DOSFileSkip // Skips n records in file
- METHOD Locate = DOSFileLocate // Search for string in file
- METHOD Found = DOSFileFound // Return whether search succeeded
- METHOD Field = DOSFileField // Return substring from record
- ENDCLASS
-
-
- *******************************************************
- *
- * DOSFileNew
- *
- *******************************************************
- * Construct DOSFile object mainly by opening input file
- *
- METHOD FUNCTION DOSFileNew(cFileName)
-
- // Initialize all instance variables to a known state
- ::nHandle := F_ERROR
- ::cRecord := ""
- ::oFound := Boolean():New(.t.)
-
- if cFileName == nil
- ?? "*** ERROR *** missing file name"
- Self := nil
- else
- if (::nHandle := fopen(cFileName, FO_READ)) == F_ERROR
- ?? "*** ERROR *** " + cFileName + " cannot be opened"
- Self := nil
- endif
- endif
-
- return Self
-
-
- *******************************************************
- *
- * DOSFileDestruct
- *
- *******************************************************
- * Destruct DOSFile object by closing input file
- *
- METHOD FUNCTION DOSFileDestruct
-
- fclose(::nHandle)
-
- return nil
-
-
- *******************************************************
- *
- * DOSFileRead
- *
- *******************************************************
- * Read next record from input file, moving pointer to next record.
- *
- METHOD FUNCTION DOSFileRead
-
- ::cRecord := freadline(::nHandle)
-
- return Self
-
-
- *******************************************************
- *
- * DOSFileField
- *
- *******************************************************
- * Return substring from current input record.
- *
- METHOD FUNCTION DOSFileField(nStart, nCount)
-
- return if(nCount == nil, substr(::cRecord, nStart), ;
- substr(::cRecord, nStart, nCount))
-
-
- *******************************************************
- *
- * DOSFileSkip
- *
- *******************************************************
- * Skip forward through file and read the last, nth record.
- * Currently, non-positive skips are treated like Skip(1).
- * This should probably be changed to accomodate non-positive
- * skips.
- *
- METHOD FUNCTION DOSFileSkip(nRecords)
-
- local n
-
- // If nil argument, treat like Skip(1)
- nRecords := if(nRecords == nil, 0, nRecords - 1)
- ::Found(.t.)
-
- for n = 1 to nRecords
- if !(::Found(fadvance(::nHandle) != F_ERROR))
- exit
- endif
- next
-
- if ::Found()
- ::Read() // Read nth record
- else
- ::cRecord := "" // Set to null string for nothing read
- endif
-
- return Self
-
-
- *******************************************************
- *
- * DOSFileLocate
- *
- *******************************************************
- * Search for string in file. If found, the entire record is read in, and
- * file pointer is moved to beginning of next record.
- *
- METHOD FUNCTION DOSFileLocate(cString)
-
- if ::Found(flocate(::nHandle, cString) >= 0)
- fbol(::nHandle)
- ::Read()
- else
- ::cRecord := ""
- endif
-
- return Self
-
-
- *******************************************************
- *
- * DOSFileFound
- *
- *******************************************************
- * Return whether last search succeeded.
- *
- METHOD FUNCTION DOSFileFound(lWhetherFound)
-
- return ::oFound:Value(lWhetherFound)
-