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

  1. /*
  2.  * GT CLIPPER STANDARD HEADER
  3.  *
  4.  * File......: fgetlr.prg
  5.  * Author....: Andy M Leighton
  6.  * BBS.......: The Dark Knight Returns
  7.  * Net/Node..: 050/069
  8.  * User Name.: Andy Leighton
  9.  * Date......: $Date$
  10.  * Revision..: $Revision$
  11.  *
  12.  * This is an original work by Andy Leighton and is placed in the
  13.  * public domain.
  14.  *
  15.  * Modification history:
  16.  * ---------------------
  17.  *
  18.  * $Log$
  19.  *
  20.  */
  21.  
  22. /*  $DOC$
  23.  *  $FUNCNAME$
  24.  *       GT_FGETLR()
  25.  *  $CATEGORY$
  26.  *       File I/O
  27.  *  $ONELINER$
  28.  *       File GET Logical Record.
  29.  *  $SYNTAX$
  30.  *       GT_FGetLR(<fp>, [<cCont>]) --> cBuff
  31.  *  $ARGUMENTS$
  32.  *       <fp>          - file pointer
  33.  *       <cCont>       - continuation character (will default to \ the
  34.  *                       standard C (and UNIX) continuity character)
  35.  *  $RETURNS$
  36.  *       cBuff         - Line of text excluding cont. characters and
  37.  *                       any CHR(13)+CHR(10) pairs
  38.  *  $DESCRIPTION$
  39.  *       Reads the next logical record from stream 'fp' into a
  40.  *       buffer until next unescaped newline, end of file or read
  41.  *       error.
  42.  *       A logical record may span several physical records (lines)
  43.  *       by having each newline escaped with the standard C escape
  44.  *       character (for those who don't know it is the \).  This
  45.  *       character may be overridden by any other character eg. a
  46.  *       semi-colon if you are trying to read clipper source.
  47.  *  $EXAMPLES$
  48.  *       See Test Harness in source.
  49.  *       compile with -DTESt
  50.  *  $CAVEATS$
  51.  *       Poor error handling for files, will return what has been
  52.  *       read up until the file error.
  53.  *
  54.  *       Assumes that CHR(13) does not occur by itself and is always
  55.  *       paired with a CHR(10)
  56.  *  $END$
  57.  */
  58.  
  59. #include "gt_LIB.ch"
  60. /*
  61.  * TEST HARNESS
  62.  *
  63.  *    Create a file with some normal lines and some continued lines.
  64.  *    and try and read it back and compare with what is expected
  65.  */
  66.  
  67. #ifdef TEST
  68.  
  69. function main()
  70.  
  71.    local aTestFil := { "Line 1",                                ;
  72.                        "Line 2 with a \ continuation \",        ;
  73.                        "more of line 2",                        ;
  74.                        "Line 3 the same file can have ;",       ;
  75.                        "more than one cont. character",         ;
  76.                        "Line 4 a normal line.",                 ;
  77.                        "Line 5" }
  78.    local i
  79.    local cLine    := chr(255)
  80.    local fp
  81.  
  82.    clear
  83.  
  84.    fp := fcreate("FGETLR.TST")
  85.  
  86. /* write out the file, don't bother error checking */
  87.  
  88.    for i := 1 to len(aTestFil)
  89.       fwrite(fp, aTestFil[i] + chr(13) + chr(10))
  90.    next
  91.  
  92.    fclose(fp)
  93.  
  94.    fp := fopen("FGETLR.TST")
  95.  
  96.    if ferror() == 0
  97.  
  98.    /* get 1st line */
  99.       cLine := GT_fGetLR(fp)
  100.       ? cLine
  101.       ?
  102.  
  103.    /* get 2nd line */
  104.       cLine := GT_fGetLR(fp)
  105.       ? cLine
  106.       ?
  107.  
  108.    /* get 3rd line */
  109.       cLine := GT_fGetLR(fp, ';')
  110.       ? cLine
  111.       ?
  112.  
  113.    /* get 4th line */
  114.       cLine := GT_fGetLR(fp)
  115.       ? cLine
  116.       ?
  117.  
  118.    /* get 5th line */
  119.       cLine := GT_fGetLR(fp)
  120.       ? cLine
  121.       ?
  122.  
  123.       inkey(0)
  124.    endif
  125.  
  126. return NIL
  127.  
  128. #endif
  129. /* */
  130. function GT_FGetLR(fp, cCont)
  131.  
  132.    local cBuff  := ""
  133.    local cTBuff := " "
  134.    local ignore := " "
  135.  
  136.    default cCont to "\"
  137.  
  138.    do while fread(fp, @cTBuff, 1) > 0
  139.       if ferror() != 0
  140.          exit
  141.       endif
  142.  
  143.       if ctBuff == chr(13)
  144.  
  145.          fread(fp, @ignore, 1)                            // ignore chr(10)
  146.  
  147.          if substr(cBuff, len(cBuff), 1) == cCont
  148.             cBuff := substr(cBuff, 1, len(cBuff) - 1)     // del cont. char
  149.          else
  150.             exit                                          // got proper EOL
  151.          endif
  152.       else
  153.          cBuff := cBuff + ctBuff
  154.       endif
  155.    enddo
  156.  
  157. return cBuff
  158.