-
Notifications
You must be signed in to change notification settings - Fork 1
/
o.c
5 lines (5 loc) · 3.1 KB
/
o.c
1
2
3
4
5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *file_read(char *filepath){FILE*file=fopen(filepath,"r");if(file==NULL){printf("Error: File not found\n");exit(1);}fseek(file,0,SEEK_END);long size=ftell(file);rewind(file);char *buffer=(char *)malloc(sizeof(char )*(size+1));if(buffer==NULL){printf("Error: Memory allocation failed\n");exit(1);}fread(buffer,1,size,file);buffer[size]='\0';fclose(file);return(buffer);}char *remove_comments(char *buffer){char *new_buffer=(char *)malloc(sizeof(char )*(strlen(buffer)+1));if(new_buffer==NULL){printf("Error: Memory allocation failed\n");exit(1);}int i=0;int j=0;while(buffer[i]!='\0'){if(buffer[i]=='/' &&buffer[i+1]=='/'){while(buffer[i]!='\n'){i++;}}else if(buffer[i]=='/' &&buffer[i+1]=='*'){i+=2;while(buffer[i]!='*' ||buffer[i+1]!='/'){i++;}i+=2;}else {new_buffer[j]=buffer[i];j++;i++;}}new_buffer[j]='\0';return(new_buffer);}char *minify_c_code(char *code){char *new_code=(char *)malloc(sizeof(char )*(strlen(code)+1));if(new_code==NULL){printf("Error: Memory allocation failed\n");exit(1);}int i=0;int j=0;while(code[i]!='\0'){if(isalpha(code[i])||isdigit(code[i])||code[i]=='_'){char *temp=(char *)malloc(sizeof(char )*(52));int temp_i=0;while(isalpha(code[i])||isdigit(code[i])||code[i]=='_'){temp[temp_i++]=code[i];i++;}temp[temp_i]='\0';for(int k=0;k<temp_i;k++){new_code[j]=temp[k];j++;}if(strcmp(temp,"typedef")==0||strcmp(temp,"return")==0||strcmp(temp,"sizeof")==0||strcmp(temp,"int")==0||strcmp(temp,"long")==0||strcmp(temp,"if")==0||strcmp(temp,"else")==0||strcmp(temp,"float")==0||strcmp(temp,"double")==0||strcmp(temp,"char")==0||strcmp(temp,"bool")==0){while(code[i]==' ' ||code[i]=='\t' ||code[i]=='\n' ||code[i]=='\r')i++;if(code[i]!='(')new_code[j++]=' ';}else {while(code[i]==' ' ||code[i]=='\t' ||code[i]=='\n' ||code[i]=='\r'){i++;}if(isalpha(code[i])||isdigit(code[i])||code[i]=='_'){new_code[j++]=' ';new_code[j++]=code[i];i++;}}free(temp);}else if(code[i]=='#'){while(code[i]!='\n'){new_code[j]=code[i];i++;j++;}new_code[j]='\n';j++;}else if(code[i]==' ' ||code[i]=='\n' ||code[i]=='\r' ||code[i]=='\t'){i++;}else if(code[i]=='{' ||code[i]=='}'){new_code[j]=code[i];j++;i++;}else if(code[i]==';'){new_code[j]=code[i];j++;i++;while(code[i]==' ' ||code[i]=='\n' ||code[i]=='\t'){i++;}}else if(code[i]=='(' ||code[i]==')'){new_code[j]=code[i];j++;i++;}else if(code[i]==','){new_code[j]=code[i];j++;i++;while(code[i]==' ' ||code[i]=='\n' ||code[i]=='\t'){i++;}}else {if(code[i]=='\''){new_code[j]=code[i];j++;i++;while(code[i]!='\''){new_code[j]=code[i];j++;i++;}new_code[j]=code[i];j++;i++;}if(code[i]=='"'){new_code[j]=code[i];i++;j++;while(code[i]!='"'){new_code[j]=code[i];j++;i++;}new_code[j]=code[i];j++;i++;}else {new_code[j]=code[i];j++;i++;}}}new_code[j]='\0';return(new_code);}int main(int argc,char **argv){if(argc==1){printf("Usage: %s <file>\n",argv[0]);return 1;}char *buffer=file_read(argv[1]);buffer=remove_comments(buffer);buffer=minify_c_code(buffer);if(argc==3){FILE*file=fopen(argv[2],"w");if(file==NULL){printf("Error: Cannot create '%s' file!\n",argv[2]);exit(1);}fprintf(file,"%s",buffer);fclose(file);}else {printf("%s",buffer);}return 0;}