home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / smiley40.zip / MKFACES.C < prev    next >
C/C++ Source or Header  |  1993-01-17  |  2KB  |  112 lines

  1. /*
  2.  * m k f a c e s . c
  3.  *
  4.  * This program reads the file with the list of smileys and writes
  5.  * out an initialized data structure containing the smileys.
  6.  * 
  7.  * It assumes the smileys are each on one line, followed by a tab,
  8.  * followed by some description.
  9.  *
  10.  * DaviD W. Sanderson
  11.  */
  12.  
  13. #include <stdio.h>
  14.  
  15. /* avoid the possible <string.h> vs <strings.h> dilemma */
  16.  
  17. extern int
  18.     strcmp();
  19. extern char *
  20.     strcpy();
  21.  
  22. /* turn string into C string initialization */
  23.  
  24. static char    *
  25. enquote(s)
  26.     char           *s;
  27. {
  28.     static char     ar[1024];
  29.     char           *t = ar;
  30.  
  31.     for (; *s; s++)
  32.     {
  33.         switch (*s)
  34.         {
  35.         case '\\':
  36.         case '\"':
  37.             *t++ = '\\';
  38.         }
  39.         *t++ = *s;
  40.     }
  41.     *t = '\0';
  42.     return ar;
  43. }
  44.  
  45. int
  46. main()
  47. {
  48.     char            prev[1024];    /* previous face */
  49.     char            face[1024];    /* current face */
  50.     char            desc[1024];    /* current description */
  51.     char        nl[2];        /* for newline at end-of-line */
  52.     int             lineno = 0;    /* number of current line */
  53.  
  54.     /* prologue */
  55.  
  56.     (void) printf("#include \"smiley.h\"\n");
  57.     (void) printf("struct smiley faces[] = {\n");
  58.  
  59.     /* process each smiley line */
  60.  
  61.     prev[0] = 0;        /* initialize prev to null string */
  62.  
  63.     /*
  64.      * If simply matched literal \n at the end of the string, scanf
  65.      * would skip leading white space on the next line.
  66.      * This is not what I want.
  67.      */
  68.     while (scanf("%[^\t]\t%[^\n]%[\n]", face, desc, nl) == 3)
  69.     {
  70.         if (strcmp(prev, face))
  71.         {
  72.  
  73.             /*
  74.              * Since this face differs from the last one,
  75.              * prepare to start a new entry.
  76.              * 
  77.              * Complete the previous entry if there was one.
  78.              */
  79.             if (lineno > 0)
  80.             {
  81.                 (void) fputs("\"},\n", stdout);
  82.             }
  83.  
  84.             (void) printf("{\"%s\",\"", enquote(face));
  85.         }
  86.         else
  87.         {
  88.  
  89.             /*
  90.              * Since this face is the same as the last
  91.              * one, simply continue the description.
  92.              */
  93.             (void) fputs("\\n\\t", stdout);
  94.         }
  95.  
  96.         /* write the current description line */
  97.         (void) fputs(enquote(desc), stdout);
  98.  
  99.         lineno++;
  100.         (void) strcpy(prev, face);
  101.     }
  102.  
  103.     (void) fputs("\"}\n", stdout);    /* complete the last entry */
  104.  
  105.     /* epilogue */
  106.  
  107.     (void) printf("};\n");
  108.     (void) printf("int nfaces = sizeof(faces)/sizeof(struct smiley);\n");
  109.  
  110.     return 0;
  111. }
  112.