home *** CD-ROM | disk | FTP | other *** search
- /* --------------------------------- -------
- * |\ | | | | | |.| | \| |/ /|\ |||||||
- * | | | |/ | |\ |/ |/| |\ |/ | ? ---+--- =<
- * | | | | | | | | | | | \qqqqqqqqq/
- * --------------------------------- ~~~~~~~~~~~~~~~~
- * COMMON - Selects or Rejects lines common to two sorted files
- * Copyright (C) 1992 Torsten Poulin
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * The author can be contacted by s-mail at
- * Torsten Poulin
- * Banebrinken 99, 2, 77
- * DK-2400 Copenhagen NV
- * DENMARK
- *
- * $Log: Common.c,v $
- * Revision 37.5 93/02/11 21:36:03 Torsten
- * Now using dos.library/CheckSignal()
- * instead of exec.library/SetSignal().
- *
- * Revision 37.4 93/02/07 11:41:04 Torsten
- * Updated the copyright tag.
- *
- * Revision 37.3 93/02/05 11:23:31 Torsten
- * checked in with -k by Torsten at 93.02.05.11.24.04.
- *
- */
-
- #include <exec/types.h>
- #include <exec/libraries.h>
- #include <dos/dos.h>
- #include <dos/dostags.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <string.h>
- #include "common_rev.h"
-
- #define MAXLEN (512+1)
- #define TEMPLATE "FILE1/A,FILE2,-1/S,-2/S,-3/S"
- #define OPT_FILE1 0
- #define OPT_FILE2 1
- #define OPT_NOT1 2
- #define OPT_NOT2 3
- #define OPT_NOT3 4
-
- char const versionID[] = VERSTAG;
- char const copyright[] = "$COPYRIGHT:© 1990,1992,1993 Torsten Poulin$";
-
- LONG Common(struct DosLibrary *DOSBase,
- BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3);
-
-
- LONG entrypoint(VOID)
- {
- struct RDArgs *args;
- struct DosLibrary *DOSBase;
- struct Library *SysBase;
- LONG arg[5];
- LONG rc = RETURN_OK;
- BPTR f1, f2;
- UBYTE *name1, *name2;
- BOOL print1, print2, print3;
-
- SysBase = *(struct Library **) 4L;
- if(!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
- return RETURN_FAIL;
-
- arg[OPT_FILE1] = arg[OPT_FILE2] = 0L;
- arg[OPT_NOT1] = arg[OPT_NOT2] = arg[OPT_NOT3] = 0L;
-
- if(args = ReadArgs(TEMPLATE, arg, NULL))
- {
- name1 = (UBYTE *) arg[OPT_FILE1];
- name2 = (UBYTE *) arg[OPT_FILE2];
-
- print1 = ! (BOOL) arg[OPT_NOT1];
- print2 = ! (BOOL) arg[OPT_NOT2];
- print3 = ! (BOOL) arg[OPT_NOT3];
-
- if(!(f1 = Open(name1, MODE_OLDFILE)))
- {
- LONG err = IoErr();
- PrintFault(err, name1);
- rc = RETURN_ERROR;
- }
-
- if(!arg[OPT_FILE2])
- f2 = Input();
- else if(!(f2 = Open(name2, MODE_OLDFILE)))
- {
- LONG err = IoErr();
- PrintFault(err, name2);
- Close(f1);
- rc = RETURN_ERROR;
- }
-
- if(f1 && f2)
- {
- rc = Common(DOSBase, f1, f2, print1, print2, print3);
- Close(f1);
- if(arg[OPT_FILE2])
- Close(f2);
- }
- FreeArgs(args);
- }
- else
- {
- LONG err = IoErr();
- PrintFault(err, "Common");
- rc = RETURN_ERROR;
- }
- CloseLibrary((struct Library *) DOSBase);
- return rc;
- }
-
-
- LONG Common(struct DosLibrary *DOSBase,
- BPTR f1, BPTR f2, BOOL print1, BOOL print2, BOOL print3)
- {
- UBYTE *stat1, *stat2;
- UBYTE line1[MAXLEN], line2[MAXLEN];
- LONG cmp_result;
- LONG err;
- BPTR out = Output();
-
- stat1 = FGets(f1, line1, MAXLEN);
- stat2 = FGets(f2, line2, MAXLEN);
-
- while(stat1 != 0 && stat2 != 0)
- {
- cmp_result = strcmp(line1, line2);
- if(cmp_result == 0)
- {
- if(print3)
- {
- if(print1) FPutC(out, '\t');
- if(print2) FPutC(out, '\t');
- PutStr(line1);
- }
- stat1 = FGets(f1, line1, MAXLEN);
- stat2 = FGets(f2, line2, MAXLEN);
- }
- else if(cmp_result < 0)
- {
- if(print1)
- PutStr(line1);
- stat1 = FGets(f1, line1, MAXLEN);
- }
- else
- {
- if(print2)
- {
- if(print1) FPutC(out, '\t');
- PutStr(line2);
- }
- stat2 = FGets(f2, line2, MAXLEN);
- }
-
- if (CheckSignal(SIGBREAKF_CTRL_C))
- {
- PrintFault(ERROR_BREAK, NULL);
- return RETURN_WARN;
- }
- }
- if(err = IoErr())
- {
- PrintFault(err, "Common");
- return RETURN_ERROR;
- }
-
- /* Output the remainder of the longest file */
- if(stat1 || stat2)
- {
- if(print1 && stat1)
- PutStr(line1);
- if(print2 && stat2)
- {
- if(print1)
- FPutC(out, '\t');
- PutStr(line2);
- }
- while(FGets(stat1 ? f1 : f2, line1, MAXLEN))
- {
- if(print1 && print2 && stat2)
- FPutC(out, '\t');
- if(stat1 ? print1 : print2)
- PutStr(line1);
-
- if (CheckSignal(SIGBREAKF_CTRL_C))
- {
- PrintFault(ERROR_BREAK, NULL);
- return RETURN_WARN;
- }
- }
- if(err = IoErr())
- {
- PrintFault(err, "Common");
- return RETURN_ERROR;
- }
- }
- }
-