; REMVB
;
; Program for Proteus
;
; (C) 2000-2004 Simone Zanella Productions
;
; Remove remarks from the specified Visual Basic files.
;
; Algorithm:
; - remove all lines starting with ' and remarks at the end of line
#!proteus -z
SourceDir = STRIPQUOTES(ARGV(5))
DestDir = STRIPQUOTES(ARGV(6))
IF OR(ISEMPTY(SourceDir), ISEMPTY(DestDir))
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " [\"filespec\" \"destination\"]"
CONSOLELN "Purpose: remove remarks from specified VB source files; new files are created"
CONSOLELN "inside destination, which must be an existing folder."
ABORT 0
FI
; Determine directory separator according to the operating system
!ifdef UNIX
DirSep = "/"
!else
DirSep = "\\"
!endif
; Isolate base directory and file specification
X = STRRSTR(SourceDir, DirSep)
IF X
BaseDir = LEFT(SourceDir, X)
Spec = RESTFROM(SourceDir, INC(X))
ELSE
BaseDir = ""
Spec = SourceDir
FI
; Build an AVL tree of the files to be processed
H = DIROPEN(SourceDir, 1)
F = IIF(NEQ(H, -1), 1, 0)
A = AVLNEW()
WHILE GT(F, 0)
AVLSET(A, DIRLAST(H, 1), 0)
F = DIRNEXT(H)
LOOP
DIRCLOSE(H)
; Transform the tree into an array.
; No sorting is required, keys are already ordered.
VF = AVLKEYS(A)
AVLFREE(A)
IF STRNEQ(RIGHT(DestDir, 1), DirSep)
DestDir = DestDir DirSep
FI
; Process all files
FOR X = 1 TO VECLEN(VF)
IF AllowedExt(VECGET(VF, X))
H = FOPEN(BaseDir IIF(ISNOTEMPTY(BaseDir), DirSep, "") \
VECGET(VF, X), 1)
IF EQ(H, -1)
CONSOLELN "Error: could not open " VECGET(VF, X)
CONTINUE
FI
D = FOPEN(DestDir VECGET(VF, X), 28)
IF EQ(D, -1)
CONSOLELN "Error: could not create \"" DestDir VECGET(VF, X) "\""
ELSE
CONSOLELN "Converting " VECGET(VF, X)
WHILE NOT(FEOF(H))
L = FREADLN(H)
IF STRNEQ(LEFT(LTRIM(L, " "), 1), "'")
FWRITELN(D, CheckLine(L))
FI
LOOP
FCLOSE(D)
FI
FCLOSE(H)
ELSE
CONSOLELN "Copying " VECGET(VF, X)
FCOPY(BaseDir IIF(ISNOTEMPTY(BaseDir), DirSep, "") \
VECGET(VF, X), DestDir VECGET(VF, X))
FI
NEXT
ABORT 0
FUNCTION CheckLine(l)
instr = 0
n = STRLEN(l)
FOR x = 1 TO n
SWITCH SUBSTR(l, x, 1) STREQ
ON "\""
instr = IIF(instr, 0, 1)
ON "'"
IF NOT(instr)
RETURN LEFT(l, DEC(x))
FI
OFF
NEXT
RETURN l
FUNCTION AllowedExt(s)
p = STRRSTR(s, ".")
ext = RESTFROM(s, INC(p))
IF OR(STRIEQ(ext, "FRM"), STRIEQ(ext, "BAS"))
RETURN 1
FI
RETURN 0