#include <iostream>#include <fstream>/* g++ -o decomment decomment.cpp */// decommentvoiddecomment(std::ostream&os,std::istream&is){boolblc,olc;/* in block/one-line comment */boolstr,chr;/* in string/char literal */blc=olc=str=chr=false;charc,pc='\0';while(is.get(c)){// escapeif(c=='\\'){if(!olc&&!blc){if(pc=='/')os.put('/');os.put(c);}is.get(c);if(!olc&&!blc)os.put(c);}// end of one-line commentelseif(olc&&(c=='\n'||c=='\r')){olc=false;os.put(c);pc='\0';continue;}// end of block commentelseif(blc&&pc=='*'&&c=='/'){blc=false;pc='\0';continue;}// start of one-line commentelseif(pc=='/'&&c=='/'&&!str&&!chr&&!blc){olc=true;pc='\0';continue;}// start of block commentelseif(pc=='/'&&c=='*'&&!str&&!chr&&!olc){blc=true;pc='\0';}// start/end of char literalelseif(!olc&&!blc&&c=='\''){chr=!chr;if(pc=='/')os.put('/');os.put(c);}// start/end of string literalelseif(!olc&&!blc&&c=='\"'){str=!str;if(pc=='/')os.put('/');os.put(c);}else{if(pc=='/'&&!olc&&!blc)os.put('/');if(c!='/'&&!olc&&!blc)os.put(c);}pc=c;}}namespace{constchar*test="//\ ""/*""hoge""*/"/* / * /* // */"hige";}intmain(intc,char**v){(void)test;if(c<2){std::cout<<"usage: "<<v[0]<<" <C++ source file>\n";return0;}std::ifstreamifs(v[1]);if(!ifs){std::cerr<<"failed to open "<<v[1]<<"\n";return1;}decomment(std::cout,ifs);return0;}
turugina
#6554()
[
C++
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]