;
; Author: Antonio SERRANO
; and LOMAS
; Program: COMPR.KIX
; Rev. 00. -- Date: 07/08/1999; Description:
; ------------
; When we put the scripts in production, sometimes we'll desire to reduce its size.
; I comment a lot my scripts but when the directors and other people want to connect via RAS
; they found the login script window is to slowly to charge the scripts.
; If we reduce the script size we'll improve (a little bit) its speed (however, it's better
; to improve the algorithm).
; How we can reduce the script file size?.
; -- No comments.
; -- No superfluous spaces.
; -- No ENTERs?. (Optional)
; We can write a PARSEC to eliminate the superfluous code of a KiXtart script.
; This script does that and IT'S READY TO USE.
; I hope you find this script useful.
;
; Characteristics:
; -- It changes the TAB by four spaces.
; -- It drops the comments between ';' until LF.
; -- It drops the spaces, on the left between LF to find a non-space character.
; -- It drops the initial spaces.
; -- It drops the isolates LF.
; -- It shows two different progress indicators.
; -- You can find a routine to find the file size.
;
;
; Variables
;
;$FileIN is an argument
;$FileOUT is optional
$LF=CHR(13)+CHR(10) ; Line feed
; Show or not the compacted file
;$ShowFile=0 ;
;$ShowFile=1 ; Show it ... ...
IF 1=$ShowFile
ELSE $ShowFile=0
ENDIF
$Output="" ; Output string null
$NLineIN=0 ; Number of lines into the original file
$NLineOUT=0 ; Number of lines into the compacted file
$NTABSpaces=" " ; Number of spaces by TAB
;
; Check the parameters
;
IF $FileIN ; Check if the input file exist
IF EXIST($FileIN)
$FileIN=UCASE($FileIN)
GOTO CHECKEXT
ELSE
?"$FileIN doesn't exist"
GOTO USAGE
ENDIF
ELSE
GOTO USAGE
ENDIF
; Get the input file extension to check if it is a KiXtart file
:CHECKEXT
$ext=UCASE(SUBSTR($FileIN, LEN($FileIN)-2, 3))
IF "KIX"<>$ext AND "SCR"<>$ext
FLUSHKB ; Flush any user keystroke
?"The file you want to compact doesn't have a Kixtart extension"
?"If you choose to continue perhaps the results should be undesirable"
?"Do you want to continue? [Y/N]"
DO
GET $op
$op=UCASE($op)
UNTIL "Y"=$op OR "N"=$op
IF "N"=$op GOTO BYE
ENDIF
ENDIF
; Check the output file
:CHECKOUT
IF $FileOUT
IF EXIST($FileOUT)
; Get the response
FLUSHKB ; Flush any user keystroke
?"The output file exists. Do you want to overwrite it? [Y/N] "
DO
GET $op
$op=UCASE($op)
UNTIL "Y"=$op OR "N"=$op
"$op" ? ; Show the answer
IF "Y"=$op ; Drop the old file
"Please wait ..." ?
DEL $FileOUT
GOTO BEGIN
ELSE
GOTO BYE
ENDIF
ELSE
GOTO BEGIN
ENDIF
ELSE ; Search the last '.' and add 'CMP' extension
$FileOUT=UCASE(SUBSTR($FileIN,1,LEN($FileIN)-3))+"CMP"
GOTO CHECKOUT
ENDIF
;
; Usage
;
:USAGE
COLOR g+/n
?"Script to compact KiXtart Scripts"
COLOR n/n
?"USAGE: KIX32 COMPR "+CHR(36)+"FileIN="+CHR(34)+"<FileNameIN>"+CHR(34)+" ["+CHR(36)+"ShowFile=<1|0>]"
?" KIX32 COMPR "+CHR(36)+"FileIN="+CHR(34)+"<FileNameIN>"+CHR(34)+" ["+CHR(36)+"FileOUT="+chr(34)+"<FileNameOUT>"+CHR(34)+"]$LF ["+CHR(36)+"ShowFile=<1|0>]"
GOTO BYE
;
; Open the files and check for errors
;
:BEGIN
IF OPEN(9, $FileOUT, 5)
?"Error opening $FileOUT"
GOTO BYE
ENDIF
IF OPEN(8, $FileIN)
?"Error opening $FileIN"
GOTO BYE
ENDIF
;
; PARSEC
;
; Preprocessing a line
:INPUT
IF $ShowFile ? ENDIF
;
; Writing compacted line
IF $Output
$NLineOUT=$NLineOUT+1
$Aux=WRITELINE(9, $Output+CHR(13)+CHR(10))
ENDIF
; Initialize vars.
$Output="" ; Output string null
$Input=READLINE(8) ; Read new line
; Check condition end of script
IF @ERROR GOTO END
ENDIF
$LInput=LEN($Input) ; Length of line to calculate the end
$NLineIN=$NLineIN+1 ; Line being processed now
; Show the compressed line or an indicator
IF $ShowFile "Line $NLineIN:"
ELSE
$Aux=$NLineIN/10
IF $Aux*10=$NLineIN
"."
IF ($Aux/80)*80=$Aux ? ENDIF
ENDIF
ENDIF
$i=0
;
; STATE (1)
;
:S1
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input, $i, 1)
SELECT
CASE CHR(32)=$CharIN ; " "
GOTO S1
CASE CHR(9)=$CharIN ; TAB
GOTO S1
CASE CHR(59)=$CharIN ; ";"
GOTO INPUT
CASE CHR(34)=$CharIN ; """
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S2
CASE CHR(39)=$CharIN ; "'"
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE 1 ; Other character
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S4
ENDSELECT
;
; STATE (2)
;
:S2
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input, $i, 1)
SELECT
CASE CHR(34)=$CharIN ; """
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S1
CASE CHR(9)=$CharIN ; TAB
$Output=$Output+$NTABSpaces
IF $ShowFile $NTABSpaces ENDIF
GOTO S2
CASE 1 ; Other character
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S2
ENDSELECT
;
; STATE (3)
;
:S3
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input, $i, 1)
SELECT
CASE CHR(39)=$CharIN ; "'"
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S1
CASE CHR(9)=$CharIN ; TAB
$Output=$Output+$NTABSpaces
IF $ShowFile $NTABSpaces ENDIF
GOTO S3
CASE 1 ; Other character
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S3
ENDSELECT
;
; STATE (4)
;
:S4
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input, $i, 1)
SELECT
CASE CHR(32)=$CharIN ; " "
$CharINAux=SUBSTR($Input, $i+1, 1)
IF CHR(32)<>$CharINAux AND CHR(9)<>$CharINAux AND CHR(59)<>$CharINAux AND $CharINAux
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
ENDIF
GOTO S5
CASE CHR(9)=$CharIN ; TAB
$CharINAux=SUBSTR($Input, $i+1, 1)
IF CHR(32)<>$CharINAux AND CHR(9)<>$CharINAux AND CHR(59)<>$CharINAux AND $CharINAux
$Output=$Output+$NTABSpaces
IF $ShowFile $NTABSpaces ENDIF
ENDIF
GOTO S5
CASE CHR(34)=$CharIN ; """
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S2
CASE CHR(39)=$CharIN ; "'"
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE 1 ; Other character
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S4
ENDSELECT
;
; STATE (5)
;
:S5
$i=$i+1 ; Counter character in each line
$CharIN=SUBSTR($Input, $i, 1)
SELECT
CASE CHR(32)=$CharIN OR CHR(9)=$CharIN ; " " or TAB
GOTO S5
CASE CHR(34)=$CharIN ; """
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S2
CASE CHR(39)=$CharIN ; "'"
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S3
CASE $LInput<$i ; NEWLINE
GOTO INPUT
CASE CHR(59)=$CharIN ; ";"
GOTO INPUT
CASE 1 ; Other character
$Output=$Output+$CharIN
IF $ShowFile $CharIN ENDIF
GOTO S4
ENDSELECT
;
; End the script
;
:END
; Close the open files
$Aux=CLOSE(9)
$Aux=CLOSE(8)
; Calculate the $FileIN file size
$File=$FileIN
GOSUB SIZEFILE
$SizeFileIN=$Size
; Calculate the $FileOUT file size
$File=$FileOUT
GOSUB SIZEFILE
$SizeFileOUT=$Size
?
?"********************************** STATISTICS **********************************"
?" $FileIN lines number: $NlineIN"
?" $FileOUT lines number: $NlineOUT"
?" $FileIN size: $SizeFileIN"
?" $FileOUT size: $SizeFileOUT" ?
?"********************************************************************************"
:BYE
?"End of script"
?"Have a nice day!" ? ?
EXIT 1
;
; Subroutines
;
; Subroutine to get the file sizes (in bytes).
; Usage: $File: Input file.
; $Size: Size of $File.
:SIZEFILE
$FileAux="%TMP%\DIR.TMP" ; Tmp file to save a DIR output
SHELL "%COMSPEC% /C DIR $File > $FileAux"
IF 0=OPEN(7,$FileAux)
; We get the last line
$i=0
WHILE 7>$i
$line=READLINE(7)
$i=$i+1
LOOP
; We get the last character
$l=LEN($line)
$times=1
; We get the substring file size begin
WHILE 2>=$times
$l=$l-1
$v=SUBSTR($line,$l,1)
IF CHR(32)=$v $Times=$Times+1 ENDIF
LOOP
; Get the file size string
$Size=SUBSTR($line,$l+1,LEN($line))
$Aux=CLOSE(7) ;
DEL $FileAux ; Delete the $FileAux file
ELSE
?"Trouble to open the tmp file."
ENDIF
RETURN