-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|574|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Link Home -> Useful Apps


 
HoboBen
Created : 11 October 2010
Edited : 11 October 2010

Sed script to trim C/C++ comments



https://bash.cyberciti.biz/academic/sed-remove-c-cpp-comments/
I like coding for ansi C. As the lowest-common-denominator, it's portable. And really the later extensions to C don't add anything that I've found important.

But sometimes, you need to use somebody else's code for something, e.g. loading images, and they've used // C++ style comments. Which breaks your lovely ansi compile.

You'd be mad to remove the comments by hand (especially if the author releases new versions with bug fixes), so this script can be used to strip the comments completely and put the result in another file.

You could run the script automatically as part of your build process, or only run it when the file in question changes.

 

Comments


Monday, 11 October 2010, 05:40
JL235
Good, but it would be more useful to change the comments from '//' to '/* */'. That way you don't lose the comments.
Monday, 11 October 2010, 06:15
HoboBen
Indeed, but did you see the sed script? I have no idea how that works!

I'm not losing the comments though; I'm just outputting a copy, e.g. stb_image.h becomes stb_image_ansi.h, then I include that instead.

But I agree; ideally it would just convert the comments. I just didn't fancy writing it myself!
Monday, 11 October 2010, 07:02
JL235
If you have Ruby installed you can do ...

... although it only works on one file at time.

Usage: