-
Jun 14th, 2020, 02:33 PM
#1
Thread Starter
Fanatic Member
Programming Language Comment Remover Beta
Hi,
This is a small project I made today it allows you to remove comments from C/C++ , Python and Basic.
At the moment it only removes single comments, and I hope to add more features soon hope you find it useful.
Code:
// File stripc.c
// By Ben
// Date 14/06/2020
// Info strips single line comments from C/C++ Python and Basic
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX_LINE 2048
enum TLANG{
C=0,
PY=1,
BASIC = 2
}Lang_Type;
int IsComment(char *s){
int x = 0;
int y = 0;
char temp[MAX_LINE];
while(x < strlen(s)){
//Skip first write spaces
if(s[x] != ' ' && s[x] != '\t'){
break;
}
x++;
}
//Copy from were the start white spaces were found to the end of the string
while(x < strlen(s)){
temp[y] = s[x];
x++;
y++;
}
//Zap temp
temp[y] = '\0';
if(strlen(temp) > 1){
if(Lang_Type == C){
//Check for single line comments
if(temp[0] == '/' && temp[1] == '/'){
return 1;
}
}
//Python
if(Lang_Type == PY){
//Check for single line comments
if(temp[0] == '#'){
return 1;
}
}
//Basic
//Python
if(Lang_Type == BASIC){
//Check for single line comments
if(temp[0] == '\''){
return 1;
}
}
}
memset(temp,0,sizeof temp);
return 0;
}
int main(int argc, char*argv[])
{
FILE *fin = NULL;
FILE *fout = NULL;
char lnBuffer[MAX_LINE];
char lang = 'C';
int len = 0;
if(argc != 4){
printf("Strip Comments Version 1.0\n");
printf("USE %s -Lang InputFile OutputFile\n",argv[0]);
printf("Lanuage Flags:\n");
printf("-C C/C++\n");
printf("-P Python\n");
printf("-B Basic");
exit(0);
}
//Get language type.
lang = toupper(argv[1][1]);
//language C/C++
if(lang == 'C'){
Lang_Type = C;
}
//language Python
if(lang == 'P'){
Lang_Type = PY;
}
//language Basic
if(lang == 'B'){
Lang_Type = BASIC;
}
//Open input file.
fin = fopen(argv[2],"r");
//Check input file was opened.
if(!fin){
printf("Error Reading Input File: %s\n",argv[1]);
exit(0);
}
//Open output file.
fout = fopen(argv[3],"w");
//Check output file was opened.
if(!fout){
printf("Error Cannot Open Output File: %s",argv[2]);
fclose(fin);
exit(0);
}
//While not end of source file read in a line
while(fgets(lnBuffer,sizeof lnBuffer,fin) != NULL){
//Get line length
len = strlen(lnBuffer);
//Zap line feeds
if((lnBuffer[len-1] = '\r')){
lnBuffer[len-1] = '\0';
}
//Check for comments.
if(IsComment(lnBuffer) != 1){
//Check length
if(len > 0){
//Write line to output file
fputs(lnBuffer,fout);
//Write new line
fputs("\n",fout);
}
}
}
//Close input and output files.
fclose(fout);
fclose(fin);
//Clear up
memset(lnBuffer,0,sizeof lnBuffer);
return 0;
}
Compiling the project
Here is an example of removing comments from C source file.
stripc -C example.c example-clean.c
Last edited by BenJones; Jun 14th, 2020 at 02:34 PM.
Reason: Spelling Correction
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|