home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / ARCVIE13.ZIP / FILEVIEW.PRG < prev    next >
Text File  |  1993-09-05  |  11KB  |  298 lines

  1. /*
  2.     FILEVIEW.PRG
  3.     AUTHOR:  Eric J. Givler
  4.     WRITTEN: 06/05/92
  5.     MODS:    06/26/92 - Documented support for ARJ files.  Some 'mite' fixes.
  6.                         Added PAK support by modifying ARCDIR.PRG to not
  7.                         choke on 2nd char after chr(26) being > 12.
  8.              06/28/92 - Support for Sorting [F8] by Name, Extension or Date.
  9.              03/13/93 - Fileviewing!
  10.                         LowLevel byte determination of compressed file type.
  11.                         Creates TEMP directory under current.  DOES NOT REMOVE.
  12.              06/26/93 - #ifdef BLINKER for viewing inside archives.
  13.     SYNTAX:  FileView( { <file1.ext> [, file2.ext] } )
  14.     PURPOSE: View .ARC, .ARJ, .LZH, .PAK, .ZIP, .ZOO.  Will support more
  15.              later.
  16.     NOTES:   Pass files to view INSIDE AN ARRAY.
  17.     Calls:   FReadln(), IsDir(), MkDir(), ChDir(), Text_Browse()
  18.              IsTextFile(), SwpRunCmd()
  19. */
  20.  
  21. #include "box.ch"
  22. #include "fileio.ch"
  23. #include "inkey.ch"
  24.  
  25. #define F_NAME   1
  26. #translate ext( <f> ) => ;
  27.            if( '.' $ <f>, substr(<f>, at('.', <f>) + 1), '')
  28.  
  29. STATIC unCompress_ := { { 'ZIP', 'pkunzip -e -o [COMPFILE] temp [FILENAME]' } ,;
  30.                         { 'ARJ', 'ARJ E -y -wtemp [COMPFILE] [FILENAME]'      } ,;
  31.                         { 'LZH', 'LHA E -m -wtemp [COMPFILE] [FILENAME]'      } ,;
  32.                         { 'ARC', 'PKXARC -E -o [COMPFILE] temp [FILENAME]'  } }
  33.  
  34. *--------------------------------------------------[ FileView() ]------------*
  35. FUNCTION FILEVIEW( aFiles_ )
  36. LOCAL ncursor  := setcursor(), ;
  37.       cCol     := setcolor(), ;
  38.       c_       := { "W+/B,GR+/R", "W+/RB,W+/BG" }, ;
  39.       elems    := len( aFiles_ ), ;
  40.       contents_:= {}, ;
  41.       n        := 1, ;                        // FOR..NEXT counter.
  42.       cTestchar:= space(8), ;
  43.       nHandle, i, nkey
  44. LOCAL b1, b2, cb, nCb, index1, index2
  45. LOCAL scrn, longest, files_
  46. LOCAL cConfig, cBuffer, aExtract_ := {}
  47.  
  48.     if elems > 2 .or. VALTYPE(aFiles_) != "A"
  49.         RETURN NIL
  50.     endif
  51.  
  52.     FOR i := 1 to elems
  53.         files_ := {}
  54.         if ( nHandle := fopen( aFiles_[n], FO_READ ) ) != F_ERROR
  55.             fseek( nHandle, 0, FS_SET )
  56.             fread( nHandle, @ctestchar, 8)
  57.             fclose( nHandle )
  58.  
  59.             // cType := upper(right(aFiles_[n], 3))
  60.             ctestchar := upper(ctestchar)
  61.             do case
  62.                 case left(cTestChar,1) = chr(26)              // ARC oder PAK
  63.                     files_ := ArcDir( afiles_[n] )
  64.                 case left(cTestChar,2) = chr(96)+chr(234)     // ARJ
  65.                     files_ := ArjDir( afiles_[n] )
  66.                 case at("-LH",cTestChar) > 0                  // LZH
  67.                     files_ := LzhDir( afiles_[n] )
  68.                 case left(cTestChar,2) = "PK"                 // PKZIP
  69.                     files_ := ZipDir( afiles_[n] )
  70.                 case left(cTestChar,3) = "ZOO"                // ZOO
  71.                     files_ := ZooDir( afiles_[n] )
  72.                 case Upper(right(aFiles_[n],3)) == "DWC"      // DWC
  73.                     // Actually, you should test last 3 characters in file.
  74.                     files_ := DWCDir( aFiles_[n] ) 
  75.             endcase
  76.  
  77.         endif
  78.  
  79.         if ! empty( files_ )
  80.             aadd( contents_, files_ )
  81.             n++
  82.         else
  83.             adel( aFiles_, n )
  84.             asize( aFiles_, 1)
  85.         endif
  86.     NEXT i
  87.  
  88.  
  89.     if empty( contents_ )
  90.         ALERT("Could not view the filenames that were passed")
  91.         RETURN NIL
  92.     endif
  93.  
  94.     // Read in viewer configuration information, else use what's
  95.     // defined above.
  96.     if EMPTY( cConfig := getenv('FILEVIEW') )
  97.         aExtract_ := unCompress_
  98.     else
  99.         if (nhandle := fopen( cConfig, FO_READ )) != F_ERROR
  100.             cbuffer := space(80)
  101.             do while freadln( nhandle, @cbuffer, 80 )
  102.                 aadd( aExtract_, { left(cbuffer,at(' ',cBuffer)-1), ;
  103.                                    substr(cbuffer,at(' ',cbuffer)+1) } )
  104.             enddo
  105.             fclose(nhandle)
  106.         else
  107.             aExtract_ := unCompress_
  108.         endif
  109.     endif
  110.  
  111.     scrn  := savescreen( 0, 0, maxrow(), maxcol() )
  112.     elems := len( contents_ )
  113.     //---------------------[ Set up first browse ]--------------------------
  114.     index1 := 1
  115.     nCb := 1
  116.     setcolor(c_[1])
  117.     b1 := tbrowsenew( 8, 1, 23, if(elems==1, 78,38) )
  118.     dispbox(b1:nTop-1,b1:nLeft-1,b1:nBottom+1,b1:nRight+1, ;
  119.             B_DOUBLE + " ")
  120.     @ b1:nTop-1,b1:nLeft+1 say left(aFiles_[1],35)
  121.     b1:headsep := "═╤═"
  122.     b1:colsep  := " │ "
  123.     // b1:footsep := "═╧═"
  124.     // b1:colorspec := m->c_normcol
  125.     b1:gotopblock    := { || index1 := 1 }
  126.     b1:gobottomblock := { || index1 := len( contents_[1] ) }
  127.     b1:skipblock := { |n| ArraySkip( len( contents_[1] ), @index1, n ) }
  128.     b1:addcolumn( tbcolumnnew( "Filename",   { || contents_[1][index1][1] } ))
  129.     b1:addcolumn( tbcolumnnew( "Compressed", { || contents_[1][index1][2] } ))
  130.     b1:addcolumn( tbcolumnnew( "UnCompress", { || contents_[1][index1][3] } ))
  131.     b1:addcolumn( tbcolumnnew( "FileDate",   { || contents_[1][index1][4] } ))
  132.  
  133.     // The following 3 lines allow for the embedded ARJ path too.
  134.     longest := 0
  135.     aeval( contents_[1], { |a| longest := max( len(a[1]), longest ) } )
  136.     b1:getcolumn(1):width := longest
  137.     b1:getcolumn(2):width := 10
  138.     b1:getcolumn(3):width := 10
  139.     b1:getcolumn(4):width :=  9
  140.  
  141.     if elems > 1
  142.         index2 := 1
  143.         setcolor(c_[2])
  144.         b2 := tbrowsenew(8, 41, 23, 78)
  145.         dispbox(b2:nTop-1,b2:nLeft-1,b2:nBottom+1,b2:nRight+1, ;
  146.             B_SINGLE + " ")
  147.         @ b2:nTop-1,b2:nLeft+1 say left(aFiles_[2],35)
  148.         b2:headsep := "═╤═"
  149.         b2:colsep  := " │ "
  150.         // b2:footsep := "═╧═"
  151.         // b2:colorspec := "W+/RB,W+/BG"
  152.         b2:gotopblock    := { || index2 := 1 }
  153.         b2:gobottomblock := { || index2 := len( contents_[2] ) }
  154.         b2:skipblock := { |n| ArraySkip( len( contents_[2] ), @index2, n ) }
  155.         b2:addcolumn( tbcolumnnew( "Filename",   { || contents_[2][index2][1] } ))
  156.         b2:addcolumn( tbcolumnnew( "Compressed", { || contents_[2][index2][2] } ))
  157.         b2:addcolumn( tbcolumnnew( "UnCompress", { || contents_[2][index2][3] } ))
  158.         b2:addcolumn( tbcolumnnew( "FileDate",   { || contents_[2][index2][4] } ))
  159.         longest := 0
  160.         aeval( contents_[2], { |a| longest := max( len(a[1]), longest ) } )
  161.         b2:getcolumn(1):width := longest
  162.         b2:getcolumn(2):width := 10
  163.         b2:getcolumn(3):width := 10
  164.         b2:getcolumn(4):width :=  9
  165.     endif
  166.  
  167.     if elems > 1
  168.         cb := b2
  169.         do while ! cb:stabilize()
  170.         enddo
  171.         cb:dehilite()
  172.     endif
  173.     cb := b1
  174.  
  175.     DO WHILE .T.
  176.  
  177.         do while ! cb:stabilize()
  178.         enddo
  179.  
  180.         nKey := inkey(0)
  181.         if StdMeth(nKey, cb)
  182.             // The previous function handles standard Tbrowse keystrokes.
  183.             loop
  184.         else
  185.             do case
  186.                 #ifdef BLINKER
  187.                 case nKey == K_ENTER
  188.                     ViewFile( aFiles_[nCb], ;
  189.                               contents_[nCb][if(nCb=1,index1,index2)][1], ;
  190.                               aExtract_ )
  191.                 #endif
  192.  
  193.                 case nKey == K_TAB
  194.                     if elems > 1
  195.                         dispbox(cb:nTop-1,cb:nLeft-1,cb:nBottom+1,cb:nRight+1,;
  196.                            B_SINGLE, c_[nCb])
  197.                         @ cb:nTop-1,cb:nLeft+1 say left(aFiles_[nCb],35) color c_[nCb]
  198.                         nCb := if(nCb == 1, 2, 1)
  199.                         cb:dehilite()
  200.                         cb  := if(nCb == 1, b1, b2)
  201.                         cb:hilite()
  202.                         dispbox(cb:nTop-1,cb:nLeft-1,cb:nBottom+1,cb:nRight+1,;
  203.                            B_DOUBLE, c_[nCb])
  204.                         @ cb:nTop-1,cb:nLeft+1 say left(aFiles_[nCb],35) color c_[nCb]
  205.                     endif
  206.  
  207.                 case nKey == K_F8
  208.                     // Sort the array in current window
  209.                     nKey := ALERT("Select a different sort order.", ;
  210.                                   {"Filename", "Extension", "Date" })
  211.                     do case
  212.                         case nKey == 1
  213.                             //---------------[ FileName ]-------------------
  214.                             asort( contents_[nCb],,, ;
  215.                                { |a,b| a[F_NAME] < b[F_NAME] } )
  216.                         case nKey == 2
  217.                             //---------------[ Extension ]------------------
  218.                             asort( contents_[nCb],,, ;
  219.                                { |a,b| if( ext(a[F_NAME]) == ext(b[F_NAME]), ;
  220.                                           a[F_NAME] < b[F_NAME], ;
  221.                                           ext(a[F_NAME]) < ext(b[F_NAME]) ) } )
  222.                         case nKey == 3
  223.                             //----------------[ Date ]----------------------
  224.                             asort( contents_[nCb],,, ;
  225.                                { |a,b| if( a[4] == b[4], ;
  226.                                            a[F_NAME] < b[F_NAME], ;
  227.                                            a[4]      < b[4]) } )
  228.                     endcase
  229.                     cb:refreshall()
  230.  
  231.                 case nKey == K_ESC
  232.                     exit
  233.             endcase
  234.         endif
  235.     ENDDO
  236.  
  237.     setcursor(ncursor)
  238.     restscreen( ,,,, scrn )
  239.     setcolor(cCol)
  240.  
  241. RETURN NIL
  242.  
  243. #ifdef BLINKER
  244.  
  245. // View compressed file internal to other (based on extension here!)
  246. STATIC FUNCTION ViewFile( cMainFile, cInternal, aMethods_ )
  247. STATIC Istemp := .F.                // Whether temp directory created.
  248. LOCAL cCommand, cExtension, n, cCurDir, cScrn, nBkSlsh
  249.  
  250.     cExtension := upper(right(cMainFile,3))
  251.  
  252.      IF ( n := ascan( aMethods_, { |a_| a_[1] == cExtension } ) ) != 0
  253.  
  254.           cCurDir := '\' + CURDIR()
  255.  
  256.           if ! ISDIR( 'TEMP' )
  257.                Istemp := MKDIR( 'TEMP' )
  258.           else
  259.                Istemp := .T.
  260.           endif
  261.  
  262.           IF Istemp
  263.  
  264.                // Build extraction command with file and dump to TEMP <dir>
  265.                cCommand := aMethods_[ n ][ 2 ]
  266.                cCommand := strtran( cCommand, '[COMPFILE]', cMainFile )
  267.                cCommand := strtran( cCommand, '[FILENAME]', cInternal )
  268.                cCommand += ' >NUL'
  269.  
  270.                cScrn     := SAVESCREEN(0,0,maxrow(),maxcol())
  271.  
  272.                // Check to make sure this is not a file in a nested directory
  273.                // If there is a /, remove the path info from the filename.
  274.  
  275.                cInternal := IF( ( nBkSlsh := RAT( '/', cInternal) ) <> 0, ;
  276.                                 STUFF( cInternal, 1, nBkSlsh, ''), cInternal )
  277.  
  278.                IF cExtension == 'ARJ' .OR. cExtension == 'LZH'
  279.                     CHDIR( 'TEMP' )
  280.                ENDIF
  281.  
  282.                IF SWPRUNCMD( cCommand, 0, "", "")
  283.                     CHDIR( 'TEMP' )
  284.                     IF FILE( cInternal ) .AND. IsTextFile( cInternal )
  285.                          Text_Browse( cInternal )
  286.                     ENDIF
  287.                     FERASE( cInternal )
  288.                ENDIF
  289.  
  290.                CHDIR( cCurDir )
  291.                RESTSCREEN(,,,,cScrn)
  292.  
  293.           ENDIF
  294.      ENDIF
  295.  
  296. RETURN NIL
  297.  
  298. #endif