I'm working on this code that I'm hoping can take a C file and rearrange bad indentation, ignore comments, remove whitespace etc to get it to an agreed format.
Heres what I have so far:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *check=0;
char ch=!EOF;
char* compare="int main";
int i, c, a, indent;
FILE *input;
FILE *output;
void Print()
{
fprintf(output, "%c", ch);
}
void Check()
{
check = realloc( check, i+1 );
check[i] = fgetc(input);
ch = check[i];
if(ch == '\t')
{
Check();
}
}
void EndofLine()
{
for(i = i; ch != '\n'; i++)
{
check = realloc( check, i+1 );
check[i] = fgetc(input);
ch = check[i];
fprintf(output, "%c", ch);
}
}
void EndofComment()
{
i++;
check = realloc( check, i+1 );
check[i] = fgetc(input);
ch = check[i];
if(ch == '*')
{
Print();
EndofLine();
}else{
i--;
}
}
void MainCheck()
{
i++;
check[i] = '\0';
int strcmp(
const char *check,
const char *compare
);
i--;
}
int main(int argc, char *argv[])
{
input = fopen(argv[1], "r");
output = fopen("output.c", "w");
for(i = 0; ch != EOF; i++)
{
Check();
if(ch == '/')
{
Print();
EndofComment();
}
if(ch == 'i')
{
MainCheck();
}
if(a > 0)
{
if((ch != '\n') || (ch != '\r'))
{
fprintf(output, "\n");
a--;
}
}
if(ch=='{')
{
a++;
}
if((ch=='}')&&(c != 0))
{
fprintf(output, "\n");
a--;
}
if(ch == ';')
{
c++;
}else{
c = 0;
}
Print();
}
fclose(input);
fclose(output);
return 0;
}
I can't help but think I'm going about it in the wrong way. I'm currently reading it character by character but what I need to be able to do is see what the character before and after it is to impose my rules.
Thanks in Advance.