IonicWind Software

Creative Basic => Console Programs => Topic started by: Egil on May 21, 2016, 01:02:47 PM

Title: Replacing TABs with spaces
Post by: Egil on May 21, 2016, 01:02:47 PM
One of the first things I noticed when starting to use EB and CB, was that TAB indents that looked "normal" in the IDE became very exaggerated when seeing the code posted on the forum. So decided to make a little utility so the indents would look more or less the same on the forum as in my IDE.
That was the first little program I ever wrote in CB.

Have fun!

Egil.


'
'----------------------------------------------------------------------------------------
' TabFix.cba - replacing TABs with spaces and making a backup of the original file.
'----------------------------------------------------------------------------------------
'
DECLARE TabFix(Fname:string,spaces:int)
def filter,cfil:string
def number:int
print
input "Specify number of spaces: ",number

OPENCONSOLE
filter = "Creative Basic Files (*.CBA)|*.cba||"
TabFix(filerequest("Select file",0,1,filter," "),number)

CLOSECONSOLE
END

SUB TabFix(Fname:string,spaces:int)
'----------------------------------------------------------------------------------------
' Convert TABs into a specified number of SPACES
' PARAMETERS:
' Fname  = name of file to be converted
' spaces = number of spaces to replace each TAB
'----------------------------------------------------------------------------------------
def infile,outfile:FILE
def tmpfil,newfil,oldtext,newtext,a$,b$:STRING
def i,cnt:int
newfil=Fname

'Set number of spaces to replace TAB:
b$=""
  for i=1 to spaces
     b$=b$+" "
  next i

'Make a backup of the "old" file:
  cnt = len(Fname)-4
  tmpfil = left$(Fname,cnt)+".BAK"
  COPYFILE(Fname,tmpfil,0)

  OPENFILE(outfile,newfil,"W")
     IF(OPENFILE(infile,tmpfil,"R") = 0)
        WHILE EOF(infile) = 0
           i = i+1
           READ(infile,oldtext)
'Replacing TABs with specified number of spaces:
           for cnt=1 to len(oldtext)
              a$=a$+mid$(oldtext,cnt,1)
              if a$ = chr$(9) then a$ = b$
              newtext=newtext+a$
              a$=""
           next cnt
'Writing "new" text to the "old" filename:
           WRITE outfile,newtext
           newtext=""
        ENDWHILE
     CLOSEFILE infile
     ENDIF
  CLOSEFILE outfile
RETURN