home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume18 / smiley / part01 / mkfaces.c < prev    next >
C/C++ Source or Header  |  1991-04-26  |  2KB  |  106 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.     int             lineno = 0;    /* number of current line */
  52.  
  53.     /* prologue */
  54.  
  55.     (void) printf("#include \"smiley.h\"\n");
  56.     (void) printf("struct smiley faces[] = {\n");
  57.  
  58.     /* process each smiley line */
  59.  
  60.     prev[0] = 0;        /* initialize prev to null string */
  61.  
  62.     while (scanf("%[^\t]\t%[^\n]\n", face, desc) == 2)
  63.     {
  64.         if (strcmp(prev, face))
  65.         {
  66.  
  67.             /*
  68.              * Since this face differs from the last one,
  69.              * prepare to start a new entry.
  70.              * 
  71.              * Complete the previous entry if there was one.
  72.              */
  73.             if (lineno > 0)
  74.             {
  75.                 (void) fputs("\"},\n", stdout);
  76.             }
  77.  
  78.             (void) printf("{\"%s\",\"", enquote(face));
  79.         }
  80.         else
  81.         {
  82.  
  83.             /*
  84.              * Since this face is the same as the last
  85.              * one, simply continue the description.
  86.              */
  87.             (void) fputs("\\n\\t", stdout);
  88.         }
  89.  
  90.         /* write the current description line */
  91.         (void) fputs(enquote(desc), stdout);
  92.  
  93.         lineno++;
  94.         (void) strcpy(prev, face);
  95.     }
  96.  
  97.     (void) fputs("\"}\n", stdout);    /* complete the last entry */
  98.  
  99.     /* epilogue */
  100.  
  101.     (void) printf("};\n");
  102.     (void) printf("int nfaces = sizeof(faces)/sizeof(struct smiley);\n");
  103.  
  104.     return 0;
  105. }
  106.