home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcresour / 1981_05 / list.bas < prev    next >
BASIC Source File  |  1988-07-08  |  5KB  |  195 lines

  1. '*****************************************************************
  2. ' DOS-level file lister.
  3. ' Invoke:  LIST filespec [option]
  4. '       Options:  /H list in hex format
  5. '                 /T text only (discard non-printable characters)
  6. '
  7. ' Written in QuickBasic 4.0
  8. '******************************************************************
  9.  
  10. DEFINT A-Z
  11. DECLARE SUB GetArgs ()
  12. DECLARE SUB ParseArgs ()
  13. DECLARE SUB Help ()
  14. DECLARE SUB HexPrint ()
  15. DECLARE SUB TextPrint ()
  16. DECLARE FUNCTION Hex8$ (ByteCount&)
  17.  
  18. CONST FALSE = 0
  19. CONST TRUE = NOT FALSE
  20.  
  21. DIM SHARED Arg$(5)                   'Command-line arguments
  22. COMMON SHARED HexFlag, TextFlag
  23. COMMON SHARED ArgCount, FileName$
  24.  
  25. HexFlag = FALSE
  26. TextFlag = FALSE
  27.  
  28. '****************************************************************
  29. '  Main Program Starts Here
  30. '****************************************************************
  31.  
  32. GetArgs
  33. ParseArgs
  34. ON ERROR GOTO ErrTrap
  35. OPEN FileName$ FOR BINARY AS 1
  36. ON ERROR GOTO 0
  37. IF HexFlag THEN
  38.    HexPrint
  39. ELSE
  40.    TextPrint
  41. END IF
  42. CLOSE 1
  43. END
  44.  
  45. ErrTrap:                             'If file not found
  46. CLOSE
  47. Help
  48. END
  49.  
  50. SUB GetArgs STATIC
  51.  
  52. '***************************************************************
  53. ' Collect arguments from command line
  54. ' Based on Microsoft's code in QuickBasic 4.0 manuals
  55. '***************************************************************
  56.  
  57. MaxArgs = UBOUND(Arg$)
  58. ArgCount = 0
  59. Cmd$ = COMMAND$
  60. CmdLen = LEN(Cmd$)
  61. InWord = FALSE
  62.  
  63. FOR Lp = 1 TO LEN(Cmd$)
  64.    C$ = MID$(Cmd$, Lp, 1)
  65.    IF C$ <> " " AND C$ <> CHR$(9) THEN
  66.       IF NOT InWord THEN
  67.          IF ArgCount >= MaxArgs THEN EXIT FOR
  68.          ArgCount = ArgCount + 1
  69.          InWord = TRUE
  70.       END IF
  71.       Arg$(ArgCount) = Arg$(ArgCount) + C$
  72.    ELSE
  73.       InWord = FALSE
  74.    END IF
  75. NEXT Lp
  76.  
  77. END SUB
  78.  
  79. SUB ParseArgs STATIC
  80.  
  81. '******************************************************************
  82. ' Parse command-line arguments, set argument flags, and check
  83. ' that only valid arguments are received.
  84. '******************************************************************
  85.  
  86. FileName$ = ""
  87. FOR Lp = 1 TO ArgCount
  88.    IF LEFT$(Arg$(Lp), 1) = "/" OR LEFT$(Arg$(Lp), 1) = "-" THEN
  89.       SELECT CASE MID$(Arg$(Lp), 2, 1)
  90.          CASE "H"
  91.             HexFlag = TRUE
  92.          CASE "T"
  93.             TextFlag = TRUE
  94.          CASE ELSE
  95.             Help
  96.       END SELECT
  97.    ELSE
  98.       IF FileName$ = "" THEN
  99.          FileName$ = Arg$(Lp)
  100.       ELSE
  101.          Help
  102.       END IF
  103.    END IF
  104. NEXT Lp
  105. IF FileName$ = "" THEN Help
  106. IF HexFlag AND TextFlag THEN Help
  107.  
  108. END SUB
  109.  
  110. SUB Help
  111.  
  112. '******************************************************************
  113. ' The user is confused!  Display program syntax and exit.
  114. '******************************************************************
  115.  
  116.    PRINT "List is a file lister."
  117.    PRINT "  Syntax:  LIST FileName [option]"
  118.    PRINT "  Options: /H Use Hex Mode"
  119.    PRINT "           /T Display text characters only"
  120.    PRINT "  Both options may NOT be used at the same time"
  121.    END
  122. END SUB
  123.  
  124. SUB TextPrint STATIC
  125.  
  126. '******************************************************************
  127. ' Print the file, a byte at a time, in ASCII or Text-Only format
  128. '******************************************************************
  129.  
  130. NextCh$ = INPUT$(1, #1)
  131. DO UNTIL EOF(1)
  132.    Ch$ = NextCh$
  133.    NextCh$ = INPUT$(1, #1)
  134.      
  135.    IF Ch$ >= " " AND Ch$ <= CHR$(127) THEN
  136.       PRINT Ch$;
  137.    ELSEIF Ch$ = CHR$(13) AND NextCh$ = CHR$(10) THEN
  138.       PRINT
  139.       NextCh$ = INPUT$(1, 1)
  140.    ELSE
  141.       IF NOT TextFlag THEN
  142.          PRINT ".";
  143.       END IF
  144.    END IF
  145. LOOP
  146. PRINT
  147.  
  148. END SUB
  149.  
  150. SUB HexPrint STATIC
  151.  
  152. '******************************************************************
  153. ' Print the file in hex-dump format.  Work with one line (16 bytes)
  154. ' at a time.
  155. '******************************************************************
  156.  
  157. ByteCount& = 0
  158. DO UNTIL EOF(1)
  159.    HexLine$ = ""
  160.    AscLine$ = ""
  161.    InLine$ = INPUT$(16, #1)
  162.    ByteCount& = ByteCount& + 16
  163.   
  164.    FOR Lp = 1 TO LEN(InLine$)
  165.       Ch$ = MID$(InLine$, Lp, 1)
  166.      
  167.       Hx$ = HEX$(ASC(Ch$))
  168.       Hx$ = STRING$(2 - LEN(Hx$), "0") + Hx$
  169.       HexLine$ = HexLine$ + Hx$ + " "
  170.      
  171.       IF Ch$ < " " OR Ch$ > CHR$(127) THEN
  172.          Ch$ = "."
  173.       END IF
  174.       AscLine$ = AscLine$ + Ch$
  175.    NEXT Lp
  176.   
  177.    PRINT Hex8$(ByteCount&); TAB(14); HexLine$; TAB(64); AscLine$
  178. LOOP
  179. PRINT
  180.  
  181. END SUB
  182.  
  183. FUNCTION Hex8$ (LongInt&) STATIC
  184.  
  185. '******************************************************************
  186. ' Change a long integer (the file address) into standard hhhh:hhhh
  187. ' hexadecimal format and return as a string.
  188. '******************************************************************
  189.  
  190.    Temp$ = HEX$(LongInt&)
  191.    Temp$ = STRING$(8 - LEN(Temp$), "0") + Temp$
  192.    Hex8 = LEFT$(Temp$, 4) + ":" + MID$(Temp$, 5)
  193. END FUNCTION
  194.  
  195.