home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 101.img / QB45-1.ZIP / ENTAB.BAS < prev    next >
BASIC Source File  |  1987-09-23  |  3KB  |  105 lines

  1. ' ENTAB.BAS
  2. '
  3. ' Replace runs of spaces in a file with tabs.
  4. '
  5. DECLARE SUB SetTabPos ()
  6. DECLARE SUB StripCommand (CLine$)
  7.  
  8.  
  9. DEFINT A-Z
  10. DECLARE FUNCTION ThisIsATab (Column AS INTEGER)
  11.  
  12. CONST MAXLINE = 255
  13. CONST TABSPACE = 8
  14. CONST NO = 0, YES = NOT NO
  15.  
  16. DIM SHARED TabStops(MAXLINE) AS INTEGER
  17.  
  18. StripCommand (COMMAND$)
  19.  
  20. ' Set the tab positions (uses the global array TabStops).
  21. SetTabPos
  22.  
  23. LastColumn = 1
  24.  
  25. DO
  26.  
  27.    CurrentColumn = LastColumn
  28.  
  29. ' Replace a run of blanks with a tab when you reach a tab
  30. ' column. CurrentColumn is the current column read.
  31. ' LastColumn is the last column that was printed.
  32.    DO
  33.       C$ = INPUT$(1,#1)
  34.       IF C$ <> " " AND C$ <> CHR$(9) THEN EXIT DO
  35.       CurrentColumn = CurrentColumn + 1
  36.       IF C$=CHR$(9) OR ThisIsATab(CurrentColumn) THEN
  37.          ' Go to a tab column if we have a tab and this
  38.          ' is not a tab column.
  39.          DO WHILE NOT ThisIsATab(CurrentColumn)
  40.             CurrentColumn=CurrentColumn+1
  41.          LOOP
  42.          PRINT #2, CHR$(9);
  43.          LastColumn = CurrentColumn
  44.       END IF
  45.    LOOP
  46.  
  47. ' Print out any blanks left over.
  48.    DO WHILE LastColumn < CurrentColumn
  49.       PRINT #2, " ";
  50.       LastColumn = LastColumn + 1
  51.    LOOP
  52.  
  53. ' Print the non-blank character.
  54.    PRINT #2, C$;
  55.  
  56. ' Reset the column position if this is the end of a line.
  57.    IF C$ = CHR$(10) THEN
  58.       LastColumn = 1
  59.    ELSE
  60.       LastColumn = LastColumn + 1
  61.    END IF
  62.  
  63. LOOP UNTIL EOF(1)
  64. CLOSE #1, #2
  65. END
  66.  
  67. '------------------SUB SetTabPos-------------------------
  68. ' Set the tab positions in the array TabStops.
  69. '
  70. SUB SetTabPos STATIC
  71.    FOR I = 1 TO 255
  72.       TabStops(I) = ((I MOD TABSPACE) = 1)
  73.    NEXT I
  74. END SUB
  75. '
  76. '------------------SUB StripCommand----------------------
  77. '
  78. SUB StripCommand (CommandLine$) STATIC
  79.    IF CommandLine$ = "" THEN
  80.       INPUT "File to entab:   ", InFileName$
  81.       INPUT "Store entabbed file in:   ", OutFileName$
  82.    ELSE
  83.       SpacePos = INSTR(CommandLine$, " ")
  84.       IF SpacePos > 0 THEN
  85.          InFileName$ = LEFT$(CommandLine$, SpacePos - 1)
  86.          OutFileName$ = LTRIM$(MID$(CommandLine$, SpacePos))
  87.       ELSE
  88.          InFileName$ = CommandLine$
  89.          INPUT "Store entabbed file in:   ", OutFileName$
  90.       END IF
  91.    END IF
  92.    OPEN InFileName$ FOR INPUT AS #1
  93.    OPEN OutFileName$ FOR OUTPUT AS #2
  94. END SUB
  95. '---------------FUNCTION ThisIsATab----------------------
  96. ' Answer the question, "Is this a tab position?"
  97. '
  98. FUNCTION ThisIsATab (LastColumn AS INTEGER) STATIC
  99.    IF LastColumn > MAXLINE THEN
  100.       ThisIsATab = YES
  101.    ELSE
  102.       ThisIsATab = TabStops(LastColumn)
  103.    END IF
  104. END FUNCTION
  105.