#include <stdio.h>
#include <string.h>
FILE *in;
FILE *out;
void fixTheFile(FILE *in,FILE*out){
int isInQuote = 0;
char prevChar = ' ';
char temp = ' ';
int havingSpace=0;
int isInScope = 0;
int levelOfScope = 0;
while(true)
{
if(temp!=' ') prevChar=temp;
fscanf(in,"%c",&temp);
if(feof(in)) break;
if(temp == ' ') havingSpace++;
else havingSpace = 0;
if(havingSpace > 1) continue;
if(temp == '"'){
isInQuote = (isInQuote == 0)? 1:0;
}
if(temp == '('){
isInScope = 1;
}
if(temp == ')'){
isInScope = 0;
}
if(temp == '{'){
levelOfScope++;
}
if(temp == '}'){
levelOfScope--;
}
if(isInQuote == 0 && prevChar == '>'){
if(temp==' '){
fprintf(out,"\n");
printf("\n");
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else if(isInQuote == 0 && (prevChar == '{' || prevChar=='}')){
if(temp==' '){
fprintf(out,"\n");
printf("\n");
for(int i=0;i<levelOfScope;i++){
fprintf(out,"\t");
printf("\t");
}
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else if(isInQuote == 0 && prevChar == ';' && isInScope == 0){
if(temp==' '){
fprintf(out,"\n\t");
printf("\n\t");
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else{
fprintf(out,"%c",temp);
printf("%c",temp);
}
}
}
void clear(){
for(int i=0;i<25;i++) printf("\n");
}
//This code can fix, awful code to become clean.
// i call it C beautifier.
// :]
// p.s: I hate awful code.
int main(int argc, char *argv[]){
char filename[9999] = "input.cpp";
printf("Created by sky.henry@live.com\n");
if(argc > 1){
strcpy(filename,argv[1]);
}
clear();
printf("Drag any file to the EXE\n");
printf("Press any key to continue\n\n");
getchar();
clear();
printf("Read file %s\n",filename);
in=fopen(filename,"r");
out = fopen("output.cpp","w");
if(in)
{
fixTheFile(in,out);
fclose(in);
fclose(out);
}else{
printf("File %s not found",filename);
}
getchar();
return 0;
}
#include <string.h>
FILE *in;
FILE *out;
void fixTheFile(FILE *in,FILE*out){
int isInQuote = 0;
char prevChar = ' ';
char temp = ' ';
int havingSpace=0;
int isInScope = 0;
int levelOfScope = 0;
while(true)
{
if(temp!=' ') prevChar=temp;
fscanf(in,"%c",&temp);
if(feof(in)) break;
if(temp == ' ') havingSpace++;
else havingSpace = 0;
if(havingSpace > 1) continue;
if(temp == '"'){
isInQuote = (isInQuote == 0)? 1:0;
}
if(temp == '('){
isInScope = 1;
}
if(temp == ')'){
isInScope = 0;
}
if(temp == '{'){
levelOfScope++;
}
if(temp == '}'){
levelOfScope--;
}
if(isInQuote == 0 && prevChar == '>'){
if(temp==' '){
fprintf(out,"\n");
printf("\n");
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else if(isInQuote == 0 && (prevChar == '{' || prevChar=='}')){
if(temp==' '){
fprintf(out,"\n");
printf("\n");
for(int i=0;i<levelOfScope;i++){
fprintf(out,"\t");
printf("\t");
}
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else if(isInQuote == 0 && prevChar == ';' && isInScope == 0){
if(temp==' '){
fprintf(out,"\n\t");
printf("\n\t");
}else{
fprintf(out,"\n%c",temp);
printf("\n%c",temp);
}
prevChar = '\n ';
}else{
fprintf(out,"%c",temp);
printf("%c",temp);
}
}
}
void clear(){
for(int i=0;i<25;i++) printf("\n");
}
//This code can fix, awful code to become clean.
// i call it C beautifier.
// :]
// p.s: I hate awful code.
int main(int argc, char *argv[]){
char filename[9999] = "input.cpp";
printf("Created by sky.henry@live.com\n");
if(argc > 1){
strcpy(filename,argv[1]);
}
clear();
printf("Drag any file to the EXE\n");
printf("Press any key to continue\n\n");
getchar();
clear();
printf("Read file %s\n",filename);
in=fopen(filename,"r");
out = fopen("output.cpp","w");
if(in)
{
fixTheFile(in,out);
fclose(in);
fclose(out);
}else{
printf("File %s not found",filename);
}
getchar();
return 0;
}
Tidak ada komentar:
Posting Komentar