stollgrin
stollgrin

Reputation: 109

Printing strings (char*) loaded from file without escaped special sequences (like \n)

I'm currently writing a parser for my toy language, and as a part of that parser i have written print function that well... basically prints its argument. For string constant all it does is

printf("%s", pointer);

so

print("\n")

should be executed as

printf("%s", ptr_to_loaded_string);

(more or less)

However, my current problem is, that C escapes special character sequences while reading script file. So instead of "\n" I get "\\n".

My question is: is there some way I can avoid the escaping of this sequences, and if not what's the best way to deal with them? I'm currently thinking about search and replace - replace each sequence of 2 '\' with one '\', but it may be a little problematic (string length change, reallocing, etc.) - i want to avoid that solution unless it's absolutely necessary.

edit: argh, stackoverflow escaped my example....

Upvotes: 1

Views: 1195

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490448

It's not that C is un-escaping your sequences -- it's that it's simply leaving them alone, so your "\n" in the input stream is read as two characters ('\' and 'n').

Here's some code I wrote years ago to deal with this:

/*
** Public Domain by Jerry Coffin.
**
** Interpets a string in a manner similar to that the compiler
** does string literals in a program.  All escape sequences are
** longer than their translated equivalant, so the string is
** translated in place and either remains the same length or
** becomes shorter.
*/

#include <string.h>
#include <stdio.h>
#include "snip_str.h"

char *translate(char *string)
{
      char *here=string;
      size_t len=strlen(string);
      int num;
      int numlen;

      while (NULL!=(here=strchr(here,'\\')))
      {
            numlen=1;
            switch (here[1])
            {
            case '\\':
                  break;

            case 'r':
                  *here = '\r';
                  break;

            case 'n':
                  *here = '\n';
                  break;

            case 't':
                  *here = '\t';
                  break;

            case 'v':
                  *here = '\v';
                  break;

            case 'a':
                  *here = '\a';
                  break;

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
                  numlen = sscanf(here,"%o",&num);
                  *here = (char)num;
                  break;

            case 'x':
                  numlen = sscanf(here,"%x",&num);
                  *here = (char) num;
                  break;
            }
            num = here - string + numlen;
            here++;
            memmove(here,here+numlen,len-num );
      }
      return string;
}

Upvotes: 2

apmasell
apmasell

Reputation: 7153

If you are willing to use the GLib, you can g_strcompress your string to convert the escaped characters and then print the result.

Upvotes: 0

Attila
Attila

Reputation: 28772

You cannot have C-style special characters directly interpreted from char sequences (e.g. from an input file). You need to write parsing logic to determine if the sequence contains the required special char sequence and treat it accordingly

Note: make sure you handle escaped-escape characters properly as well.

Upvotes: 1

Related Questions