! SPACER.BAS - deletes multiple spaces and replaces them with single
!              spaces. should speed up Sharon's editing considerably
! December 14, 1985  ske

! August 27, 1986 - modify to eliminate blank lines, and hopefully
! fix the bug where it gets into an endless loop while eliminating
! leading spaces ske

map1 input'text,s,150
map1 short'name,s,10            ! file name with no extension
map1 input'file'name,s,10
map1 output'file'name,s,10
map1 leader,s,1                 ! flag for eliminating leading spaces
map1 blanks,s,1                 ! flag for eliminating blank lines
map1 dummy,s,1                  ! all purpose string variable
map1 front,f                    ! set to first printing char of a line
map1 X,f                        ! lookup variable for file naming
map1 x,f                        ! checker outer for multiple spaces
map1 y,f                        ! thumb for input'text (marks the place)

initialize:
       ? tab(-1,0);
       ? tab(2,25); "Able Spacer at your service, M'am"
       call name'that'input'file
       call name'that'output'file
       call shall'we'eliminate'leading'spaces
       call shall'we'eliminate'blank'lines
       open #1, input'file'name,  input
       open #2, output'file'name, output
       ? tab(4,1); tab(-1,10);
       ? tab(4,10); "Input file: " input'file'name;
       ? tab(40); "Output file: " output'file'name
       ? "================================================================================"

       ? tab(10,1); "Processing";

loop:
       input line #1, input'text
       ? ".";
       if eof(1) = 1 &
           then &
               goto thats'all
       ! check to see if we're supposed to eliminate blank lines
sub'loop:
       if blanks = "Y" &
           then &
               if len(input'text) = 0 &
                   then &
                       goto skip'the'line'entirely
       ! on the other hand, if it's just a line with nothing in it...
       if input'text = space(len(input'text)) &
           then &
               input'text = "" :&
               goto forget'it
       ! Is the first character a control character
       ! if so, eliminate it.
       if input'text[1,1] <= chr(31) &
           then &
               input'text = input'text[2,len(input'text)] :&
               goto sub'loop
       ! Shall we eliminate leading blanks?
       if leader = "Y" &
           then &
               call check'for'leading'spaces &
           else &
               front = 1 :&
               call set'front
       call check'for'four'spaces
       call check'for'three'spaces
       call check'for'two'spaces
       call check'for'trailing'spaces
forget'it:
       ? #2, input'text
skip'the'line'entirely:
       goto loop

thats'all:
       ? tab(10,1); tab(-1,10); tab(10,35); "Done"
       close #1
       ? tab(16,29); "Results in: " output'file'name
       close #2
       ? tab(23,1);
       end

! ::::::::::::::::: File naming subroutines :::::::::::::::::::::::::::::::::::::

name'that'input'file:
       ? tab(10,1); tab(-1,10);
       ? tab(20,20); "The default extension is .TXT"
       ? tab(10,5);
       input "What file shall we de-space? --> ",input'file'name
       input'file'name = ucs(input'file'name)
       X = 0
       X = instr(1,input'file'name,".")
       if X = 0 &
           then &
               input'file'name = input'file'name + ".TXT"
       call lookup'input'file
       if X <= 0 &
           then &
               goto name'that'input'file
        return

lookup'input'file:
       X = 0
       lookup input'file'name, X
       if X <= 0 &
           then &
               ? tab(10,1); tab(-1,10) :&
               ? "I'm terribly sorry, but I can't find that file. Please check" :&
               ? "The spelling, etc. and try again " :&
               call pause
       return

name'that'output'file:
       ? tab(10,1); tab(-1,10);
       X = instr(1,input'file'name,".")
       short'name = input'file'name[1,X-1]
       ? tab(20,20); "The default name is "short'name".LST"
       ? tab(10,1);
       input "What would you like to call the output file? --> "output'file'name
       if len(output'file'name) = 0 &
           then &
               output'file'name = short'name + ".LST" &
           else &
               output'file'name = ucs(output'file'name) :&
               X = instr(1,output'file'name,".") :&
               if X = 0 &
                   then &
                       output'file'name = output'file'name + ".LST"
       return

shall'we'eliminate'leading'spaces:
       ? tab(10,1); tab(-1,10);
       input "shall we eliminate the leading spaces too? --> ", leader
       leader = ucs(leader[1,1])
       return

shall'we'eliminate'blank'lines:
       ? tab(10,1); tab(-1,10);
       input "shall we eliminate blank lines? --> ", blanks
       blanks = ucs(blanks[1,1])
       return

! :::::::::::::::::::::: text processing subroutines :::::::::::::::::::::::

check'for'leading'spaces:
       if len(input'text) > 0 &
           then &
               if input'text[1,1] = " " &
                   then &
                       input'text = input'text[2,len(input'text)] :&
                       goto check'for'leading'spaces
       return

check'for'four'spaces:
       ! x finds the spaces, y marks the spot to start looking
       x = 0
       if leader = "Y" &
           then &
               y = 1 &
           else &
               y = front
find'four'more:
       x = instr(y,input'text,"    ")
       if x > 0 &
           then &
               input'text = input'text[1,x] + input'text[x+4,len(input'text)] :&
               y = x :&
               x = 0 :&
               goto find'four'more
       return

check'for'three'spaces:
       x = 0
       if leader = "Y" &
           then &
               y = 1 &
           else &
               y = front

find'three'more:
       x = instr(y,input'text,"   ")
       if x > 0 &
           then &
               input'text = input'text[1,x] + input'text[x+3,len(input'text)] :&
               y = x :&
               x = 0 :&
               goto find'three'more
       return

check'for'two'spaces:
       x = 0
       if leader = "Y" &
           then &
               y = 1 &
           else &
               y = front

find'two'more:
       x = instr(y,input'text,"  ")
       if x > 0 &
           then &
               input'text = input'text[1,x] + input'text[x+2,len(input'text)] :&
               y = x :&
               x = 0 :&
               goto find'two'more
       return

check'for'trailing'spaces:
       if len(input'text) > 0 &
           then &
               dummy = right(input'text,1)
               if dummy = " " &
                   then &
                       input'text = input'text[1,len(input'text)-1] :&
                       goto check'for'trailing'spaces
       return

! ::::::::::::::::::::::: miscellaneous subroutines :::::::::::::::::::::::

pause:
       ? tab(23,1); tab(-1,10);
       input line "Hit RETURN to continue --> ", dummy
       ? tab(23,1) ; tab(-1,10);
       return

set'front:
       ! this sets y to the first printing character on the line
       if input'text[front,front] = " " &
           then &
               front = front + 1 :&
               goto set'front
       return