home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / clipper / oop / oopexm / odosfile.prg < prev    next >
Encoding:
Text File  |  1992-01-27  |  4.2 KB  |  159 lines

  1. **
  2. **  PROGRAM TITLE  : DOSFILE
  3. **
  4. **   WRITTEN BY    :  Paul Long
  5. **
  6. **   WRITTEN FOR   :  Copyright (C) 1992 All rights reserved. XL Systems, Inc.
  7. **
  8. **   PROGRAM INTENT:  DOS file class
  9. **
  10. #include "oclip.ch"
  11. #include "fileio.ch"
  12.  
  13. // Beginnings of a class for general-purpose access to DOS files
  14. CLASS DOSFile
  15.    VAR nHandle                            // File handle to DOS input file
  16.    VAR cRecord                            // Current record stored here
  17.    VAR oFound                             // Boolean.  Whether search succeeded
  18.    METHOD New = DOSFileNew                // Constructor
  19.    METHOD Destruct = DOSFileDestruct      // Destructor
  20.    METHOD Read = DOSFileRead              // Read record from input file
  21.    METHOD Skip = DOSFileSkip              // Skips n records in file
  22.    METHOD Locate = DOSFileLocate          // Search for string in file
  23.    METHOD Found = DOSFileFound            // Return whether search succeeded
  24.    METHOD Field = DOSFileField            // Return substring from record
  25. ENDCLASS
  26.  
  27.  
  28. *******************************************************
  29. *
  30. *                             DOSFileNew
  31. *
  32. *******************************************************
  33. * Construct DOSFile object mainly by opening input file
  34. *
  35. METHOD FUNCTION DOSFileNew(cFileName)
  36.  
  37. // Initialize all instance variables to a known state
  38. ::nHandle := F_ERROR
  39. ::cRecord := ""
  40. ::oFound := Boolean():New(.t.)
  41.  
  42. if cFileName == nil
  43.    ?? "*** ERROR *** missing file name"
  44.    Self := nil
  45. else
  46.    if (::nHandle := fopen(cFileName, FO_READ)) == F_ERROR
  47.       ?? "*** ERROR *** " + cFileName + " cannot be opened"
  48.       Self := nil
  49.    endif
  50. endif
  51.  
  52. return Self
  53.  
  54.  
  55. *******************************************************
  56. *
  57. *                             DOSFileDestruct
  58. *
  59. *******************************************************
  60. * Destruct DOSFile object by closing input file
  61. *
  62. METHOD FUNCTION DOSFileDestruct
  63.  
  64. fclose(::nHandle)
  65.  
  66. return nil
  67.  
  68.  
  69. *******************************************************
  70. *
  71. *                             DOSFileRead
  72. *
  73. *******************************************************
  74. * Read next record from input file, moving pointer to next record.
  75. *
  76. METHOD FUNCTION DOSFileRead
  77.  
  78. ::cRecord := freadline(::nHandle)
  79.  
  80. return Self
  81.  
  82.  
  83. *******************************************************
  84. *
  85. *                             DOSFileField
  86. *
  87. *******************************************************
  88. * Return substring from current input record.
  89. *
  90. METHOD FUNCTION DOSFileField(nStart, nCount)
  91.  
  92. return if(nCount == nil, substr(::cRecord, nStart), ;
  93.                          substr(::cRecord, nStart, nCount))
  94.  
  95.  
  96. *******************************************************
  97. *
  98. *                             DOSFileSkip
  99. *
  100. *******************************************************
  101. * Skip forward through file and read the last, nth record.
  102. * Currently, non-positive skips are treated like Skip(1).
  103. * This should probably be changed to accomodate non-positive
  104. * skips.
  105. *
  106. METHOD FUNCTION DOSFileSkip(nRecords)
  107.  
  108. local n
  109.  
  110. // If nil argument, treat like Skip(1)
  111. nRecords := if(nRecords == nil, 0, nRecords - 1)
  112. ::Found(.t.)
  113.  
  114. for n = 1 to nRecords
  115.    if !(::Found(fadvance(::nHandle) != F_ERROR))
  116.       exit
  117.    endif
  118. next
  119.  
  120. if ::Found()
  121.    ::Read()                               // Read nth record
  122. else
  123.    ::cRecord := ""                        // Set to null string for nothing read
  124. endif
  125.  
  126. return Self
  127.  
  128.  
  129. *******************************************************
  130. *
  131. *                             DOSFileLocate
  132. *
  133. *******************************************************
  134. * Search for string in file.  If found, the entire record is read in, and
  135. * file pointer is moved to beginning of next record.
  136. *
  137. METHOD FUNCTION DOSFileLocate(cString)
  138.  
  139. if ::Found(flocate(::nHandle, cString) >= 0)
  140.    fbol(::nHandle)
  141.    ::Read()
  142. else
  143.    ::cRecord := ""
  144. endif
  145.  
  146. return Self
  147.  
  148.  
  149. *******************************************************
  150. *
  151. *                             DOSFileFound
  152. *
  153. *******************************************************
  154. * Return whether last search succeeded.
  155. *
  156. METHOD FUNCTION DOSFileFound(lWhetherFound)
  157.  
  158. return ::oFound:Value(lWhetherFound)
  159.