1

I have interpreter from another language in C. I have to pass code (about 200 lines) from another language to this interpreter and here problem occur.

char* command_line[] = { "", "-e", "print \"Hello from C!\\n\";" }; 

This code is parse by:

(..) parser(my_p, NULL, 3, command_line, (char **)NULL); (...) 

In code abobe I use array but even simple code have to be wrapped with \ before chars like " ; | \ etc.

How to avoid this problem and pass more then 200 multi row lines of code comfortable?

5
  • 2
    As it stands, this is a terrible question. What does parser do? You can't tell anything from the question as it is. Commented Aug 21, 2013 at 9:44
  • 2
    It's not too bad! My understand is that he just wants to write code for the "interpreted language" in his C program, but having to escape all the special characters and quotes is tiresome. Commented Aug 21, 2013 at 9:45
  • 1
    load it from a file and do not put it into the source code. Commented Aug 21, 2013 at 9:50
  • is it c or c++? in c++(11) you have the option as in my above comment. in c, no such luck. but why not read from a file? also your question could really be better formulated, it's very hard to guess what is actually being asked (that that problem are the special characters) Commented Aug 21, 2013 at 9:51
  • @PeterWalser What did you change with respect to the suggested edit, why didn't you mark it as helpful and/or at least also remove the "thanks" at the end? Commented Aug 21, 2013 at 9:57

4 Answers 4

5

If you are using C++11, you can use raw string literals.

R"(print "Hello from C!\n";)" 

Or you can simply put all the data into an external file and just read it on start. No need to escape any data there.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please provide example of load from external file ?
1

You could use the C preprocessor to do the stringification for you:

#define STRINGIFY(...) #__VA_ARGS__ #define STRINGIFY_NL(...) #__VA_ARGS__ "\n" char* command_line[] = { "", "-e", STRINGIFY(print "Hello from C!\n";), //< one element of the array //< another element of the array //< with embedded NL STRINGIFY_NL(print "Hello from C!") //< no comma and the next line is glued STRINGIFY ("for a second time";), //< end of other string }; 

The only restrictions to observe would be that possible () have to balance inside the argument to STRINGIFY and that you'd have to place the macro on each line that you want to escape.

4 Comments

It works but how to work with multi row text / code ?
If you enter several lines into this, they will be stashed together into one single line. I'll add something that would help you to add a nl character at the end of an input line.
This doesn't work if any of the lines contain a comma character.
@interjay, right, please see my edit. For this to work you'd need C99 or C++11, I think.
0

Unfortunately there is no support for literal strings or similar useful constructs in C, so if you want to write the interpreted code inside your C program you will have to be careful and escape quotes and slashes as you have stated.

The alternative is to write the code into a text file and treat it as an external resource file. You can read the resource file from inside your code into a string and then pass that to parser().

The ease of this depends on the platform you are using. Windows has good support for resource files and embedding them in to .exe files. I am sure it is possible with gcc too, but I haven't done it before. A bit vague I'm afraid, but I hope it helps.

Comments

-1

You can use a script to take your text input, as a file, and stringify it (escaping double-quotes and newlines):

#!/usr/bin/env python import sys def main(argv = None): if argv is None: argv = sys.argv if len(argv) < 2: sys.stderr.write("Usage: stringify.py input.txt [... input.txt]\n") sys.exit(1) i = 1 while i < len(argv): inname = argv[i] firstline = True try: infile = open(inname, "r") for line in infile: line = line.replace('"', '\\"').replace('\n', '\\n') sys.stdout.write('"{0}"\n'.format(line)) except IOError, msg: sys.stderr.write("exception {0}\n".format(msg)) return 2 i = i + 1 return 0 if __name__ == "__main__": sys.exit(main()) 

4 Comments

<irony>Wow, that looks so universal :-D</irony>
@Let_Me_Be You don't like the idea of universal?
I don't see how this helps with the problem.
@MariusBancila Because it saves the OP from having to manually escape his input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.