Back to index

radiance  4R0+20100331
psort.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: psort.c,v 1.3 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *   Program to sort a meta-file
00006  *
00007  *   cc psort.c sort.o palloc.o mfio.o syscalls.o misc.o
00008  */
00009 
00010 
00011 #define  MAXALLOC  1100            /* must be >= PBSIZE in sort.c */
00012 
00013 
00014 #include  "meta.h"
00015 
00016 
00017 char  *progname;
00018 
00019 int  maxalloc = MAXALLOC;
00020 
00021 static int  val[5], extrema[5];
00022 
00023 
00024 static int pcompare(PRIMITIVE  **pp1, PRIMITIVE  **pp2);
00025 
00026 
00027 int
00028 main(
00029        int  argc,
00030        char  **argv
00031 )
00032 {
00033  FILE  *fp;
00034  int  i;
00035 
00036  progname = *argv++;
00037  argc--;
00038 
00039  for (i = 0; i < 4 && argc && (**argv == '+' || **argv == '-'); i++)  {
00040     val[i] = (**argv == '+') ? 1 : -1;
00041     switch (*(*argv+1))  {
00042        case 'x':
00043          extrema[i] = XMN;
00044          break;
00045        case 'y':
00046          extrema[i] = YMN;
00047          break;
00048        case 'X':
00049          extrema[i] = XMX;
00050          break;
00051        case 'Y':
00052          extrema[i] = YMX;
00053          break;
00054        default:
00055           sprintf(errmsg, "unknown option \"%s\"", *argv);
00056          error(USER, errmsg);
00057          break;
00058        }
00059     argv++;
00060     argc--;
00061     }
00062 
00063  val[i] = 0;
00064 
00065  if (argc)
00066     while (argc)  {
00067        fp = efopen(*argv, "r");
00068        sort(fp, pcompare);
00069        fclose(fp);
00070        argv++;
00071        argc--;
00072        }
00073  else
00074     sort(stdin, pcompare);
00075 
00076  writeof(stdout);
00077 
00078  return(0);
00079  }
00080 
00081 
00082 
00083 
00084 int
00085 pcompare(
00086        PRIMITIVE  **pp1,
00087        PRIMITIVE  **pp2
00088 )
00089 {
00090  register PRIMITIVE  *p1 = *pp1, *p2 = *pp2;
00091  register int  i;
00092 
00093  for (i = 0; val[i]; i++)
00094     if (p1->xy[extrema[i]] > p2->xy[extrema[i]])
00095        return(val[i]);
00096     else if (p1->xy[extrema[i]] < p2->xy[extrema[i]])
00097        return(-val[i]);
00098 
00099  return(0);
00100  }