home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1400 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: rsalz@bbn.com (Rich Salz)
  2. Newsgroups: comp.lang.c,alt.sources
  3. Subject: Re: How can I de-escape my strings at run time?
  4. Message-ID: <2596@litchi.bbn.com>
  5. Date: 1 Jun 90 15:11:59 GMT
  6.  
  7. In <6550.26639B0A@puddle.fidonet.org> cspw.quagga@p0.f4.n494.z5.fidonet.org (cspw quagga) writes:
  8. >Is there an easy way to read a string into a buffer with automatic run-time
  9. >translation of the escape sequences?  I want to do something like this:
  10. > { char fmt[100];
  11. >   gets(fmt);
  12. >      descape(fmt);   /*   ... This is the function I need   */
  13. >   printf(fmt,123);
  14. >   ...
  15. > }
  16.  
  17. /*
  18. **  Convert C escape sequences in a string.  Returns a pointer to
  19. **  malloc'd space, or NULL if malloc failed.
  20. */
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #define OCTDIG(c)    ('0' <= (c) && (c) <= '7')
  25. #define HEXDIG(c)    isxdigit(c)
  26.  
  27. char *
  28. UnEscapify(text)
  29.     register char    *text;
  30. {
  31.     extern char        *malloc();
  32.     register char    *p;
  33.     char        *save;
  34.     int            i;
  35.  
  36.     if ((save = malloc(strlen(text) + 1)) == NULL)
  37.     return NULL;
  38.  
  39.     for (p = save; *text; text++, p++) {
  40.     if (*text != '\\')
  41.         *p = *text;
  42.     else {
  43.         switch (*++text) {
  44.         default:                /* Undefined; ignore it    */
  45.         case '\'': case '\\': case '"': case '?':
  46.         *p = *text;
  47.         break;
  48.  
  49.         case '\0':
  50.         *p = '\0';
  51.         return save;
  52.  
  53.         case '0': case '1': case '2': case '3':
  54.         case '4': case '5': case '6': case '7':
  55.         for (*p = 0, i = 0; OCTDIG(*text) && i < 3; text++, i++)
  56.             *p = (*p << 3) + *text - '0';
  57.         text--;
  58.         break;
  59.  
  60.         case 'x':
  61.         for (*p = 0; *++text && isxdigit(*text); )
  62.             if (isdigit(*text))
  63.             *p = (*p << 4) + *text - '0';
  64.             else if (isupper(*text))
  65.             *p = (*p << 4) + *text - 'A';
  66.             else
  67.             *p = (*p << 4) + *text - 'a';
  68.         text--;
  69.         break;
  70.  
  71.         case 'a':    *p = '\007';    break;    /* Alert        */
  72.         case 'b':    *p = '\b';    break;    /* Backspace        */
  73.         case 'f':    *p = '\f';    break;    /* Form feed        */
  74.         case 'n':    *p = '\n';    break;    /* New line        */
  75.         case 'r':    *p = '\r';    break;    /* Carriage return    */
  76.         case 't':    *p = '\t';    break;    /* Horizontal tab    */
  77.         case 'v':    *p = '\n';    break;    /* Vertical tab        */
  78.  
  79.         }
  80.     }
  81.     }
  82.     *p = '\0';
  83.     return save;
  84. }
  85.  
  86. #ifdef    TEST
  87.  
  88. main()
  89. {
  90.     char    buff[256];
  91.     char    *p;
  92.  
  93.     printf("Enter strings, EOF to quit:\n");
  94.     while (gets(buff)) {
  95.     if ((p = UnEscapify(buff)) == NULL) {
  96.         perror("Malloc failed");
  97.         abort();
  98.     }
  99.     printf("|%s|\n", p);
  100.     free(p);
  101.     }
  102.     exit(0);
  103.  
  104. }
  105. #endif    /*TEST */
  106. -- 
  107. Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
  108. Use a domain-based address or give alternate paths, or you may lose out.
  109.