home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / clipper / viewtext / viewtext.prg
Text File  |  1992-08-19  |  5KB  |  159 lines

  1. /* 
  2.    Program name.:  Viewtext.prg.
  3.         Purpose.:  Let you browse a text file using pure Clipper code.
  4.            Date.:  August 18, 1992.
  5.              By.:  Stephen L. Woolstenhulme, CIS 73060,1702.
  6.    Restrictions.:  None.  Use it!  You could have written it yourself anyway.
  7.  
  8.         Compile.:  clipper viewtext /n /w
  9.            Link.:  rtlink fi viewtext
  10.  
  11.      Parameters.:  cFile --> name of ascii text file to view.  If Nil
  12.                              the program asks for a file name.
  13.  
  14.                    nWidth--> maximum width (widest line in file).  If
  15.                              Nil program defaults to 120 columns.
  16.  
  17.         Example.:  As a function call:  viewtext( 'myfile.txt', 80 )
  18.                    Or direct from DOS:  viewtext myfile.txt 80
  19. */
  20.  
  21.  
  22.  
  23. #include 'inkey.ch'
  24.  
  25. function ViewText( cFile, nWidth )
  26.    local getlist := {}, oBrowse, key
  27.    local cTemp := '$$$$0000.$$$'
  28.    local nFile := 10000, nRecs := 0
  29.    local cColor := setcolor( 'w+/b' )
  30.    local cScrn := savescreen( 0, 0, maxrow(), maxcol() )
  31.    local nRow := row()
  32.    local lOldScore := set( _SET_SCOREBOARD, .F. )
  33.    local nOldCursor := setcursor( 0 )
  34.  
  35.    cls
  36.  
  37.    if ! nWidth == Nil
  38.        nWidth := val( nWidth )
  39.    endif
  40.  
  41.    if nWidth == Nil .or. nWidth == 0
  42.        nWidth := 120
  43.    endif
  44.    
  45.    if ! nWidth % 20 == 0
  46.         nWidth := ( int( nWidth / 6 ) + 1 ) * 6
  47.    endif
  48.  
  49.    nWidth := max( nWidth, 80 )
  50.    
  51.    if cFile == Nil
  52.        cFile := space( 65 )
  53.    endif
  54.    
  55.    do while ! lastkey() == K_ESC .and. ;
  56.         ( empty( cFile ) .or. ! file( strtran( cFile, ' ' ) ) )
  57.         cFile := padr( cFile, 65 )
  58.         @ maxrow(), 0 say 'File to view: ' get cFile
  59.         set cursor on
  60.         read
  61.         set cursor off
  62.    enddo
  63.  
  64.    if file( strtran( cFile, ' ' ) )
  65.  
  66.        do while file( cTemp )    // create a unique file name.
  67.            nFile++
  68.            cTemp := '$$$$' + substr( str( nFile, 5 ), 2, 4 )
  69.        enddo
  70.    
  71.        if nWidth <= 80
  72.            dbcreate( cTemp, { { 'VIEW1', 'C', nWidth, 0 } } )
  73.        else
  74.            dbcreate( cTemp, { { 'VIEW1', 'C', nWidth/6, 0 } , ;
  75.                               { 'VIEW2', 'C', nWidth/6, 0 } , ;
  76.                               { 'VIEW3', 'C', nWidth/6, 0 } , ;
  77.                               { 'VIEW4', 'C', nWidth/6, 0 } , ;
  78.                               { 'VIEW5', 'C', nWidth/6, 0 } , ;
  79.                               { 'VIEW6', 'C', nWidth/6, 0 } } )
  80.        endif
  81.  
  82.        use ( cTemp ) alias TEMP new
  83.        @ maxrow(), 0 say padl( 'Reading ' + strtran( cFile, ' ' ) + ;
  84.                                '...', maxcol() ) + ' '
  85.        append from ( strtran( cFile, ' ' ) ) SDF
  86.        oBrowse := tBrowseDB( 0, 0, maxrow() - 1, maxcol() )
  87.        oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW1 } ) )
  88.  
  89.        if nwidth > 80
  90.            oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW2 } ) )
  91.            oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW3 } ) )
  92.            oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW4 } ) )
  93.            oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW5 } ) )
  94.            oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW6 } ) )
  95.        endif
  96.        
  97.        oBrowse:headSep := '═══'
  98.        oBrowse:colSep  := '' 
  99.        oBrowse:footSep := '───'
  100.        goto top
  101.        @ maxrow(), 0 say padl( 'Viewing ' + strtran( cFile, ' ' ), ;
  102.                                 maxcol() ) + ' '
  103.        nRecs := reccount()
  104.  
  105.        do while ! lastkey() == K_ESC
  106.            
  107.            do while ! oBrowse:stabilize() .and. ( key := inkey() ) == 0
  108.            enddo
  109.            @ maxrow(), 1 say padr( 'Line ' + ltrim( str( recno(), 5 ) ) + ;
  110.                                    ' of ' + ltrim( str( nrecs, 5 ) ), 20 )
  111.            key := inkey( 0 )
  112.  
  113.            do case
  114.               case key == K_UP
  115.                  oBrowse:up()
  116.               case key == K_DOWN
  117.                  oBrowse:down()
  118.               case key == K_LEFT
  119.                  oBrowse:left()
  120.               case key == K_RIGHT
  121.                  oBrowse:right()
  122.               case key == K_PGDN
  123.                  oBrowse:pageDown()
  124.               case key == K_PGUP
  125.                  oBrowse:pageUp()
  126.               case key == K_CTRL_PGDN
  127.                  oBrowse:goBottom()
  128.               case key == K_CTRL_PGUP
  129.                  oBrowse:goTop()
  130.               case key == K_HOME
  131.                  oBrowse:home()
  132.               case key == K_END
  133.                  oBrowse:end()
  134.               case key == K_CTRL_HOME
  135.                  oBrowse:panHome()
  136.               case key == K_CTRL_END
  137.                  oBrowse:panEnd()
  138.               case key == K_CTRL_LEFT
  139.                  oBrowse:panLeft()
  140.               case key == K_CTRL_RIGHT
  141.                  oBrowse:panRight()
  142.               case key == K_F5
  143.            endcase
  144.        enddo
  145.  
  146.        @ maxrow(), 0 say padl( 'Closing ' + strtran( cFile, ' ' ) + ;
  147.                                '...', maxcol() )
  148.        TEMP->( dbclosearea() )
  149.    endif
  150.  
  151.    ferase( cTemp )
  152.    setcursor( nOldCursor )
  153.    set( _SET_SCOREBOARD, lOldScore )
  154.    setcolor( cColor )
  155.    restscreen( 0, 0, maxrow(), maxcol(), cScrn )
  156.    setpos( nRow, 0 )
  157. return NIL
  158.  
  159.