c - simple script or commands to *substitute* stray "\\n" with "\n" -


alright, understand title of topic sounds bit gibberish... i'll try explain can...

this related previous post (an approach that's been verified work): multipass source code cpp

-- asks cpp preprocess code once before starting gcc compile build process

take previous post's sample code:

#include <stdio.h> #define def_x   #define x           22  int main(void) {     def_x     printf("%u", x);      return 1; } 

now, able freely insert def_x anywhere, need add newline

this doesn't work:

#define def_x                    \                 #define x    22 

this still doesn't work, more to:

#define def_x   \n                    \                 #define x    22 

if latter above work, c's free form syntax , constant string multiline concatenation, works anywhere far c/c++ concerned:

"literal_str0" def_x "literal_str1" 

now when cpp preprocesses this:

# 1 "d:/projects/research/tests/test.c" # 1 "<command-line>" # 1 "d:/projects/research/test/test.c" # 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/stdio.h" 1 3 # 19 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/stdio.h" 3 # 1 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/_mingw.h" 1 3 # 32 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/_mingw.h" 3= # 33 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/_mingw.h" 3 # 20 "c:\\mingw\\bin\\../lib/gcc/mingw32/4.7.2/../../../../include/stdio.h" 2 3  etc_etc_etc_ignored_for_brevity_but_lots_of_declarations   int main(void) {     \n #define x 22     printf("%u", x);      return 1; } 

we have stray \n in our preprocessed file. problem rid of it....

now, unix system commands aren't strongest suit. i've compiled dozens of packages in linux , written simple bash scripts enter multiline commands (so don't have type them every time or keep pressing arrow , choose correct command successions). don`t know finer points of stream piping , arguments.

having said that, tried these commands:

cpp $my_dir/test.c | perl -p -e 's/\\n/\n/g' > $my_dir/test0.c gcc $my_dir/test0.c -o test.exe 

it works, removes stray \n.

ohh, using perl rather sed, i'm more familiar perl's variant regex... it's more consistent in eyes.

anyways, has nasty side effect of eating \n in file (even in string literals)... need script or series of commands to:

remove \n if:

  1. if not inside quote -- won't modified: "hell0_there\n"
  2. not passed function call (inside argument list)
    • this safe 1 can never pass single \n, neither keyword nor identifier.
    • if need "stringify" expression \n, can call function macro quote_var(token). encapsulates instances \n have treated string.

this should cover cases \n should substituted... @ least own coding conventions.

really, if manage on own... skills in regex extremely lacking, using in simple substitutions.

the better way replace \n if occurs in beginning of line.

the following command should work:

sed -e 's/\s*\\n/\n/g' 

or occurs before #

sed -e 's/\\n\s*#/\n#/g' 

or can reverse order of preprocessing , substitute def_x own tool before c preprocessor.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -