WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Auto Indent WBT Script


;automaticaly indented
;by Winbatch
;--------------------------------------------------------------------------
; OBJET   : indent a  WIL program.
; INPUT   : WIL program choosen by user.
; OUTPUT  : WIL program xx.wbt : indented program choosen.
; DATE    : 18 juin 2004.
; VERSION : 1.0
; auteur  : delgove  jean-jacques
; No modification of code is done apart additionnal comment and indentation
;---------------------------------------------------------------------------

; choix du programme WIL en entrée
; --------------------------------
types="WIL Files|*.wbt;*.mnu|"
input=AskFileName("Select Winbatch file to indent", dirget(), types, "", 1)
output="xx.wbt"
old = FileOpen(input, "READ")

x = FileRead(old)

; test fichier vide
; -----------------
If x == "*EOF*" Then
	Message("INFO","File empty")
	exit
endif

; si le progamme a déjà été indenté par ce programme
; on sort un message

if ( x == ";automaticaly indented") then
	Message("INFO","File already automaticaly indended")
	exit
endif
fileclose(old)

; on commence l'indentation

old = FileOpen(input, "READ")
new = FileOpen(output, "WRITE")

; on marque le fichier de sortie
FileWrite(new, strcat(";automaticaly indented",@crlf,";by Winbatch"))
indent=0

; I N I T
;on précalcule les tabulations 20 max
; pour gagner du temps
x=""
tab0=""
for i=1 to 20
	x=strcat(x,@tab)
	tab%i%=x
next
fct=0
r=strcat(" ",@tab)

; boucle de lecture du fichier d'entrée
while @TRUE             ; Loop till break do us end
	xx = FileRead(old)
	If xx == "*EOF*" Then Break
	if ( xx == ";already indented") then
		Message("INFO","File already indended")
		exit
	endif
	xx=StrTrim(xx)
	; on met toute le ligne en capitales pour
	; gagner des traitements
	x=StrUpper(xx)
	s1=""
	s2=""
	indic=0
	; on calcule le nombre de champs
	GoSub parse1
	indent1=indent
	if( nb == 0 ) then
		gosub print_line
		continue
	endif
	sx=s%nb%
	
	if( s1 == "ENDSWITCH") then indent=iv
	if( s1 == "ENDWHILE") then indent=indent-1
	if( s1 == "END" && s2 == "SWITCH") then indent=iv
	if( s1 == "END" && s2 == "WHILE") then indent=indent-1
	if( s1 == "ENDIF") then indent=indent-1
	if( s1 == "NEXT") then indent=indent-1
	if( s1 == "ELSE") then indent=indent-1
	if( s1 == "#DEFINEFUNCTION") then
		indent=0
		FileWrite(new, strcat(@crlf,@crlf))
		indic=1
	endif
	if( s1 == "#ENDFUNCTION" || s1 == "#ENDSUBROUTINE") then
		fct=0
		indic =1
	endif
	if( s1 == "#DEFINESUBROUTINE") then
		indent=0
		FileWrite(new, strcat(@crlf,@crlf))
		indic=1
	endif
	if( s1 == "END" && s2 == "IF") then indent=indent-1
	if( indent1 != indent) then indent1=indent
	ij=1
	if( s1 == "IF") then
		for k=2 to nb-1
			if ( s%k%  == "THEN") then ij=0
		next
		if( s%nb%  == "THEN") then ij=1
		if( ij == 1) then indent=indent+1
	endif
	if( s1 == "SWITCH") then
		iv=indent
		indent=indent+1
	endif
	if( s1 == "SWITCH(") then
		iv=indent
		indent=indent+1
	endif
	if( s1 == "WHILE") then indent=indent+1
	if( s1 == "CASE") then indent=indent+1
	if( s1 == "FOR") then indent=indent+1
	
	gosub print_line
	if( s1 == "ELSE") then indent=indent+1
	if( s1 == "BREAK") then indent=indent-1
	if( indic == 1) then
		FileWrite(new, strcat(@crlf,@crlf))
	endif
	
endwhile
FileClose(new)
FileClose(old)
run("notepad.exe",output)
exit

; S O U S   P R O G R A M M E S
; -----------------------------

; Impression d'une ligne avec les tabulations souhaitées
; ------------------------------------------------------


:print_line
if(strsub(xx,1,1) == ":" ) then
	FileWrite(new, "")
	FileWrite(new, xx)
	return
endif
if( indent1 < 0) then indent1=0
if( indent1 > 19) then indent1=19
ch=strcat(tab%indent1%,xx)
FileWrite(new, ch)
return

;-------------------------------------------------
; Cette routine sort les champs d'une ligne,
; les champs étant délimités par un blanc
; ou une tabulation sans tenir compte des quotes.
;-------------------------------------------------


:parse1
nb=0
x=StrTrim(x)
x=StrReplace(x,"("," ") ; éviter if(xxxx
x=StrReplace(x,")"," ") ; éviter xxxx)then
while @TRUE
	fin = StrScan(x, r, 1, @FWDSCAN)
	If (fin == 0) then break
	nb = nb+1
	s%nb% = StrSub(x, 1, fin -  1)
	x=StrTrim(StrSub(x,fin,-1))
endwhile
nb=nb+1
s%nb%=x
; ne pas rater next; par exemple
; mais  ne pas indenter ;if par exemple
for i = 1 to nb
	if( strsub(s%i%,1,1) != ";") then
		s%i%=StrReplace(s%i%,";","")
	endif
next
return

;---------------------------------------------------------------
; Cette routine n'est pas utilisée car trop lente,
; mais elle rend correctement le nombre de champs
; en tenant compte des quotes ' " et ` reconnues par Winbatch ,
; ce qui n'est pas vraiment nécessaire pour l'indentation.
; Elle est lente car on travaille sur chaque caractère.
; (parseData est limité à neuf paramètres)
;---------------------------------------------------------------


:parse

nb=1
ind=0
l=strlen(x)
s1=""
for i=1 to l
	xy=strsub(x,i,1)
	if (xy == '"' || xy == "'" || xy == "`")then ind=ind+1
	
	if( ((ind mod 2) == 0) && ( xy == " "   || xy == @tab)) then
		nb=nb+1
		s%nb%=""
	else
		s%nb%=strcat(s%nb%,xy)
	endif
next
return

Article ID:   W16686
File Created: 2005:02:18:12:21:52
Last Updated: 2005:02:18:12:21:52