+string preprocess(char const * line, bool & comment, bool & single_quote,
+ bool & double_quote, unsigned & open_braces)
+{
+ // "preprocess" the line entered to be able to decide if the command shall be
+ // executed directly or more input is needed or this is a special command
+
+ // all whitespace will be removed
+ // all comments (/* */ and //) will be removed
+ // open and close braces ( { and } ) outside strings will be counted
+
+ /*
+ cout << "line=" << line << endl;
+ cout << "comment=" << comment << ", single_quote=" << single_quote
+ << ",double_quote=" << double_quote << ", open_braces=" << open_braces
+ << endl;
+ */
+
+ string preprocessed;
+ int pos=0;
+ bool end=false;
+ bool escape=false;
+ bool slash=false;
+ bool asterisk=false;
+ while ((line[pos]!='\0')&&!end) {
+ if (escape) {
+ // last character was a \, ignore this one
+ escape=false;
+ } else if (slash) {
+ // last character was a /, test if * or /
+ slash=false;
+ if (line[pos]=='/') {
+ end=true;
+ } else if (line[pos]=='*') {
+ comment=true;
+ } else {
+ preprocessed += '/';
+ }
+ } else if (asterisk) {
+ // last character was a *, test if /
+ asterisk=false;
+ if (line[pos]=='/') {
+ comment=false;
+ } else if (line[pos]=='*') {
+ preprocessed += '*';
+ asterisk=true;
+ }
+ } else {
+ switch (line[pos]) {
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ // whitespace: ignore
+ break;
+ case '\\':
+ // escape character, ignore next
+ escape=true;
+ break;
+ case '"':
+ if ((!single_quote)&&(!comment)) {
+ double_quote = !double_quote;
+ }
+ break;
+ case '\'':
+ if ((!double_quote)&&(!comment)) {
+ single_quote = !single_quote;
+ }
+ break;
+ case '{':
+ if ((!single_quote)&&(!double_quote)&&(!comment)) {
+ open_braces++;
+ }
+ break;
+ case '}':
+ if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
+ open_braces--;
+ }
+ break;
+ case '/':
+ slash=true;
+ break;
+ case '*':
+ asterisk=true;
+ break;
+ default:
+ preprocessed += line[pos];
+ }
+ }
+ pos++;
+ }
+
+ /*
+ cout << "preprocessed=" << preprocessed << endl;
+ cout << "comment=" << comment << ", single_quote=" << single_quote
+ << ",double_quote=" << double_quote << ", open_braces=" << open_braces
+ << endl;
+ */
+
+ return preprocessed;
+}
+