home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / jpegv6 / c / cdjpeg < prev    next >
Encoding:
Text File  |  1995-10-08  |  4.9 KB  |  186 lines

  1. /*
  2.  * cdjpeg.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains common support routines used by the IJG application
  9.  * programs (cjpeg, djpeg, jpegtran).
  10.  */
  11.  
  12. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  13. #include <ctype.h>        /* to declare isupper(), tolower() */
  14. #ifdef NEED_SIGNAL_CATCHER
  15. #include <signal.h>        /* to declare signal() */
  16. #endif
  17. #ifdef USE_SETMODE
  18. #include <fcntl.h>        /* to declare setmode()'s parameter macros */
  19. /* If you have setmode() but not <io.h>, just delete this line: */
  20. #include <io.h>            /* to declare setmode() */
  21. #endif
  22.  
  23. #include "DeskLib:hourglass.h"    /* DeskLib hourglass support */
  24.  
  25. /*
  26.  * Signal catcher to ensure that temporary files are removed before aborting.
  27.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  28.  * we put "#define signal_catcher _abort" in jconfig.h.  Talk about bogus...
  29.  */
  30.  
  31. #ifdef NEED_SIGNAL_CATCHER
  32.  
  33. static j_common_ptr sig_cinfo;
  34.  
  35. GLOBAL void            /* must be global for Manx C */
  36. signal_catcher (int signum)
  37. {
  38.   if (sig_cinfo != NULL) {
  39.     if (sig_cinfo->err != NULL) /* turn off trace output */
  40.       sig_cinfo->err->trace_level = 0;
  41.     jpeg_destroy(sig_cinfo);    /* clean up memory allocation & temp files */
  42.     Hourglass_Off();        /* kill the hourglass */
  43.   }
  44.   exit(EXIT_FAILURE);
  45. }
  46.  
  47.  
  48. GLOBAL void
  49. enable_signal_catcher (j_common_ptr cinfo)
  50. {
  51.   sig_cinfo = cinfo;
  52.   signal(SIGINT, signal_catcher);
  53. #ifdef SIGTERM            /* not all systems have SIGTERM */
  54.   signal(SIGTERM, signal_catcher);
  55. #endif
  56. }
  57.  
  58. #endif
  59.  
  60.  
  61. /*
  62.  * Optional progress monitor: display a percent-done figure on stderr.
  63.  */
  64.  
  65. #ifdef PROGRESS_REPORT
  66.  
  67. METHODDEF void
  68. progress_monitor (j_common_ptr cinfo)
  69. {
  70.   cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
  71.   int total_completed_passes = prog->pub.completed_passes + prog->completed_extra_passes;
  72.   int total_passes = prog->pub.total_passes + prog->total_extra_passes;
  73.   int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
  74.  
  75. /*  if (percent_done != prog->percent_done) {
  76.     prog->percent_done = percent_done;
  77.     if (total_passes > 1) {
  78.       fprintf(stderr, "\rPass %d/%d: %3d%% ",
  79.           prog->pub.completed_passes + prog->completed_extra_passes + 1,
  80.           total_passes, percent_done);
  81.     } else {
  82.       fprintf(stderr, "\r %3d%% ", percent_done);
  83.     }
  84.     fflush(stderr);
  85.   } */
  86.   Hourglass_Percentage((percent_done/total_passes)+(total_completed_passes*100L/total_passes));
  87. }
  88.  
  89.  
  90. GLOBAL void
  91. start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
  92. {
  93.   /* Enable progress display, unless trace output is on */
  94.   /* if (cinfo->err->trace_level == 0) { */
  95.     progress->pub.progress_monitor = progress_monitor;
  96.     progress->completed_extra_passes = 0;
  97.     progress->total_extra_passes = 0;
  98.     progress->percent_done = -1;
  99.     cinfo->progress = &progress->pub;
  100.     Hourglass_On();
  101.   /* } */
  102. }
  103.  
  104.  
  105. GLOBAL void
  106. end_progress_monitor (j_common_ptr cinfo)
  107. {
  108.   /* Clear away progress display */
  109.   /* if (cinfo->err->trace_level == 0) {
  110.     fprintf(stderr, "\r                \r");
  111.     fflush(stderr);
  112.   } */
  113.   Hourglass_Off();
  114. }
  115.  
  116. #endif
  117.  
  118.  
  119. /*
  120.  * Case-insensitive matching of possibly-abbreviated keyword switches.
  121.  * keyword is the constant keyword (must be lower case already),
  122.  * minchars is length of minimum legal abbreviation.
  123.  */
  124.  
  125. GLOBAL boolean
  126. keymatch (char * arg, const char * keyword, int minchars)
  127. {
  128.   register int ca, ck;
  129.   register int nmatched = 0;
  130.  
  131.   while ((ca = *arg++) != '\0') {
  132.     if ((ck = *keyword++) == '\0')
  133.       return FALSE;        /* arg longer than keyword, no good */
  134.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  135.       ca = tolower(ca);
  136.     if (ca != ck)
  137.       return FALSE;        /* no good */
  138.     nmatched++;            /* count matched characters */
  139.   }
  140.   /* reached end of argument; fail if it's too short for unique abbrev */
  141.   if (nmatched < minchars)
  142.     return FALSE;
  143.   return TRUE;            /* A-OK */
  144. }
  145.  
  146.  
  147. /*
  148.  * Routines to establish binary I/O mode for stdin and stdout.
  149.  * Non-Unix systems often require some hacking to get out of text mode.
  150.  */
  151.  
  152. GLOBAL FILE *
  153. read_stdin (void)
  154. {
  155.   FILE * input_file = stdin;
  156.  
  157. #ifdef USE_SETMODE        /* need to hack file mode? */
  158.   setmode(fileno(stdin), O_BINARY);
  159. #endif
  160. #ifdef USE_FDOPEN        /* need to re-open in binary mode? */
  161.   if ((input_file = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
  162.     fprintf(stderr, "Cannot reopen stdin\n");
  163.     exit(EXIT_FAILURE);
  164.   }
  165. #endif
  166.   return input_file;
  167. }
  168.  
  169.  
  170. GLOBAL FILE *
  171. write_stdout (void)
  172. {
  173.   FILE * output_file = stdout;
  174.  
  175. #ifdef USE_SETMODE        /* need to hack file mode? */
  176.   setmode(fileno(stdout), O_BINARY);
  177. #endif
  178. #ifdef USE_FDOPEN        /* need to re-open in binary mode? */
  179.   if ((output_file = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
  180.     fprintf(stderr, "Cannot reopen stdout\n");
  181.     exit(EXIT_FAILURE);
  182.   }
  183. #endif
  184.   return output_file;
  185. }
  186.