Advertisement
vencinachev

Course project C

Dec 24th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // states for source code trimming
  6. enum { START, COMMENT_START, COMMENT, COMMENT_END, LINE_COMMENT, STRING, STRING_ESCAPE, CHAR, CHAR_ESCAPE };
  7.  
  8. // states for identifiers
  9. enum { OUT, IDENT };
  10.  
  11. void sourceCode(FILE* program, FILE* destination);
  12. int countSubstr(char* text, char *word);
  13. int countInFile(FILE* program, char* phrase);
  14. int countIdentificators(FILE* program);
  15. void fromFileToFile();
  16. void fromConsoleToFile();
  17. void fromConsoleToConsole();
  18. void fromFileToConsole();
  19.  
  20. int main()
  21. {
  22.     char option;
  23.     printf(" --> Program analysis! <-- \n");
  24.     while(1)
  25.     {
  26.         printf("\n1. From file to file\n");
  27.         printf("2. From file to console\n");
  28.         printf("3. From console to file\n");
  29.         printf("4. From console to console\n");
  30.         printf("5. Exit\n");
  31.         printf("Enter option: ");
  32.         option = getch();
  33.  
  34.         switch (option)
  35.         {
  36.             case '1': fromFileToFile(); break;
  37.             case '2': fromFileToConsole(); break;
  38.             case '3': fromConsoleToFile(); break;
  39.             case '4': fromConsoleToConsole(); break;
  40.             case '5': return 0; break;
  41.             default: printf("Invalid option! Try again...\n");
  42.         }
  43.     }
  44.     return 1;
  45. }
  46.  
  47.  
  48. void sourceCode(FILE* program, FILE* destination)
  49. {
  50.     int state = START;
  51.     int c;
  52.     while ((c = fgetc(program)) != EOF)
  53.     {
  54.         switch(state)
  55.         {
  56.             case START:
  57.                 if (c == '/')
  58.                 {
  59.                     state = COMMENT_START;
  60.                 }
  61.                 else if (c == '"')
  62.                 {
  63.                     state = STRING;
  64.                 }
  65.                 else if (c == '\'')
  66.                 {
  67.                     state = CHAR;
  68.                 }
  69.                 break;
  70.             case COMMENT_START:
  71.                 if (c == '/')
  72.                 {
  73.                     state = LINE_COMMENT;
  74.                 }
  75.                 else if (c == '*')
  76.                 {
  77.                     state = COMMENT;
  78.                 }
  79.                 else
  80.                 {
  81.                     state = START;
  82.                 }
  83.                 continue;
  84.                 break;
  85.             case COMMENT:
  86.                 if (c == '*')
  87.                 {
  88.                     state = COMMENT_END;
  89.                 }
  90.                 continue;
  91.                 break;
  92.             case COMMENT_END:
  93.                 if (c == '/')
  94.                 {
  95.                     fputc('\n', destination);
  96.                     state = START;
  97.                 }
  98.                 else if (c == '*')
  99.                 {
  100.                     state = COMMENT_END;
  101.                 }
  102.                 else
  103.                 {
  104.                     state = COMMENT;
  105.                 }
  106.                 continue;
  107.                 break;
  108.             case STRING:
  109.                 if (c == '"')
  110.                 {
  111.                     state = START;
  112.                 }
  113.                 else if (c == '\\')
  114.                 {
  115.                     state = STRING_ESCAPE;
  116.                 }
  117.                 continue;
  118.                 break;
  119.             case CHAR:
  120.                 if (c == '\'')
  121.                 {
  122.                     state = START;
  123.                 }
  124.                 else if (c == '\\')
  125.                 {
  126.                     state = CHAR_ESCAPE;
  127.                 }
  128.                 continue;
  129.                 break;
  130.             case CHAR_ESCAPE:
  131.                 state = CHAR;
  132.                 continue;
  133.                 break;
  134.             case STRING_ESCAPE:
  135.                 state = STRING;
  136.                 continue;
  137.                 break;
  138.             case LINE_COMMENT:
  139.                 if (c == '\n')
  140.                 {
  141.                     state = START;
  142.                 }
  143.                 continue;
  144.                 break;
  145.         }
  146.         if (state == START)
  147.         {
  148.             fputc(c, destination);
  149.         }
  150.     }
  151. }
  152.  
  153. int countSubstr(char* text, char *word)
  154. {
  155.     int textL = strlen(text);
  156.     int wordL = strlen(word);
  157.     int cnt = 0;
  158.     for (int i = 0; i <= textL - wordL; i++)
  159.     {
  160.         if (!strncmp(word, text, wordL))
  161.         {
  162.             cnt++;
  163.         }
  164.         text++;
  165.     }
  166.     return cnt;
  167. }
  168.  
  169. int countInFile(FILE* program, char* phrase)
  170. {
  171.     char buffer[300];
  172.     int cnt = 0;
  173.     while (fgets(buffer, 200, program))
  174.     {
  175.         cnt += countSubstr(buffer, phrase);
  176.     }
  177.     return cnt;
  178. }
  179.  
  180. int countIdentificators(FILE* program)
  181. {
  182.     int cnt = 0, c;
  183.     int state = OUT;
  184.     while ((c = fgetc(program)) != EOF)
  185.     {
  186.         switch (state)
  187.         {
  188.             case OUT:
  189.                 if (isalpha(c) || c == '_')
  190.                 {
  191.                     state = IDENT;
  192.                 }
  193.                 break;
  194.             case IDENT:
  195.                 if (!isalpha(c) && !isdigit(c) && c != '_')
  196.                 {
  197.                     state = OUT;
  198.                     cnt++;
  199.                 }
  200.                 break;
  201.         }
  202.     }
  203.     return cnt;
  204. }
  205.  
  206. void fromFileToFile()
  207. {
  208.     FILE *fr, *fw, *ftemp;
  209.     char frname[200], fwname[200];
  210.     printf("\nEnter program file name: ");
  211.     scanf("%s", frname);
  212.     if (!(fr = fopen(frname, "r")))
  213.     {
  214.         printf("Problem opening file!");
  215.         return;
  216.     }
  217.     printf("Enter output file name: ");
  218.     scanf("%s", fwname);
  219.     if (!(fw = fopen(fwname, "w")))
  220.     {
  221.         printf("Problem opening file!");
  222.         return;
  223.     }
  224.  
  225.     ftemp = fopen("temp.txt", "w+");
  226.     sourceCode(fr, ftemp);
  227.     rewind(ftemp);
  228.     fprintf(fw, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
  229.     rewind(ftemp);
  230.     fprintf(fw, "Count of identifiers: %d\n", countIdentificators(ftemp));
  231.     fclose(fr);
  232.     fclose(fw);
  233.     fclose(ftemp);
  234. }
  235.  
  236. void fromConsoleToFile()
  237. {
  238.     FILE *fconsole, *fw, *ftemp;
  239.     int c;
  240.     char fwname[200];
  241.     printf("\nEnter output file name: ");
  242.     scanf("%s", fwname);
  243.     if (!(fw = fopen(fwname, "w")))
  244.     {
  245.         printf("Problem opening file!");
  246.         return;
  247.     }
  248.     fconsole = fopen("console.txt", "w+");
  249.     printf("\nEnter program:\n");
  250.     while((c = getchar()) != EOF)
  251.     {
  252.         fputc(c, fconsole);
  253.     }
  254.     rewind(fconsole);
  255.     ftemp = fopen("temp.txt", "w+");
  256.     sourceCode(fconsole, ftemp);
  257.     rewind(ftemp);
  258.     fprintf(fw, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
  259.     rewind(ftemp);
  260.     fprintf(fw, "Count of identifiers: %d\n", countIdentificators(ftemp));
  261.     fclose(fw);
  262.     fclose(fconsole);
  263.     fclose(ftemp);
  264. }
  265.  
  266. void fromConsoleToConsole()
  267. {
  268.     FILE *fconsole, *ftemp;
  269.     int c;
  270.     fconsole = fopen("console.txt", "w+");
  271.     printf("\nEnter program:\n");
  272.     while((c = getchar()) != EOF)
  273.     {
  274.         fputc(c, fconsole);
  275.     }
  276.     rewind(fconsole);
  277.     ftemp = fopen("temp.txt", "w+");
  278.     sourceCode(fconsole, ftemp);
  279.     rewind(ftemp);
  280.     fprintf(stdout, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
  281.     rewind(ftemp);
  282.     fprintf(stdout, "Count of identifiers: %d\n", countIdentificators(ftemp));
  283.     fclose(fconsole);
  284.     fclose(ftemp);
  285. }
  286.  
  287. void fromFileToConsole()
  288. {
  289.     FILE *fr, *ftemp;
  290.     char frname[200], fwname[200];
  291.     printf("\nEnter program file name: ");
  292.     scanf("%s", frname);
  293.     if (!(fr = fopen(frname, "r")))
  294.     {
  295.         printf("\nProblem opening file!");
  296.         return;
  297.     }
  298.     ftemp = fopen("temp.txt", "w+");
  299.     sourceCode(fr, ftemp);
  300.     rewind(ftemp);
  301.     fprintf(stdout, "Count of '==' operators: %d\n", countInFile(ftemp, "=="));
  302.     rewind(ftemp);
  303.     fprintf(stdout, "Count of identifiers: %d\n", countIdentificators(ftemp));
  304.     fclose(fr);
  305.     fclose(ftemp);
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement