Skip to content

Instantly share code, notes, and snippets.

@mudream4869
Last active August 29, 2015 13:58
Show Gist options
  • Save mudream4869/9970114 to your computer and use it in GitHub Desktop.
Save mudream4869/9970114 to your computer and use it in GitHub Desktop.
ruby indent
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char uppre[10][10] = {"module","class","def","if","else","unless","for","when","case","do"};
const int UP_MAX = 10;
char downpre[3][10] = {"end","else","when"};
const int DOWN_MAX = 3 ;
bool isdown(char *str)
{
if((str[0] == '#') or (str[1] == '#')) return false;
if((str[0] == '\n') or (str[1] == '\n')) return false;
char get[100];
sscanf(str,"%s",get);
for(int lx = 0;lx < DOWN_MAX;lx++)
{
if(strcmp(get,downpre[lx]) == 0)
return true;
}
int sl = strlen(str);
if(sl > 4)
{
if((str[sl - 4] == 'e') and (str[sl - 3] == 'n') and (str[sl - 2] == 'd'))
return true;
}
return false;
}
bool isup(char *str)
{
if((str[0] == '#') or (str[1] == '#')) return false;
if((str[0] == '\n') or (str[1] == '\n')) return false;
char get[100];
sscanf(str,"%s",get);
for(int lx = 0;lx < UP_MAX;lx++)
{
if(strcmp(get,uppre[lx]) == 0)
return true;
}
char *tokptr;
tokptr = strtok(str," \t");
while(tokptr != NULL)
{
//printf("get token: [%s]\n",tokptr);
for(int lx = 0;lx < UP_MAX;lx++)
if(strcmp(tokptr,uppre[lx]) == 0)
return true;
tokptr = strtok(NULL," \t");
}
return false;
}
int main(int argc,char* argv[])
{
int level = 0;
if(argc < 3)
{
printf("Parameter isn't enough.\n");
exit(1);
}
FILE *fin = fopen(argv[1],"r");
FILE *fout = fopen(argv[2],"w");
char string[200];
int linecnt = 0;
if(fin == NULL)
{
printf("Read Null file?\n");
exit(1);
}
while(fgets(string,100,fin) != NULL)
{
linecnt++;
//printf("%s",string);
if(isdown(string))
{
//puts("[[ DOWN");
level -= 1;
if(level < 0)
{
printf("Line %04d Warning: Indent Error.\n",linecnt);
level = 0;
}
}
for(int lx = 0;lx < level;lx++)
fprintf(fout,"\t");
fprintf(fout,"%s",string);
if(isup(string))
{
//puts("[[ UP");
level += 1;
}
}
fclose(fin);
fclose(fout);
return 0;
}
@mudream4869
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment