home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / calgen / part1 / pic_h2m.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  5.6 KB  |  197 lines

  1. /*******************************************************
  2.  *
  3.  *          Calendar Generation Program
  4.  *        Copyright 1986 David H. Brierley
  5.  *
  6.  * Permission is granted to anyone to use this software for any
  7.  * purpose on any computer system, and to redistribute it freely,
  8.  * subject to the following restrictions:
  9.  * 1. The author is not responsible for the consequences of use of
  10.  *    this software, no matter how awful, even if they arise
  11.  *    from defects in it.
  12.  * 2. The origin of this software must not be misrepresented, either
  13.  *    by explicit claim or by omission.
  14.  * 3. Altered versions must be plainly marked as such, and must not
  15.  *    be misrepresented as being the original software.
  16.  *
  17.  * David H. Brierley
  18.  * Portsmouth, RI
  19.  * {allegra,ihnp4,linus}!rayssd!dhb
  20.  *
  21.  ********************************************************/
  22.  
  23. /*
  24.  * pic_h2m - calndr program support module
  25.  *
  26.  * This program converts the picture data file from human readable
  27.  * format to the internal machine format that the calndr program
  28.  * is expecting to see.  The purpose for doing this is to speed up
  29.  * execution of the calndr program.  If you are concerned about the
  30.  * extra disk space required by the internal format of the data file
  31.  * you may use the '-r' flag to the calndr program.
  32.  */
  33.  
  34. /*
  35.  * Format of human readable version of the data file.
  36.  * 1. The first 84 lines of the file contain the names of the months.
  37.  *    Each month name is 7 lines long and should be centered in
  38.  *    eighty columns.  You should probably not touch this data.
  39.  * 2. The next 50 lines contain the numbers from 0 to 9, five lines
  40.  *    each, five characters wide.  Again, dont touch.
  41.  * 3. The last section of the file contains the pictures.  This section
  42.  *    may be modified to give different pictures.  Since the original
  43.  *    program was in FORTRAN, the picture data uses fortran carriage
  44.  *    control characters in the first column.  The beginning of each
  45.  *    months picture is denoted by a top of form control ("1").  There
  46.  *    should be exactly twelve pictures.  The first picture is always
  47.  *    used for January, the second for February, etc.  As a general
  48.  *    rule, the picture data is straight ascii text.  The one exception
  49.  *    to this is that a character may be followed by a count enclosed
  50.  *    within squares brackets.  The purpose of this is to allow the
  51.  *    data file to be compressed but still be, for the most part, human
  52.  *    readable.  One implication of this is that the data file cannot
  53.  *    contain an open bracket followed by a string of digits followed
  54.  *    by a close bracket.  The current data file does not even contain
  55.  *    any open brackets, never mind all the rest of the crap.
  56.  */
  57.  
  58. #include <stdio.h>
  59. #include <ctype.h>
  60.  
  61. main (argc, argv)
  62. int     argc;
  63. char   *argv[];
  64. {
  65.     char    line[140];
  66.     char    enil[140];
  67.     char   *p1;
  68.     char   *p2;
  69.     char   *p3;
  70.     char    moname[85];
  71.     char    nums[10];
  72.     int     points[13];
  73.     int     psize;
  74.     FILE   *picdata;
  75.     int     newdata;
  76.     int     n;
  77.     int     len;
  78.     int     locator;
  79.     long    pos;
  80.     long    lseek ();
  81.  
  82.     if (argc != 3) {
  83.     fprintf (stderr, "Usage: %s infile outfile\n", argv[0]);
  84.     exit (1);
  85.     }
  86.     picdata = fopen (argv[1], "r");
  87.     if (picdata == NULL) {
  88.     perror ("calndr");
  89.     fprintf (stderr, "Unable to open file '%s'\n", argv[1]);
  90.     exit (1);
  91.     }
  92.     newdata = creat (argv[2], 0666);
  93.     if (newdata == -1) {
  94.     perror ("calndr");
  95.     fprintf (stderr, "Unable to open file '%s'\n", argv[2]);
  96.     }
  97.  
  98.     for (n = 0; n < 12 * 7; n++) {
  99.     if (fgets (moname, 85, picdata) == NULL) {
  100.         fprintf (stderr, "I/O error on file\n");
  101.         exit (1);
  102.     }
  103.     len = strlen (moname);
  104.     for (--len; len < 81; len++) {
  105.         moname[len] = '\0';
  106.     }
  107.     if (write (newdata, moname, 81) != 81) {
  108.         perror ("calndr");
  109.         fprintf (stderr, "I/O error writing output file\n");
  110.         exit (1);
  111.     }
  112.     }
  113.     for (n = 0; n < 10 * 5; n++) {
  114.     if (fgets (nums, 10, picdata) == NULL) {
  115.         fprintf (stderr, "I/O error on file\n");
  116.         exit (1);
  117.     }
  118.     len = strlen (nums);
  119.     for (--len; len < 6; len++) {
  120.         nums[len] = '\0';
  121.     }
  122.     if (write (newdata, nums, 6) != 6) {
  123.         perror ("calndr");
  124.         fprintf (stderr, "I/O error writing output file\n");
  125.         exit (1);
  126.     }
  127.     }
  128.     pos = lseek (newdata, 0L, 1);
  129.     if (pos == -1) {
  130.     perror ("calndr");
  131.     fprintf (stderr, "Unable to determine position in file\n");
  132.     exit (1);
  133.     }
  134.     psize = 13 * sizeof (int);
  135.     if (write (newdata, (char *) points, psize) != psize) {
  136.     perror ("calndr");
  137.     fprintf (stderr, "I/O error writing output file\n");
  138.     exit (1);
  139.     }
  140.     locator = 0;
  141.     for (n = 0;; n++) {
  142.     if (fgets (enil, 140, picdata) == NULL) {
  143.         break;
  144.     }
  145.     if (enil[0] == '1') {
  146.         if (locator == 12) {
  147.         break; /* already have 12 pictures */
  148.         }
  149.         points[locator] = n;
  150.         locator++;
  151.     }
  152.     p1 = enil;
  153.     p2 = line;
  154.     while (*p1 != '\n') {
  155.         if (*p1 != '[') {
  156.         *p2++ = *p1++;
  157.         continue;
  158.         }
  159.             for (p3 = p1 + 1; isdigit (*p3); p3++) ; /* find close bracket */
  160.         if (*p3 == ']') {
  161.         p3 = p1 - 1;
  162.         len = atoi (++p1);
  163.         while (len-- > 0) {
  164.             *p2++ = *p3;
  165.         }
  166.         while (*p1 != ']') p1++;
  167.         p1++;
  168.         continue;
  169.         }
  170.         *p2++ = *p1++;
  171.     }
  172.     for (; p2 < line + 134; p2++) {
  173.         *p2 = '\0';
  174.     }
  175.     if (write (newdata, line, 134) != 134) {
  176.         perror ("calndr");
  177.         fprintf (stderr, "I/O error writing output file\n");
  178.         exit (1);
  179.     }
  180.     }
  181.     points[locator] = n;
  182.     if (lseek (newdata, pos, 0) == -1) {
  183.     perror ("calndr");
  184.     fprintf (stderr, "Unable to seek backwards in file\n");
  185.     exit (1);
  186.     }
  187.     if (write (newdata, (char *) points, psize) != psize) {
  188.     perror ("calndr");
  189.     fprintf (stderr, "I/O error writing output file\n");
  190.     exit (1);
  191.     }
  192.  
  193.     (void) fclose (picdata);
  194.     (void) close (newdata);
  195.  
  196. }
  197.