home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / util / makestrs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-04  |  4.4 KB  |  193 lines

  1. /* $XConsortium: makestrs.c,v 1.4 91/05/04 18:25:06 rws Exp $ */
  2. /*
  3. Copyright 1991 by the Massachusetts Institute of Technology
  4.  
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. /* Constructs string definitions */
  17.  
  18. #include <stdio.h>
  19. #include <X11/Xos.h>
  20. #ifndef X_NOT_STDC_ENV
  21. #include <stdlib.h>
  22. #else
  23. char *malloc();
  24. #endif
  25. #if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
  26. char *malloc();
  27. #endif /* macII */
  28.  
  29. typedef struct _Rec {
  30.     struct _Rec *next;
  31.     char *left;
  32.     char *right;
  33.     int offset;
  34. } Rec;
  35.  
  36. char buf[1024];
  37. char global[64];
  38. char header_name[64];
  39. Rec *recs;
  40. Rec **tail = &recs;
  41. int offset;
  42.  
  43. AddRec(left, right)
  44.     char *left;
  45.     char *right;
  46. {
  47.     Rec *rec;
  48.     int llen;
  49.     int rlen;
  50.     int len;
  51.  
  52.     llen = len = strlen(left) + 1;
  53.     rlen = strlen(right) + 1;
  54.     if (right != left + 1)
  55.     len += rlen;
  56.     rec = (Rec *)malloc(sizeof(Rec) + len);
  57.     if (!rec)
  58.     exit(1);
  59.     rec->offset = offset;
  60.     offset += rlen;
  61.     rec->left = (char *)(rec + 1);
  62.     strcpy(rec->left, left);
  63.     if (llen != len) {
  64.     rec->right = rec->left + llen;
  65.     strcpy(rec->right, right);
  66.     } else {
  67.     rec->right = rec->left + 1;
  68.     }
  69.     *tail = rec;
  70.     tail = &rec->next;
  71. }
  72.  
  73. FlushRecs(header)
  74.     FILE *header;
  75. {
  76.     Rec *rec;
  77.  
  78.     *tail = NULL;
  79.     fprintf(header, "/* This file is automatically generated. */\n");
  80.     fprintf(header, "/* Do not edit. */\n");
  81.     fprintf(header, "\n");
  82.     fprintf(header, "#ifndef _Xt%s_h_\n", header_name);
  83.     fprintf(header, "#define _Xt%s_h_\n", header_name);
  84.     fprintf(header, "#ifdef XTSTRINGDEFINES\n");
  85.     for (rec = recs; rec; rec = rec->next) {
  86.     fprintf(header, "#define Xt%s \"%s\"\n", rec->left, rec->right);
  87.     }
  88.     fprintf(header, "#else\n");
  89.     fprintf(header, "#if __STDC__\n");
  90.     fprintf(header, "#define _XtConst_ const\n");
  91.     fprintf(header, "#else\n");
  92.     fprintf(header, "#define _XtConst_ /**/\n");
  93.     fprintf(header, "#endif\n");
  94.     fprintf(header, "extern _XtConst_ char %s[];\n", global);
  95.     for (rec = recs; rec; rec = rec->next) {
  96.     fprintf(header, "#ifndef Xt%s\n", rec->left);
  97. #ifdef ARRAYPERSTR
  98.     fprintf(header, "extern char Xt%s[];\n", rec->left);
  99. #else
  100.     fprintf(header, "#define Xt%s ((char*)&%s[%d])\n",
  101.         rec->left, global, rec->offset);
  102. #endif
  103.     fprintf(header, "#endif\n");
  104.     }
  105.     fprintf(header, "#undef _XtConst_\n");
  106.     fprintf(header, "#endif\n");
  107.     fprintf(header, "#endif\n");
  108.     while (rec = recs) {
  109.     recs = rec->next;
  110.     free((char *)rec);
  111.     }
  112.     tail = &recs;
  113.     offset = 0;
  114. }
  115.  
  116. main()
  117. {
  118.     FILE *header = NULL;
  119.     char *right;
  120. #ifndef ARRAYPERSTR
  121.     int first;
  122. #endif
  123.  
  124.     printf("/* This file is automatically generated. */\n");
  125.     printf("/* Do not edit. */\n");
  126.     printf("\n");
  127.     printf("#if __STDC__\n");
  128.     printf("#define Const const\n");
  129.     printf("#else\n");
  130.     printf("#define Const /**/\n");
  131.     printf("#endif\n");
  132.     while (gets(buf)) {
  133.     if (!buf[0] || (buf[0] == '!'))
  134.         continue;
  135.     if (buf[0] == ':') {
  136.         if (header) {
  137. #ifndef ARRAYPERSTR
  138.         printf("\n");
  139.         printf("};\n");
  140. #endif
  141.         FlushRecs(header);
  142.         fclose(header);
  143.         }
  144.         right = index(buf, ' ');
  145.         if (!right)
  146.         exit(1);
  147.         *right = 0;
  148.         header = fopen(buf+1, "w");
  149.         if (!header) {
  150.         perror(buf+1);
  151.         exit(1);
  152.         }
  153. #ifndef ARRAYPERSTR
  154.         first = 1;
  155.         strcpy(global, right+1);
  156.         printf("\n");
  157.         printf("Const char %s[] = {\n", global);
  158. #endif
  159.         strcpy(header_name, buf+1);
  160.         right = index(header_name, '.');
  161.         if (right)
  162.         *right = 0;
  163.         continue;
  164.     }
  165.     if (right = index(buf, ' '))
  166.         *right++ = 0;
  167.     else
  168.         right = buf + 1;
  169.     AddRec(buf, right);
  170. #ifdef ARRAYPERSTR
  171.     printf("Const char Xt%s[] = \"%s\";\n", buf, right);
  172. #else
  173.     if (!first)
  174.         printf(",\n");
  175.     first = 0;
  176.     while (*right) {
  177.         printf("'%c',", *right);
  178.         right++;
  179.     }
  180.     printf("0");
  181. #endif
  182.     }
  183.     if (header) {
  184.     FlushRecs(header);
  185.     fclose(header);
  186. #ifndef ARRAYPERSTR
  187.     printf("\n");
  188.     printf("};\n");
  189. #endif
  190.     }
  191.     exit(0);
  192. }
  193.