Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // states for source code trimming
- enum { START, COMMENT_START, COMMENT, COMMENT_END, LINE_COMMENT, STRING, STRING_ESCAPE, CHAR, CHAR_ESCAPE };
- // states for identifiers
- enum { OUT, IDENT };
- void sourceCode(FILE* program, FILE* destination);
- int countSubstr(char* text, char *word);
- int countInFile(FILE* program, char* phrase);
- int countIdentificators(FILE* program);
- void fromFileToFile();
- void fromConsoleToFile();
- void fromConsoleToConsole();
- void fromFileToConsole();
- int main()
- {
- char option;
- printf(" --> Program analysis! <-- \n");
- while(1)
- {
- printf("\n1. From file to file\n");
- printf("2. From file to console\n");
- printf("3. From console to file\n");
- printf("4. From console to console\n");
- printf("5. Exit\n");
- printf("Enter option: ");
- option = getch();
- switch (option)
- {
- case '1': fromFileToFile(); break;
- case '2': fromFileToConsole(); break;
- case '3': fromConsoleToFile(); break;
- case '4': fromConsoleToConsole(); break;
- case '5': return 0; break;
- default: printf("Invalid option! Try again...\n");
- }
- }
- return 1;
- }
- void sourceCode(FILE* program, FILE* destination)
- {
- int state = START;
- int c;
- while ((c = fgetc(program)) != EOF)
- {
- switch(state)
- {
- case START:
- if (c == '/')
- {
- state = COMMENT_START;
- }
- else if (c == '"')
- {
- state = STRING;
- }
- else if (c == '\'')
- {
- state = CHAR;
- }
- break;
- case COMMENT_START:
- if (c == '/')
- {
- state = LINE_COMMENT;
- }
- else if (c == '*')
- {
- state = COMMENT;
- }
- else
- {
- state = START;
- }
- continue;
- break;
- case COMMENT:
- if (c == '*')
- {
- state = COMMENT_END;
- }
- continue;
- break;
- case COMMENT_END:
- if (c == '/')
- {
- fputc('\n', destination);
- state = START;
- }
- else if (c == '*')
- {
- state = COMMENT_END;
- }
- else
- {
- state = COMMENT;
- }
- continue;
- break;
- case STRING:
- if (c == '"')
- {
- state = START;
- }
- else if (c == '\\')
- {
- state = STRING_ESCAPE;
- }
- continue;
- break;
- case CHAR:
- if (c == '\'')
- {
- state = START;
- }
- else if (c == '\\')
- {
- state = CHAR_ESCAPE;
- }
- continue;
- break;
- case CHAR_ESCAPE:
- state = CHAR;
- continue;
- break;
- case STRING_ESCAPE:
- state = STRING;
- continue;
- break;
- case LINE_COMMENT:
- if (c == '\n')
- {
- state = START;
- }
- continue;
- break;
- }
- if (state == START)
- {
- fputc(c, destination);
- }
- }
- }
- int countSubstr(char* text, char *word)
- {
- int textL = strlen(text);
- int wordL = strlen(word);
- int cnt = 0;
- for (int i = 0; i <= textL - wordL; i++)
- {
- if (!strncmp(word, text, wordL))
- {
- cnt++;
- }
- text++;
- }
- return cnt;
- }
- int countInFile(FILE* program, char* phrase)
- {
- char buffer[300];
- int cnt = 0;
- while (fgets(buffer, 200, program))
- {
- cnt += countSubstr(buffer, phrase);
- }
- return cnt;
- }
- int countIdentificators(FILE* program)
- {
- int cnt = 0, c;
- int state = OUT;
- while ((c = fgetc(program)) != EOF)
- {
- switch (state)
- {
- case OUT:
- if (isalpha(c) || c == '_')
- {
- state = IDENT;
- }
- break;
- case IDENT:
- if (!isalpha(c) && !isdigit(c) && c != '_')
- {
- state = OUT;
- cnt++;
- }
- break;
- }
- }
- return cnt;
- }
- void fromFileToFile()
- {
- FILE *fr, *fw, *ftemp;
- char frname[200], fwname[200];
- printf("\nEnter program file name: ");
- scanf("%s", frname);
- if (!(fr = fopen(frname, "r")))
- {
- printf("Problem opening file!");
- return;
- }
- printf("Enter output file name: ");
- scanf("%s", fwname);
- if (!(fw = fopen(fwname, "w")))
- {
- printf("Problem opening file!");
- return;
- }
- ftemp = fopen("temp.txt", "w+");
- sourceCode(fr, ftemp);
- rewind(ftemp);
- fprintf(fw, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
- rewind(ftemp);
- fprintf(fw, "Count of identifiers: %d\n", countIdentificators(ftemp));
- fclose(fr);
- fclose(fw);
- fclose(ftemp);
- }
- void fromConsoleToFile()
- {
- FILE *fconsole, *fw, *ftemp;
- int c;
- char fwname[200];
- printf("\nEnter output file name: ");
- scanf("%s", fwname);
- if (!(fw = fopen(fwname, "w")))
- {
- printf("Problem opening file!");
- return;
- }
- fconsole = fopen("console.txt", "w+");
- printf("\nEnter program:\n");
- while((c = getchar()) != EOF)
- {
- fputc(c, fconsole);
- }
- rewind(fconsole);
- ftemp = fopen("temp.txt", "w+");
- sourceCode(fconsole, ftemp);
- rewind(ftemp);
- fprintf(fw, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
- rewind(ftemp);
- fprintf(fw, "Count of identifiers: %d\n", countIdentificators(ftemp));
- fclose(fw);
- fclose(fconsole);
- fclose(ftemp);
- }
- void fromConsoleToConsole()
- {
- FILE *fconsole, *ftemp;
- int c;
- fconsole = fopen("console.txt", "w+");
- printf("\nEnter program:\n");
- while((c = getchar()) != EOF)
- {
- fputc(c, fconsole);
- }
- rewind(fconsole);
- ftemp = fopen("temp.txt", "w+");
- sourceCode(fconsole, ftemp);
- rewind(ftemp);
- fprintf(stdout, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
- rewind(ftemp);
- fprintf(stdout, "Count of identifiers: %d\n", countIdentificators(ftemp));
- fclose(fconsole);
- fclose(ftemp);
- }
- void fromFileToConsole()
- {
- FILE *fr, *ftemp;
- char frname[200], fwname[200];
- printf("\nEnter program file name: ");
- scanf("%s", frname);
- if (!(fr = fopen(frname, "r")))
- {
- printf("\nProblem opening file!");
- return;
- }
- ftemp = fopen("temp.txt", "w+");
- sourceCode(fr, ftemp);
- rewind(ftemp);
- fprintf(stdout, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
- rewind(ftemp);
- fprintf(stdout, "Count of identifiers: %d\n", countIdentificators(ftemp));
- fclose(fr);
- fclose(ftemp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement