home *** CD-ROM | disk | FTP | other *** search
- /*
- Program name.: Viewtext.prg.
- Purpose.: Let you browse a text file using pure Clipper code.
- Date.: August 18, 1992.
- By.: Stephen L. Woolstenhulme, CIS 73060,1702.
- Restrictions.: None. Use it! You could have written it yourself anyway.
-
- Compile.: clipper viewtext /n /w
- Link.: rtlink fi viewtext
-
- Parameters.: cFile --> name of ascii text file to view. If Nil
- the program asks for a file name.
-
- nWidth--> maximum width (widest line in file). If
- Nil program defaults to 120 columns.
-
- Example.: As a function call: viewtext( 'myfile.txt', 80 )
- Or direct from DOS: viewtext myfile.txt 80
- */
-
-
-
- #include 'inkey.ch'
-
- function ViewText( cFile, nWidth )
- local getlist := {}, oBrowse, key
- local cTemp := '$$$$0000.$$$'
- local nFile := 10000, nRecs := 0
- local cColor := setcolor( 'w+/b' )
- local cScrn := savescreen( 0, 0, maxrow(), maxcol() )
- local nRow := row()
- local lOldScore := set( _SET_SCOREBOARD, .F. )
- local nOldCursor := setcursor( 0 )
-
- cls
-
- if ! nWidth == Nil
- nWidth := val( nWidth )
- endif
-
- if nWidth == Nil .or. nWidth == 0
- nWidth := 120
- endif
-
- if ! nWidth % 20 == 0
- nWidth := ( int( nWidth / 6 ) + 1 ) * 6
- endif
-
- nWidth := max( nWidth, 80 )
-
- if cFile == Nil
- cFile := space( 65 )
- endif
-
- do while ! lastkey() == K_ESC .and. ;
- ( empty( cFile ) .or. ! file( strtran( cFile, ' ' ) ) )
- cFile := padr( cFile, 65 )
- @ maxrow(), 0 say 'File to view: ' get cFile
- set cursor on
- read
- set cursor off
- enddo
-
- if file( strtran( cFile, ' ' ) )
-
- do while file( cTemp ) // create a unique file name.
- nFile++
- cTemp := '$$$$' + substr( str( nFile, 5 ), 2, 4 )
- enddo
-
- if nWidth <= 80
- dbcreate( cTemp, { { 'VIEW1', 'C', nWidth, 0 } } )
- else
- dbcreate( cTemp, { { 'VIEW1', 'C', nWidth/6, 0 } , ;
- { 'VIEW2', 'C', nWidth/6, 0 } , ;
- { 'VIEW3', 'C', nWidth/6, 0 } , ;
- { 'VIEW4', 'C', nWidth/6, 0 } , ;
- { 'VIEW5', 'C', nWidth/6, 0 } , ;
- { 'VIEW6', 'C', nWidth/6, 0 } } )
- endif
-
- use ( cTemp ) alias TEMP new
- @ maxrow(), 0 say padl( 'Reading ' + strtran( cFile, ' ' ) + ;
- '...', maxcol() ) + ' '
- append from ( strtran( cFile, ' ' ) ) SDF
- oBrowse := tBrowseDB( 0, 0, maxrow() - 1, maxcol() )
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW1 } ) )
-
- if nwidth > 80
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW2 } ) )
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW3 } ) )
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW4 } ) )
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW5 } ) )
- oBrowse:AddColumn( tbColumnNew( Nil, { || TEMP->VIEW6 } ) )
- endif
-
- oBrowse:headSep := '═══'
- oBrowse:colSep := ''
- oBrowse:footSep := '───'
- goto top
- @ maxrow(), 0 say padl( 'Viewing ' + strtran( cFile, ' ' ), ;
- maxcol() ) + ' '
- nRecs := reccount()
-
- do while ! lastkey() == K_ESC
-
- do while ! oBrowse:stabilize() .and. ( key := inkey() ) == 0
- enddo
- @ maxrow(), 1 say padr( 'Line ' + ltrim( str( recno(), 5 ) ) + ;
- ' of ' + ltrim( str( nrecs, 5 ) ), 20 )
- key := inkey( 0 )
-
- do case
- case key == K_UP
- oBrowse:up()
- case key == K_DOWN
- oBrowse:down()
- case key == K_LEFT
- oBrowse:left()
- case key == K_RIGHT
- oBrowse:right()
- case key == K_PGDN
- oBrowse:pageDown()
- case key == K_PGUP
- oBrowse:pageUp()
- case key == K_CTRL_PGDN
- oBrowse:goBottom()
- case key == K_CTRL_PGUP
- oBrowse:goTop()
- case key == K_HOME
- oBrowse:home()
- case key == K_END
- oBrowse:end()
- case key == K_CTRL_HOME
- oBrowse:panHome()
- case key == K_CTRL_END
- oBrowse:panEnd()
- case key == K_CTRL_LEFT
- oBrowse:panLeft()
- case key == K_CTRL_RIGHT
- oBrowse:panRight()
- case key == K_F5
- endcase
- enddo
-
- @ maxrow(), 0 say padl( 'Closing ' + strtran( cFile, ' ' ) + ;
- '...', maxcol() )
- TEMP->( dbclosearea() )
- endif
-
- ferase( cTemp )
- setcursor( nOldCursor )
- set( _SET_SCOREBOARD, lOldScore )
- setcolor( cColor )
- restscreen( 0, 0, maxrow(), maxcol(), cScrn )
- setpos( nRow, 0 )
- return NIL
-