home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / stread.zip / STRMTEST.PRG < prev   
Text File  |  1994-05-14  |  3KB  |  102 lines

  1. /* StrmTest.prg : Example program, to show how to use the StrmRead function.
  2.  
  3.    StrmRead accelerates Fread dramatically by buffering the file (upto 32k)
  4.  
  5.    Written By Chris Murray, December 1993, First uploaded to Compuserve May 1994.
  6.  
  7.    Feel free to copy and distribute all the files, I would be interested
  8.    in any comments and I can be reached on compuserve mail (100043,2247).
  9.  
  10.    I would be appreciate $25, for my time and effort from people that are
  11.    going to continue using the routines, (especially companies).
  12.  
  13.    I will admit that the $25 is purely to feed my compuserve addiction,
  14.      currently runnning at $100 a month. Thats a lot of money compared to my
  15.      salary.
  16.  
  17.    For that fee I will do all the normal stuff, like sending updates when needed
  18.    and online support via compuserve.
  19.  
  20.    Even if you are an unregistered user, feel free to contact me via Compuserve
  21.    if you have any questions.
  22.  
  23.    Note : if the file you are dealing with ends without CRLF the function will
  24.           FALSE but the buffer will be filled with the remaining characters. I
  25.           haven't come across this yet in a text file. The obvious way round it
  26.           would be to use a repeat/until loop and process the buffer if the
  27.           function returns FALSE but the buffer is not empty, and of course when
  28.           the function returns TRUE.
  29. */
  30.  
  31. #include "Fileio.ch"
  32.  
  33. Procedure Main( cFileName )
  34.  
  35.   LOCAL cBuf := ""    , ;                        // line buffer
  36.         nHandle       , ;                        // dos file handle
  37.         nCounter := 0 , ;                        // line counter
  38.         nStartTime    , ;                        // timer start time
  39.         nStopTime                                // timer stop time
  40.  
  41.   Cls
  42.  
  43.   If cFileName == NIL .or. .not. File( cFileName )
  44.     cFileName := CreateTestFile()
  45.   Endif
  46.  
  47.   nHandle := Fopen( cFileName , FO_READ )
  48.  
  49.   If nHandle == F_ERROR
  50.     ? "File open error :", FERROR()
  51.   Else
  52.  
  53.     @ 05 , 10 Say "Strmread Demo, Written By Chris Murray, April 1994"
  54.  
  55.     @ 10 , 10 Say "StrmRead - Lines read    :"
  56.     @ 12 , 10 Say "           Lines per sec :"
  57.  
  58.     // accept default buffer of 32k
  59.     StrmBufReset( nHandle )
  60.  
  61.     // start timer
  62.     nStartTime := Seconds()
  63.  
  64.     Do While StrmLineRead( @cBuf )
  65.       // do stuff here
  66.       @ 10 , 37 Say Str( ++nCounter , 6 , 0 )
  67.     Enddo
  68.  
  69.     // stop timer
  70.     nStopTime := Seconds()
  71.  
  72.     // close stream and release static memory.
  73.     StrmBufReset()
  74.  
  75.     // display lines per second
  76.     @ 12 , 37 Say Str( nCounter / ( nStopTime - nStartTime ) , 7 , 2 )
  77.  
  78.   Endif
  79.  
  80.   Fclose( nHandle )
  81.  
  82. Return
  83.  
  84. Static Function CreateTestFile()
  85.  
  86.   Local cFileName := "StrmRead.clm" , ;
  87.         nCounter                    , ;
  88.         nHandle
  89.  
  90.   @ 07 , 10 Say "Creating test file, please wait..."
  91.  
  92.   nHandle := FCreate( cFileName )
  93.  
  94.   For nCounter := 1 To 1000
  95.     Fwrite( nHandle , "line : " + Str( nCounter , 6 , 0 ) + ;
  96.            Space( 80 ) + Str( nCounter , 6 , 0 ) + Chr( 13 ) + Chr( 10 ) )
  97.   Next
  98.  
  99.     FClose( nHandle )
  100.  
  101. Return( cFileName )
  102.