![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
I like the idea. Now, can you make a program that GENERATEs comments for me. Because really that's my problem right there
![]() |
|
|
|
|
|
#3 | |
|
Professional Programmer
|
You took the words right out of my mouth.
Nice work.
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]() 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 |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#8 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,193
Rep Power: 5
![]() |
Quote:
__________________
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 |
|
|
|
|
|
|
#9 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
Quote:
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 |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|