home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / TO_PORT / compfacelib.lzh / cmain.c < prev    next >
C/C++ Source or Header  |  1992-04-30  |  3KB  |  163 lines

  1.  
  2. /*  @(#)cmain.c 1.5 91/04/15
  3.  *
  4.  *  Compface - 48x48x1 image compression.
  5.  *
  6.  *  Copyright (c) James Ashton - Sydney University - June 1990.
  7.  *
  8.  *  Written 11th November 1889.
  9.  *
  10.  *  Permission is given to distribute these sources, as long as the
  11.  *  copyright messages are not removed, and no monies are exchanged. 
  12.  *
  13.  *  No responsibility is taken for any errors on inaccuracies inherent
  14.  *  either to the comments or the code of this program, but if reported
  15.  *  to me, then an attempt will be made to fix them.
  16.  */
  17.  
  18. #include <fcntl.h>
  19.  
  20. /* the buffer is longer than needed to handle sparse input formats */
  21. #define FACEBUFLEN 2048
  22. char fbuf[FACEBUFLEN];
  23.  
  24. /* IO file descriptors and their names */
  25. int infile    = 0;
  26. char *inname  = "<stdin>";
  27. int outfile   = 1;
  28. char *outname = "<stdout>";
  29.  
  30. /* basename of executable */
  31. char *cmdname;
  32.  
  33. /* error handling definitions follow */
  34.  
  35. extern int errno, sys_nerr;
  36. extern char *sys_errlist[];
  37.  
  38. void exit();
  39. char *strcat(), *strcpy();
  40.  
  41. #define ERR ((errno < sys_nerr) ? sys_errlist[errno] : "")
  42. #define INITERR(s) {(void)strcpy(fbuf, cmdname); (void)strcat(fbuf, ": ");\
  43.                     (void)strcat(fbuf, (s));}
  44. #define ADDERR(s) (void)strcat(fbuf, (s));
  45. #define ERROR {(void)strcat(fbuf, "\n");\
  46.                 (void)write(2, fbuf, strlen(fbuf)); exit(1);}
  47. #define INITWARN(s) {(void)strcpy(fbuf, cmdname);\
  48.                     (void)strcat(fbuf, ": (warning) ");\
  49.                     (void)strcat(fbuf, (s));}
  50. #define ADDWARN(s) (void)strcat(fbuf, (s));
  51. #define WARN {(void)strcat(fbuf, "\n"); (void)write(2, fbuf, strlen(fbuf));}
  52.  
  53. main(argc, argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.   cmdname = *argv;
  58.   while (**argv)
  59.     if (*((*argv)++) == '/')
  60.       cmdname = *argv;               /* find the command's basename */
  61.  
  62.   if (argc > 3)
  63.     {
  64.       INITERR("usage: ")
  65.       ADDERR(cmdname)
  66.       ADDERR(" [infile [outfile]]")
  67.       ERROR
  68.     }
  69.  
  70.   if ((argc > 1) && strcmp(*++argv, "-"))
  71.     {
  72.       inname = *argv;
  73.       if ((infile = open(inname, O_RDONLY)) == -1)
  74.         {
  75.           INITERR(inname)
  76.           ADDERR(": ")
  77.           ADDERR(ERR)
  78.           ERROR
  79.         }
  80.     }
  81.  
  82.   if (argc > 2)
  83.     {
  84.       outname = *++argv;
  85.       if ((outfile = open(outname, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
  86.         {
  87.           INITERR(outname)
  88.           ADDERR(": ")
  89.           ADDERR(ERR)
  90.           ERROR
  91.         }
  92.     }
  93.  
  94.   (void) ReadBuf();
  95.   switch (compface(fbuf))
  96.     {
  97.       case -2 : INITERR("internal error")
  98.                 ERROR
  99.       case -1 : INITERR(inname)
  100.                 ADDERR(": insufficient or invalid data")
  101.                 ERROR
  102.       case  1 : INITWARN(inname)
  103.                 ADDWARN(": excess data ignored")
  104.                 WARN
  105.       default : ;
  106.     }
  107.   (void) WriteBuf();
  108.   exit(0);
  109. /*NOTREACHED*/
  110. }
  111.  
  112.  
  113. WriteBuf()
  114. {
  115.     register char *s, *t;
  116.     register int len;
  117.  
  118.     s = fbuf;
  119.     t = s + strlen(s);
  120.     while (s < t)
  121.     {
  122.         if ((len = write(outfile, s, t - s)) == -1)
  123.         {
  124.             INITERR(outname)
  125.             ADDERR(": ")
  126.             ADDERR(ERR)
  127.             ERROR
  128.         }
  129.         s += len;
  130.     }
  131.     return 0;
  132. }
  133.  
  134.  
  135. ReadBuf()
  136. {
  137.     register int count, len;
  138.     register char *t;
  139.  
  140.     count = 0;
  141.     t = fbuf;
  142.     while (len = read(infile, t, FACEBUFLEN - count))
  143.     {
  144.         if (len == -1)
  145.         {
  146.             INITERR(inname)
  147.             ADDERR(": ")
  148.             ADDERR(ERR)
  149.             ERROR
  150.         }
  151.         t += len;
  152.         if ((count += len) >= FACEBUFLEN)
  153.         {
  154.             INITWARN(inname)
  155.             ADDWARN(" exceeds internal buffer size.  Data may be lost")
  156.             WARN
  157.             break;
  158.         }
  159.     }
  160.     *t = '\0';
  161.     return count;
  162. }
  163.