home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gif-util.zip / GIFINTO.C < prev    next >
C/C++ Source or Header  |  1989-08-01  |  4KB  |  137 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 0.1, Jul. 1989    *
  5. ******************************************************************************
  6. * Program to read stdin, and save it into the specified file iff the result  *
  7. * and inspired by the rle utah tool kit I decided to implement and add it.   *
  8. * -s minsize : the minimum file size to keep it.                 *
  9. * -h : on line help.                                 *
  10. ******************************************************************************
  11. * History:                                     *
  12. * 7 Jul 89 - Version 1.0 by Gershon Elber.                     *
  13. *****************************************************************************/
  14.  
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #include <alloc.h>
  21. #include <string.h>
  22. #include "gif_lib.h"
  23. #include "getarg.h"
  24.  
  25. #define PROGRAM_NAME    "GifInto"
  26. #define VERSION        "ß Version 1.0, "
  27.  
  28. #define DEFAULT_MIN_FILE_SIZE    14     /* More than GIF stamp + screen desc. */
  29. #define    DEFAULT_OUT_NAME    "GifInto.Gif"
  30.  
  31. extern unsigned int
  32.     _stklen = 16384;                  /* Increase default stack size */
  33.  
  34. static char
  35.     *VersionStr =
  36.     PROGRAM_NAME
  37.     "    IBMPC "
  38.     VERSION
  39.     "    Gershon Elber,    "
  40.     __DATE__ ",   " __TIME__ "\n"
  41.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  42. static char
  43.     *CtrlStr =
  44.     PROGRAM_NAME
  45.     " s%-MinFileSize!d h%- GifFile!*s";
  46. static char
  47.     *ProgramName;
  48. static int
  49.     MinFileSize = DEFAULT_MIN_FILE_SIZE;
  50.  
  51. /******************************************************************************
  52. * The is simply: read until EOF, then close the output, test its length, and  *
  53. * if non zero then rename it.                              *
  54. ******************************************************************************/
  55. void main(int argc, char **argv)
  56. {
  57.     int    Error, NumFiles,
  58.     MinSizeFlag = FALSE, HelpFlag = FALSE;
  59.     char **FileName = NULL, FoutTmpName[80], FullPath[80], DefaultName[80],
  60.     s[80], *p;
  61.     FILE *Fin, *Fout;
  62.  
  63.     if (strlen(ProgramName = argv[0]) == 0)            /* DOS 3.x only! */
  64.     ProgramName = PROGRAM_NAME;      /* Do something reasonable for 2.x */
  65.  
  66.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  67.         &MinSizeFlag, &MinFileSize, &HelpFlag,
  68.         &NumFiles, &FileName)) != FALSE ||
  69.         (NumFiles > 1 && !HelpFlag)) {
  70.     if (Error) GAPrintErrMsg(Error);
  71.     else
  72.     if (NumFiles != 1)
  73.         MESSAGE("Error in command line parsing - one GIF file please\n");
  74.     GAPrintHowTo(CtrlStr);
  75.     exit(1);
  76.     }
  77.  
  78.     if (HelpFlag) {
  79.     fprintf(stderr, VersionStr);
  80.     GAPrintHowTo(CtrlStr);
  81.     exit(0);
  82.     }
  83.  
  84.     /* Open the stdin in binary mode and increase its buffer size: */
  85.     setmode(0, O_BINARY);           /* Make sure it is in binary mode */
  86.     if ((Fin = fdopen(0, "rb")) == NULL)        /* Make it into a stream: */
  87.     EXIT("Failed to open input\n");
  88.     if (setvbuf(Fin, NULL, _IOFBF, FILE_BUFFER_SIZE))    /* Incr. stream buf */
  89.     EXIT("Failed to open input\n");
  90.  
  91.     /* Isolate the directory where our destination is, and set tmp file name */
  92.     /* in the very same directory.                         */
  93.     strcpy(FullPath, *FileName);
  94.     if ((p = strrchr(FullPath, '/')) != NULL ||
  95.     (p = strrchr(FullPath, '\\')) != NULL) p[1] = 0;
  96.     else
  97.     if ((p = strrchr(FullPath, ':')) != NULL) p[1] = 0;
  98.     else FullPath[0] = 0;           /* No directory or disk specified */
  99.  
  100.     strcpy(FoutTmpName, FullPath);    /* Generate destination temporary name */
  101.     strcat(FoutTmpName, tmpnam(NULL));
  102.  
  103.     if ((Fout = fopen(FoutTmpName, "wb")) == NULL)
  104.     EXIT("Failed to open output\n");
  105.     if (setvbuf(Fout, NULL, _IOFBF, FILE_BUFFER_SIZE))   /* Incr. stream buf */
  106.     EXIT("Failed to open output\n");
  107.  
  108.     while (!feof(Fin)) {
  109.     if (putc(getc(Fin), Fout) == EOF)
  110.         EXIT("Failed to write output\n");
  111.     }
  112.  
  113.     fclose(Fin);
  114.     if (ftell(Fout) >= (long) MinFileSize) {
  115.     fclose(Fout);
  116.     unlink(*FileName);
  117.     if (rename(FoutTmpName, *FileName) != 0) {
  118.         strcpy(DefaultName, FullPath);
  119.         strcat(DefaultName, DEFAULT_OUT_NAME);
  120.         if (rename(FoutTmpName, DefaultName) == 0) {
  121.         sprintf(s, "Failed to rename out file - left as %s\n",
  122.                                 DefaultName);
  123.         MESSAGE(s);
  124.         }
  125.         else {
  126.         unlink(FoutTmpName);
  127.         MESSAGE("Failed to rename out file - deleted\n");
  128.         }
  129.     }
  130.     }
  131.     else {
  132.     fclose(Fout);
  133.     unlink(FoutTmpName);
  134.     MESSAGE("File too small - not renamed\n");
  135.     }
  136. }
  137.