; XCOMP
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; Compare a group of file recursively between two directory trees.
; Implicit parameters: input and output NULL
;!proteus -z
IF OR(ISEMPTY(ARGV(5)), ISEMPTY(ARGV(6)))
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) \
" \"specs\" \"dir_root\""
CONSOLELN ""
CONSOLELN "Purpose: compare recursively (including subdirectories)"
CONSOLELN "all the files corresponding to \"specs\" starting " \
"from \"dir_root\"."
ABORT 0
FI
!ifdef UNIX
DirSep = "/"
!else
DirSep = "\\"
!endif
SourceDir = STRIPQUOTES(ARGV(5))
DestDir = STRIPQUOTES(ARGV(6))
IF STRNEQ(RIGHT(DestDir, 1), DirSep)
DestDir = DestDir DirSep
FI
X = STRRSTR(SourceDir, DirSep)
IF X
BaseDir = LEFT(SourceDir, X)
Spec = RESTFROM(SourceDir, INC(X))
ELSE
BaseDir = ""
Spec = SourceDir
FI
TotFound = 0
NotComp = 0
DifSize = 0
Success = 0
Different = 0
RecursiveCompare("")
CONSOLELN "---------------"
CONSOLELN "Results summary"
CONSOLELN "---------------"
CONSOLELN "Files found: " TotFound
IF NotComp
CONSOLELN "Files that could not be opened: " NotComp
FI
IF DifSize
CONSOLELN "Files of different sizes: " DifSize
FI
IF Different
CONSOLELN "Files with differences: " Different
FI
CONSOLELN IIF(NEQ(TotFound, Success), \
"Error: not all files were equal.", \
"Compare succesfull.")
ABORT 0
FUNCTION CompareFiles(source, dest)
hs = FOPEN(source, 1)
IF EQ(hs, -1)
CONSOLELN "Could not open " source
RETURN -1
FI
hd = FOPEN(dest, 1)
IF EQ(hd, -1)
FCLOSE(hs)
CONSOLELN "Could not open " dest
RETURN -1
FI
IF NEQ(FSIZE(hs), FSIZE(hd))
FCLOSE(hs)
FCLOSE(hd)
CONSOLELN "File sizes do not match (" \
source ", " dest ")"
RETURN -2
FI
diff = 0
REPEAT
ss = FREAD(hs, 8192)
sd = FREAD(hd, 8192)
dp = POSDIFF(ss, sd)
WHILE dp
INC(@diff)
INC(@dp)
ss = RESTFROM(ss, dp)
sd = RESTFROM(sd, dp)
dp = POSDIFF(ss, sd)
LOOP
UNTIL NEQ(STRLEN(ss), 8192)
IF diff
CONSOLELN diff " differences found between " source " and " dest
FI
FCLOSE(hs)
FCLOSE(hd)
RETURN diff
FUNCTION RecursiveCompare(extdir)
h = DIROPEN(_BaseDir extdir _Spec, 3)
f = IIF(NEQ(h, -1), 1, 0)
WHILE GT(f, 0)
; File found
IF NAND(DIRLAST(h, 5), 16)
; Directory - enter...
CONSOLELN "Entering directory " DIRLAST(h, 1) "..."
RecursiveCompare(extdir DIRLAST(h, 1) _DirSep)
ELSE
; File - compare...
CONSOLELN "Comparing file " DIRLAST(h, 1) "..."
SWITCH CompareFiles(_BaseDir extdir DIRLAST(h, 1), \
_DestDir extdir DIRLAST(h, 1))
ON -1
_NotComp = INC(_NotComp)
ON -2
_DifSize = INC(_DifSize)
ON 0
_Success = INC(_Success)
OTHER
_Different = INC(_Different)
OFF
_TotFound = INC(_TotFound)
FI
f = DIRNEXT(h)
LOOP
DIRCLOSE(h)
RETURN 0