changeset 1280:75860b90034f

Rewrite in Bourne shell by Steve Robbins. Addition of -header and -body options, plus allowing of diff options.
author neelin <neelin>
date Tue, 12 Sep 2000 15:11:04 +0000
parents 643736f9e582
children ada508652f8b
files progs/mincdiff/mincdiff
diffstat 1 files changed, 70 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/progs/mincdiff/mincdiff
+++ b/progs/mincdiff/mincdiff
@@ -1,12 +1,16 @@
-#! /bin/csh -f
+#! /bin/sh
 #
 # Script to find differences between minc files
 #
-# Usage: mincdiff [-l] <file1.mnc> <file2.mnc>
+# Usage: mincdiff [-l] [diff options] <file1.mnc> <file2.mnc>
 #
 # Modifications: 
 #   $Log: mincdiff,v $
-#   Revision 6.1  1999-10-19 14:45:20  neelin
+#   Revision 6.2  2000-09-12 15:11:04  neelin
+#   Rewrite in Bourne shell by Steve Robbins. Addition of -header and -body
+#   options, plus allowing of diff options.
+#
+#   Revision 6.1  1999/10/19 14:45:20  neelin
 #   Fixed Log subsitutions for CVS
 #
 #   Revision 6.0  1997/09/12 13:23:46  neelin
@@ -37,6 +41,8 @@
 #   Some fixes.
 # 
 #
+# Copyright 2000 Steve Robbins, McConnell Brain Imaging Centre, 
+# Montreal Neurological Institute, McGill University.
 # Copyright 1993 Peter Neelin, McConnell Brain Imaging Centre, 
 # Montreal Neurological Institute, McGill University.
 # Permission to use, copy, modify, and distribute this
@@ -47,67 +53,74 @@
 # software for any purpose.  It is provided "as is" without
 # express or implied warranty.
 
-# Constants
-if (! $?TMPDIR) set TMPDIR = /usr/tmp
-set working_dir = $TMPDIR
+die () {
+    echo >&2 $@
+    exit 1
+}
 
-# Check arguments
-switch ("$#argv")
-case 2:
-   set file1 = $1
-   set file2 = $2
-   set cmp_switch = ""
-   breaksw
-case 3:
-   if ("$1" == "-l") then
-      set file1 = $2
-      set file2 = $3
-      set cmp_switch = -l
-      breaksw
-   endif
-default:
-   echo "Usage: $0 [-l] <file1.mnc> <file2.mnc>"
-   exit
-endsw
+header_diff=yes
+header_diff_opt=
+body_cmp=yes
+body_cmp_opt=
+while test $# -gt 2; do 
+    case $1 in
+	-header) header_diff=yes; body_cmp=no ;;
+	-body)   header_diff=no; body_cmp=yes ;;
+	-l) body_cmp_opt=-l ;;
+	-*) header_diff_opt="$header_diff_opt $1" ;;
+	*) break;
+    esac
+    shift
+done
+test $# -eq 2 || die "Usage: $0 [-header|-body] [-l] [diff options] <file1.mnc> <file2.mnc>"
 
-# Set up the file names
-set file1base = $file1:r
-set file2base = $file2:r
-set fileprefix = $working_dir/mincdiff-$$
-set file1tmp  = ${fileprefix}-1-${file1base:t}
-set file2tmp  = ${fileprefix}-2-${file2base:t}
-set file1tmpmnc = ${fileprefix}-minc-1-${file1base:t}
-set file2tmpmnc = ${fileprefix}-minc-2-${file1base:t}
+if test x$TMPDIR = x; then
+    for TMPDIR in /usr/tmp /var/tmp /tmp; do
+	test -d $TMPDIR && break;
+    done
+fi
+test -d $TMPDIR || die "TMPDIR $TMPDIR does not exist."
+
 
-# Set interrupt handling
-onintr cleanup
+# Files created
+tmp1="$TMPDIR/mincdiff-$$-tmp1"
+tmp2="$TMPDIR/mincdiff-$$-tmp2"
+header1="$TMPDIR/mincdiff-$$-header1"
+header2="$TMPDIR/mincdiff-$$-header2"
+body1="$TMPDIR/mincdiff-$$-body1"
+body2="$TMPDIR/mincdiff-$$-body2"
+
+# Clean up upon exit
+trap "/bin/rm -f $tmp1 $tmp2 $header1 $header2 $body1 $body2" 0 1 2 15
 
 # Expand the files
-set dumpfile1 = `mincexpand $file1 $file1tmp.mnc -name_only`
-set dumpfile2 = `mincexpand $file2 $file2tmp.mnc -name_only`
+dumpfile1=`mincexpand $1 $tmp1 -name_only`
+dumpfile2=`mincexpand $2 $tmp2 -name_only`
 
 # Dump the headers and compare them
-mincheader $dumpfile1 > $file1tmp.cdl && \
-   mincheader $dumpfile2 > $file2tmp.cdl
-if ($status) then
-   echo "${0}: Error reading file headers"
-   goto cleanup
-endif
-diff $file1tmp.cdl $file2tmp.cdl
-if ($status) then
-   echo "Binary image comparison:"
-endif
+if test $header_diff = yes; then
+    mincheader $dumpfile1 > $header1 || exit 1
+    mincheader $dumpfile2 > $header2 || exit 1
+    diff $header_diff_opt $header1 $header2
+    diff_status=$?
+else
+    diff_status=0
+fi
 
 # Dump the raw data and compare them
-mincextract -nonormalize -file $dumpfile1 > $file1tmp.raw && \
-      mincextract -nonormalize -file $dumpfile2 > $file2tmp.raw
-if ($status) then
-   echo "${0}: Error reading raw data"
-   goto cleanup
-endif
-cmp $cmp_switch $file1tmp.raw $file2tmp.raw
+if test $body_cmp = yes; then
+    echo "Binary image comparison:"
+    mincextract -nonormalize -file $dumpfile1 > $body1 \
+	|| die "Error extracting image data from $dumpfile1"
+    mincextract -nonormalize -file $dumpfile2 > $body2 \
+	|| die "Error extracting image data from $dumpfile2"
+    cmp $body_cmp_opt $body1 $body2 \
+	&& echo "Images are identical."
+    cmp_status=$?
+else
+    cmp_status=0
+fi
 
-cleanup:
-   rm -f {$file1tmp,$file2tmp}.{raw,cdl,mnc}
-   exit
-
+# Exit with SUCCESS if and only if the parts (header and/or body)
+# that we compared are identical.
+test $diff_status -eq 0 -a $cmp_status -eq 0