home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / giflib11 / util / gifinto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-06  |  5.0 KB  |  159 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                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. * 22 Dec 89 - Fix problem with tmpnam (Version 1.1).                         *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <io.h>
  18. #include <stdlib.h>
  19. #include <alloc.h>
  20. #endif /* __MSDOS__ */
  21.  
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "gif_lib.h"
  27. #include "getarg.h"
  28.  
  29. #define PROGRAM_NAME    "GifInto"
  30.  
  31. #define DEFAULT_MIN_FILE_SIZE    14     /* More than GIF stamp + screen desc. */
  32. #define    DEFAULT_OUT_NAME    "GifInto.Gif"
  33. #define DEFAULT_TMP_NAME    "TempInto.$$$"
  34.  
  35. #ifdef __MSDOS__
  36. extern unsigned int
  37.     _stklen = 16384;                 /* Increase default stack size. */
  38. #endif /* __MSDOS__ */
  39.  
  40. #ifdef SYSV
  41. static char *VersionStr =
  42.         "Gif library module,\t\tGershon Elber\n\
  43.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  44. static char
  45.     *CtrlStr = "GifInto s%-MinFileSize!d h%- GifFile!*s";
  46. #else
  47. static char
  48.     *VersionStr =
  49.     PROGRAM_NAME
  50.     GIF_LIB_VERSION
  51.     "    Gershon Elber,    "
  52.     __DATE__ ",   " __TIME__ "\n"
  53.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  54. static char
  55.     *CtrlStr =
  56.     PROGRAM_NAME
  57.     " s%-MinFileSize!d h%- GifFile!*s";
  58. #endif /* SYSV */
  59.  
  60. static int
  61.     MinFileSize = DEFAULT_MIN_FILE_SIZE;
  62.  
  63. /******************************************************************************
  64. * The is simply: read until EOF, then close the output, test its length, and  *
  65. * if non zero then rename it.                              *
  66. ******************************************************************************/
  67. void main(int argc, char **argv)
  68. {
  69.     int    Error, NumFiles,
  70.     MinSizeFlag = FALSE, HelpFlag = FALSE;
  71.     char **FileName = NULL,
  72.         TmpName[80], FoutTmpName[80], FullPath[80], DefaultName[80], s[80], *p;
  73.     FILE *Fin, *Fout;
  74.  
  75.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  76.         &MinSizeFlag, &MinFileSize, &HelpFlag,
  77.         &NumFiles, &FileName)) != FALSE ||
  78.         (NumFiles > 1 && !HelpFlag)) {
  79.     if (Error)
  80.         GAPrintErrMsg(Error);
  81.     else if (NumFiles != 1)
  82.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  83.     GAPrintHowTo(CtrlStr);
  84.     exit(1);
  85.     }
  86.  
  87.     if (HelpFlag) {
  88.     fprintf(stderr, VersionStr);
  89.     GAPrintHowTo(CtrlStr);
  90.     exit(0);
  91.     }
  92.  
  93.     /* Open the stdin in binary mode and increase its buffer size: */
  94. #ifdef __MSDOS__
  95.     setmode(0, O_BINARY);          /* Make sure it is in binary mode. */
  96.     if ((Fin = fdopen(0, "rb")) == NULL ||       /* Make it into a stream: */
  97.         setvbuf(Fin, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE))/* Incr. stream buf.*/
  98. #else
  99.     if ((Fin = fdopen(0, "r")) == NULL)        /* Make it into a stream: */
  100. #endif /* __MSDOS__ */
  101.     GIF_EXIT("Failed to open input.");
  102.  
  103.     /* Isolate the directory where our destination is, and set tmp file name */
  104.     /* in the very same directory.                         */
  105.     strcpy(FullPath, *FileName);
  106.     if ((p = strrchr(FullPath, '/')) != NULL ||
  107.     (p = strrchr(FullPath, '\\')) != NULL)
  108.     p[1] = 0;
  109.     else if ((p = strrchr(FullPath, ':')) != NULL)
  110.     p[1] = 0;
  111.     else
  112.     FullPath[0] = 0;          /* No directory or disk specified. */
  113.  
  114.     strcpy(FoutTmpName, FullPath);   /* Generate destination temporary name. */
  115.     /* Make sure the temporary is made in the current directory: */
  116.     p = tmpnam(TmpName);
  117.     if (strrchr(p, '/')) p = strrchr(p, '/') + 1;
  118.     if (strrchr(p, '\\')) p = strrchr(p, '\\') + 1;
  119.     if (strlen(p) == 0) p = DEFAULT_TMP_NAME;
  120.     strcat(FoutTmpName, p);
  121.  
  122. #ifdef __MSDOS__
  123.     if ((Fout = fopen(FoutTmpName, "wb")) == NULL ||
  124.     setvbuf(Fout, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE))/*Incr. stream buf.*/
  125. #else
  126.     if ((Fout = fopen(FoutTmpName, "w")) == NULL)
  127. #endif /* __MSDOS__ */
  128.     GIF_EXIT("Failed to open output.");
  129.  
  130.     while (!feof(Fin)) {
  131.     if (putc(getc(Fin), Fout) == EOF)
  132.         GIF_EXIT("Failed to write output.");
  133.     }
  134.  
  135.     fclose(Fin);
  136.     if (ftell(Fout) >= (long) MinFileSize) {
  137.     fclose(Fout);
  138.     unlink(*FileName);
  139.     if (rename(FoutTmpName, *FileName) != 0) {
  140.         strcpy(DefaultName, FullPath);
  141.         strcat(DefaultName, DEFAULT_OUT_NAME);
  142.         if (rename(FoutTmpName, DefaultName) == 0) {
  143.         sprintf(s, "Failed to rename out file - left as %s.",
  144.                                 DefaultName);
  145.         GIF_MESSAGE(s);
  146.         }
  147.         else {
  148.         unlink(FoutTmpName);
  149.         GIF_MESSAGE("Failed to rename out file - deleted.");
  150.         }
  151.     }
  152.     }
  153.     else {
  154.     fclose(Fout);
  155.     unlink(FoutTmpName);
  156.     GIF_MESSAGE("File too small - not renamed.");
  157.     }
  158. }
  159.