home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / CLIPPER / NTXDIR / NTXDIR.PRG
Text File  |  1993-11-26  |  4KB  |  171 lines

  1. // NTXDir
  2. //
  3. // Author : ShaunB..  (Shaun Botha [CI$ 70043,2641])
  4. // Purpose: Emulate DOS's DIR command but with special handling for NTX files -
  5. //          index expression for each NTX file
  6. // Compile: Clipper NTXDIR /n/m/w
  7. // Link   : Blinker / RTLink fi NTXDIR
  8. // Notes  : This is 'fame-ware' - copy as much as you want, modify all you want,
  9. //          distribute all you want.  Just include my name as the original
  10. //          author.
  11.  
  12. #include "fileio.ch"
  13. #include "directry.ch"
  14.  
  15. #define BLOCKSZ    512
  16. #define EXPOFS        22
  17. #define NULL        chr(0)
  18.  
  19.  
  20. procedure main(cSpec, cSwitch)
  21.     local aFiles, x
  22.     local cBase, cExt, cSize, cDate, cTime, nTotSize
  23.     local dLUpd, nRecs, lPaged, nRows, lAborted, cExpr
  24.  
  25.     // Fix our parameters
  26.     if cSpec != nil
  27.         cSpec := upper(cSpec)
  28.     endif
  29.     if cSwitch != nil
  30.         cSwitch := upper(cSwitch)
  31.     else
  32.         cSwitch := ""
  33.     endif
  34.  
  35.     // Determine whether display is to be paged
  36.     lPaged := .f.
  37.     if cSpec == "/P"
  38.         lPaged := .t.
  39.         cSpec := ""
  40.         cSwitch := ""
  41.     endif
  42.     if cSwitch == "/P"
  43.         lPaged := .t.
  44.     endif
  45.  
  46.     // Default to "*.*"
  47.     if empty(cSpec)
  48.         cSpec := "*.*"
  49.     endif
  50.  
  51.     // Just some blurb
  52.     ? "NTXDIR v1.0 (c) 1993 ShaunB.."
  53.     ?
  54.  
  55.     if cSpec == "/?"
  56.         ? "Displays a directory similar to that of DOS's DIR command.  In addition"
  57.         ? "the program will display the index expression of Clipper NTX files."
  58.         ?
  59.         ? "Note: This program does not support path names in NTX files (yet)."
  60.         ?
  61.         ? "usage: NTXDIR [file_spec] [/P]"
  62.         ?
  63.         quit
  64.     endif
  65.  
  66.     ?
  67.  
  68.  
  69.     lAborted := .f.
  70.     aFiles := directory(cSpec, "HSD")      // Get all files
  71.     nRows    := 0                          // Initialize row count for pages
  72.  
  73.     // Figure total size of files
  74.     nTotSize := 0
  75.     aeval(aFiles, {|f| nTotSize += f[F_SIZE] })
  76.  
  77.     for x := 1 to len(aFiles)
  78.         if inkey() == 27
  79.             lAborted := .t.
  80.             exit
  81.         endif
  82.  
  83.         nRows++
  84.         cBase := cExt := ""                 // Init these vars
  85.  
  86.         // Treat directories differently to 'normal' files
  87.         if at("D", aFiles[x, F_ATTR]) != 0
  88.             cSize := padr("<DIR>", 8)
  89.             cBase := aFiles[x, F_NAME]
  90.         else
  91.             if at(".", aFiles[x, F_NAME]) != 0
  92.                 cBase := left(aFiles[x, F_NAME], at(".", aFiles[x, F_NAME])-1)
  93.                 cExt  := substr(aFiles[x, F_NAME], at(".", aFiles[x, F_NAME])+1)
  94.             else
  95.                 cBase := aFiles[x, F_NAME]
  96.             endif
  97.             cSize := ltrim(str(aFiles[x, F_SIZE]))
  98.             cSize := padl(cSize, 8)
  99.         endif
  100.  
  101.         // Get date and time - convert time to look like DOS
  102.         cDate := dtoc(aFiles[x, F_DATE])
  103.         cTime := left(aFiles[x, F_TIME], 5) + iif(val(left(aFiles[x, F_TIME], 2)) < 12, "a", "p")
  104.  
  105.         // Show the info
  106.         if lPaged
  107.             if nRows > maxRow()
  108.                 ?? "Press any key to continue or [Esc] to quit"
  109.                 if inkey(0) == 27
  110.                     lAborted := .t.
  111.                     ?
  112.                     exit
  113.                 endif
  114.                 ?
  115.                 nRows := 0
  116.             endif
  117.         endif
  118.         ?? padr(cBase, 9), padr(cExt, 3), cSize, cDate, cTime
  119.  
  120.         // If this is a NTX file then show the index expression
  121.         if at(".NTX", aFiles[x, F_NAME]) != 0
  122.             // Show the expression - if it's longer than what's still available
  123.             // on the screen show it on the next line
  124.             cExpr := ntxExpr(aFiles[x, F_NAME])
  125.             if len(cExpr) <= (maxCol() - col()-3)
  126.                 ?? ".." + cExpr
  127.             else
  128.                 ? ".." + cExpr
  129.                 nRows++
  130.             endif
  131.         endif
  132.         ?
  133.     next x
  134.  
  135.     // Show some summary info
  136.     if lAborted
  137.         ? "*Process aborted*"
  138.     endif
  139.     ? padl(ltrim(str(len(aFiles))), 10) + " file(s) " + padl(ltrim(str(nTotSize)),8) + " bytes used"
  140. return
  141.  
  142.  
  143. // Return the index expression for a NTX file
  144. static function ntxExpr(cFile)
  145.     local nHandle, cBuff, cExpr, x
  146.  
  147.     cExpr := "*Unknown*"
  148.     cBuff := space(BLOCKSZ)
  149.  
  150.     // Open the file as READONLY, SHARED
  151.     if (nHandle := fopen(cFile, FO_READ + FO_SHARED)) != -1
  152.         // The expression starts at byte 22 in the file
  153.         if fseek(nHandle, EXPOFS, FS_SET) == EXPOFS
  154.             // Read a block big enough to accomodate the index expression
  155.             if fread(nHandle, @cBuff, BLOCKSZ) == BLOCKSZ
  156.                 // The expression is terminated by a NULL character
  157.                 cExpr := left(cBuff, at(NULL, cBuff)-1)
  158.             else
  159.                 cExpr := "Read error in NTX file"
  160.             endif
  161.         else
  162.             cExpr := "Seek error in NTX file"
  163.         endif
  164.  
  165.         // Close the file
  166.         fclose(nHandle)
  167.     else
  168.         cExpr := "Could not open NTX file"
  169.     endif
  170. return cExpr
  171.