Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2006, 9:55 AM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Comments stripper (stripc in C)

Well I had a file which had a load of comments, and I wanted to get rid of them fast. So I made this program. Please comment for any enhancements or anything else.

LGPL basically means you can use it in both proprietary and open source projects, but just give me credit. (I think, if not then change the licence to that.)
The file I tested it on is shown below:
  	/* very stupid
  	example */
#include <stdio.h>

int main()
{
	int i;
	printf("hi\n");

	/* huh? */ printf("%d\n", '/*');

	if(1 != 1/2)
		printf("stup/*id\n");

	/*** bye ***/
	for(i = 0; /* lol */ i < 1; i++)
		printf("bye\n");

	// exit
	return 0;
}
/*
stripc - strips your comments.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

/* trouble in river city (TM DaWei) */
int uhOh(char * message)
{
	fprintf(stderr, "%s\n", message);
	return 1;
}

/* main */
int main(int argc, char ** argv)
{
	int i;
	bool comment = false, block = false, qliteral = false;
	FILE * fp;
	long fsize;
	char * buffer;
	char * file;

	/* check arguments and get filename */
	if(argc != 2)
		return uhOh("Usage: stripc <filename>");
	file = argv[1];

	/* open file */
	fp = fopen(file, "rb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* obtain file size */
	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	rewind(fp);

	/* allocate memory */
	buffer = malloc(fsize);
	if(buffer == NULL)
		return uhOh("Could not allocate memory.");

	/* copy file into buffer */
	fread(buffer, 1, fsize, fp);
	fclose(fp);
	fp = fopen(file, "wb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* loop through buffer */
	for(i = 0; i < fsize; i++)
	{
		if(!comment && !qliteral && (buffer[i] == '/' && (buffer[i+1] == '/' || buffer[i+1] == '*')))
		{
			comment = true;
			if(buffer[i+1] == '*')
				block = true;
			i++;
		}

		if(!comment && (buffer[i] == '"' || buffer[i] == '\'' && buffer[i-1] != '\\'))
		{
			static bool squote = false, dblquote = false;
			if(buffer[i] == '"' && !squote)
			{
				dblquote =! dblquote;
				qliteral =! qliteral;
			}
			else if(buffer[i] == '\'' && !dblquote)
			{
				squote =! squote;
				qliteral =! qliteral;
			}
		}

		if(!comment || qliteral)
                fprintf(fp, "%c", buffer[i]);

		else
		{
			if(block)
			{
				if(buffer[i] == '*' && buffer[i+1] == '/')
				{
					comment = false;
					block = false;
					i++;
				}
			}
			else
			{
				if(buffer[i] == '\n')
				{
					comment = false;
					i++;
				}
			}

		}
	}

	/* close & free */
	fclose (fp);
	free (buffer);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 26th, 2006, 10:39 AM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I like the idea. Now, can you make a program that GENERATEs comments for me. Because really that's my problem right there
OpenLoop is offline   Reply With Quote
Old Apr 26th, 2006, 12:37 PM   #3
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
You took the words right out of my mouth.

Nice work.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Apr 26th, 2006, 2:32 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by OpenLoop
I like the idea. Now, can you make a program that GENERATEs comments for me. Because really that's my problem right there
Hehe well, I'm pretty sure there are some programs that generate comments for your functions. I haven't searched on google but I'm pretty sure there are a couple.

Anyway, I fixed a small bug, which happened when '\\' was in the code, because it wouldn't see '\\' but would see '\'', so that it would think the literal did not end.
/*
stripc - strips your comments.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

/* trouble in River City (TM DaWei) */
int uhOh(char * message)
{
	fprintf(stderr, "%s\n", message);
	return 1;
}

/* main */
int main(int argc, char ** argv)
{
	int i;
	bool comment = false, block = false, qliteral = false;
	FILE * fp;
	long fsize;
	char * buffer;
	char * file;

	/* check arguments and get filename */
	if(argc != 2)
		return uhOh("Usage: stripc <filename>");
	file = argv[1];

	/* open file */
	fp = fopen(file, "rb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* obtain file size */
	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	rewind(fp);

	/* allocate memory */
	buffer = malloc(fsize);
	if(buffer == NULL)
		return uhOh("Could not allocate memory.");

	/* copy file into buffer */
	fread(buffer, 1, fsize, fp);
	fclose(fp);
	fp = fopen(file, "wb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* loop through buffer */
	for(i = 0; i < fsize; i++)
	{
		if(!comment && !qliteral && (buffer[i] == '/' && (buffer[i+1] == '/' || buffer[i+1] == '*')))
		{
			comment = true;
			if(buffer[i+1] == '*')
				block = true;
			i++;
		}

		if(!comment && (buffer[i] == '"' || buffer[i] == '\''))
		{
			static bool squote = false, dblquote = false;
			if(buffer[i-1] != '\\' || buffer[i-2] != '\\')
				;
			else if(buffer[i] == '"' && !squote)
			{
				dblquote =! dblquote;
				qliteral =! qliteral;
			}
			else if(buffer[i] == '\'' && !dblquote)
			{
				squote =! squote;
				qliteral =! qliteral;
			}
		}

		if(!comment || qliteral)
                fprintf(fp, "%c", buffer[i]);

		else
		{
			if(block)
			{
				if(buffer[i] == '*' && buffer[i+1] == '/')
				{
					comment = false;
					block = false;
					i++;
				}
			}
			else
			{
				if(buffer[i] == '\n')
				{
					comment = false;
					i++;
				}
			}

		}
	}

	/* close & free */
	fclose(fp);
	free(buffer);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 27th, 2006, 3:46 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Two other minor bugs fixed.
When a line comment was present it skipped the first character and didn't print the newline.
/*
stripc - strips your comments.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

/* trouble in River City (TM DaWei) */
int uhOh(char * message)
{
	fprintf(stderr, "%s\n", message);
	return 1;
}

/* main */
int main(int argc, char ** argv)
{
	int i;
	bool comment = false, block = false, qliteral = false;
	FILE * fp;
	long fsize;
	char * buffer;
	char * file = "C:\\Documents and Settings\\adriaanseru\\My Documents\\My Code\\opengl\\opengl.cpp";

	/* check arguments and get filename */
	//if(argc != 2)
	//	return uhOh("Usage: stripc <filename>");
	//file = argv[1];

	/* open file */
	fp = fopen(file, "rb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* obtain file size */
	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	rewind(fp);

	/* allocate memory */
	buffer = malloc(fsize);
	if(buffer == NULL)
		return uhOh("Could not allocate memory.");

	/* copy file into buffer */
	fread(buffer, 1, fsize, fp);
	fclose(fp);
	fp = fopen(file, "wb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* loop through buffer */
	for(i = 0; i < fsize; i++)
	{
		if(!comment && !qliteral && (buffer[i] == '/' && (buffer[i+1] == '/' || buffer[i+1] == '*')))
		{
			comment = true;
			if(buffer[i+1] == '*')
				block = true;
			i++;
		}

		if(!comment && (buffer[i] == '"' || buffer[i] == '\''))
		{
			static bool squote = false, dblquote = false;
			if(buffer[i-1] != '\\' || buffer[i-2] != '\\')
				;
			else if(buffer[i] == '"' && !squote)
			{
				dblquote =! dblquote;
				qliteral =! qliteral;
			}
			else if(buffer[i] == '\'' && !dblquote)
			{
				squote =! squote;
				qliteral =! qliteral;
			}
		}

		if(comment && !qliteral)
		{
			if(block)
			{
				if(buffer[i] == '*' && buffer[i+1] == '/')
				{
					comment = false;
					block = false;
					i++;
				}
			}
			else
			{
				if(buffer[i] == '\n')
				{
					comment = false;
					fprintf(fp, "%c", '\n');
				}
			}
		}
		else
			fprintf(fp, "%c", buffer[i]);
	}

	/* close & free */
	fclose(fp);
	free(buffer);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 27th, 2006, 4:33 AM   #6
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Whoops my bad, left it to specify a file (debugging purposes).
Here the final version, 1.0:
/*
stripc 1.0 - strips your comments.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

/* trouble in River City (TM DaWei) */
int uhOh(char * message)
{
	fprintf(stderr, "%s\n", message);
	return 1;
}

/* main */
int main(int argc, char ** argv)
{
	int i;
	bool comment = false, block = false, qliteral = false;
	FILE * fp;
	long fsize;
	char * buffer;
	char * file;

	/* check arguments and get filename */
	if(argc != 2)
		return uhOh("Author: Ruben AKA Rubenisme(.com) AKA nnxion\nVersion: 1.0\nUsage: stripc <filename>\n");
	file = argv[1];

	/* open file */
	fp = fopen(file, "rb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* obtain file size */
	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	rewind(fp);

	/* allocate memory */
	buffer = malloc(fsize);
	if(buffer == NULL)
		return uhOh("Could not allocate memory.");

	/* copy file into buffer */
	fread(buffer, 1, fsize, fp);
	fclose(fp);
	fp = fopen(file, "wb");
	if(fp == NULL)
		return uhOh("Could not open file.");

	/* loop through buffer */
	for(i = 0; i < fsize; i++)
	{
		if(!comment && !qliteral && (buffer[i] == '/' && (buffer[i+1] == '/' || buffer[i+1] == '*')))
		{
			comment = true;
			if(buffer[i+1] == '*')
				block = true;
			i++;
		}

		if(!comment && (buffer[i] == '"' || buffer[i] == '\''))
		{
			static bool squote = false, dblquote = false;
			if(buffer[i-1] != '\\' || buffer[i-2] != '\\')
				;
			else if(buffer[i] == '"' && !squote)
			{
				dblquote =! dblquote;
				qliteral =! qliteral;
			}
			else if(buffer[i] == '\'' && !dblquote)
			{
				squote =! squote;
				qliteral =! qliteral;
			}
		}

		if(comment && !qliteral)
		{
			if(block)
			{
				if(buffer[i] == '*' && buffer[i+1] == '/')
				{
					comment = false;
					block = false;
					i++;
				}
			}
			else
			{
				if(buffer[i] == '\n')
				{
					comment = false;
					fprintf(fp, "%c", '\n');
				}
			}
		}
		else
			fprintf(fp, "%c", buffer[i]);
	}

	/* close & free */
	fclose(fp);
	free(buffer);
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 4th, 2006, 5:03 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Well I _REALLY_ thought it was finished, until I wanted to strip multiple files of comments... I also changed it so one can see which file fails if it does.
/*
stripc 1.0 - strips your comments.
Copyright (C) 2006 Ruben aka nnxion aka rubenisme (rubenisme.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <stdio.h>
#include <stdlib.h>

#define bool int
#define false 0
#define true 1

/* trouble in River City (TM DaWei) */
int uhOh(char * message)
{
	fprintf(stderr, "%s\n", message);
	return 1;
}

/* main */
int main(int argc, char ** argv)
{
	int i;
	bool comment = false, block = false, qliteral = false;
	FILE * fp;
	long fsize;
	char * buffer;

	/* check arguments and get filename */
	if (argc == 1)
	   return uhOh("Author: Ruben AKA Rubenisme(.com) AKA nnxion\nVersion: 1.0\nUsage: stripc <filename(s)>\n");
	else
		while(--argc > 0)
		{
			/* open file */
			if((fp = fopen(*++argv, "rb")) == NULL)
			{
				fprintf(stderr, "stripc: could not open file %s for reading\n", *argv);
				return 1;
			}

			/* obtain file size */
			fseek(fp, 0, SEEK_END);
			fsize = ftell(fp);
			rewind(fp);

			/* allocate memory */
			if((buffer = malloc(fsize)) == NULL)
				return uhOh("Could not allocate memory.");

			/* copy file into buffer */
			fread(buffer, 1, fsize, fp);
			fclose(fp);
			if((fp = fopen(*argv, "wb")) == NULL)
			{
				fprintf(stderr, "stripc: could not open file %s for writing\n", *argv);
				return 1;
			}

			/* loop through buffer */
			for(i = 0; i < fsize; i++)
			{
				if(!comment && !qliteral && (buffer[i] == '/' && (buffer[i+1] == '/' || buffer[i+1] == '*')))
				{
					comment = true;
					if(buffer[i+1] == '*')
						block = true;
					i++;
				}

				if(!comment && (buffer[i] == '"' || buffer[i] == '\''))
				{
					static bool squote = false, dblquote = false;
					if(buffer[i-1] != '\\' || buffer[i-2] != '\\')
						;
					else if(buffer[i] == '"' && !squote)
					{
						dblquote =! dblquote;
						qliteral =! qliteral;
					}
					else if(buffer[i] == '\'' && !dblquote)
					{
						squote =! squote;
						qliteral =! qliteral;
					}
				}

				if(comment && !qliteral)
				{
					if(block)
					{
						if(buffer[i] == '*' && buffer[i+1] == '/')
						{
							comment = false;
							block = false;
							i++;
						}
					}
					else
					{
						if(buffer[i] == '\n')
						{
							comment = false;
							fprintf(fp, "%c", '\n');
						}
					}
				}
				else
					fprintf(fp, "%c", buffer[i]);
			}

			/* close & free */
			fclose(fp);
			free(buffer);
		}
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 4th, 2006, 5:25 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,031
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by nnxion
Well I had a file which had a load of comments, and I wanted to get rid of them fast. So I made this program. Please comment for any enhancements or anything else.
Interesting, but wouldn't it be easier to just run it through the preprocessor with the appropriate switches? And yes, before you ask, I realize they are not actually removed, but are replaced with whitespace, but that should be good enough, right?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old May 4th, 2006, 6:04 AM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by lectricpharaoh
Interesting, but wouldn't it be easier to just run it through the preprocessor with the appropriate switches? And yes, before you ask, I realize they are not actually removed, but are replaced with whitespace, but that should be good enough, right?
Yeah I guess that would be okay too, but the VC++ compiler will complain:
Quote:
la.cpp(4) : fatal error C1083: Can not open include file: 'iostream': No such file or directory
So I have to specify where it can find them.

It would also preprocess (read: remove) my macros and if I included headers it might really include them in the file.

And lastly if I'm somewhere without a compiler, and would still like to look at some code, but's it's so heavily commented that I can't see the forest for the trees, then I'd be stuck.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC