home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gt_file2.prg < prev    next >
Text File  |  1993-10-14  |  2KB  |  106 lines

  1. /*
  2.     File......: GT_File2.prg
  3.     Author....: Martin Bryant
  4.     BBS.......: The Dark Knight Returns
  5.     Net/Node..: 050/069
  6.     User Name.: Martin Bryant
  7.     Date......: 04/02/93
  8.     Revision..: 1.0
  9.  
  10.     This is an original work by Martin Bryant and is placed
  11.     in the public domain.
  12.  
  13.     Modification history:
  14.     ---------------------
  15.  
  16.     Rev 1.0 04/02/93
  17.     PD Revision.
  18. */
  19.  
  20.  
  21. /*  $DOC$
  22.  *  $FUNCNAME$
  23.  *      GT_FILE2A()
  24.  *  $CATEGORY$
  25.  *      Array
  26.  *  $ONELINER$
  27.  *      Convert a text file to an array
  28.  *  $SYNTAX$
  29.  *      GT_File2a(<cFile>,[<aData>]) -> aData
  30.  *  $ARGUMENTS$
  31.  *      <cFile> is the name (including any path and
  32.  *      extension) of the text file to read from.
  33.  *
  34.  *      <aData> is an array to use to put the data into
  35.  *      added.
  36.  *  $RETURNS$
  37.  *      GT_File2a() returns an array.
  38.  *  $DESCRIPTION$
  39.  *      To read a text file in line by line and build an
  40.  *      array of the text, one line for each array element.
  41.  *  $EXAMPLES$
  42.  *      // Build a new array
  43.  *      aData := GT_File2a('C:\Import.txt')
  44.  *
  45.  *      // And add some more ....
  46.  *      aData := GT_File2a('C:\Import2.txt',aData)
  47.  *  $SEEALSO$
  48.  *
  49.  *  $INCLUDE$
  50.  *
  51.  *  $END$
  52.  */
  53.  
  54. #include "GT_LIB.ch"
  55.  
  56. //  64K max data size
  57. #define MAXFILELEN  (64 * 1024)
  58.  
  59. FUNCTION GT_File2a(cFile,aData)
  60.  
  61. Local cData := ''
  62. Local nHandle := 0
  63. Local nLineLen := 0
  64. Local nPos := 0
  65. Local nMaxLen := 0
  66.  
  67. Default cFile To ''
  68. Default aData To {}
  69.  
  70. BEGIN SEQUENCE
  71.  
  72.     // Found ?
  73.     IF .NOT. FILE(cFile)
  74.         BREAK(NIL)
  75.     ENDIF
  76.  
  77.     // Open for read only
  78.     nHandle := FOPEN(cFile,0)
  79.     IF nHandle < 0
  80.         BREAK(NIL)
  81.     ENDIF
  82.  
  83.     // Read
  84.     cData := FREADSTR(nHandle,MAXFILELEN)
  85.     nPos := AT(CRLF,cData)
  86.  
  87.     DO WHILE nPos > 0
  88.  
  89.         AADD(aData,SUBSTR(cData,1,nPos-1))
  90.         cData := SUBSTR(cData,nPos+2)
  91.  
  92.         // Complete line ?
  93.         nPos := AT(CRLF,cData)
  94.  
  95.     ENDDO
  96.  
  97. END SEQUENCE
  98.  
  99. // Reset
  100. FCLOSE(nHandle)
  101.  
  102. /*
  103.     End of GT_File2a()
  104. */
  105. RETURN(aData)
  106.