Advertisement
michciu

c prj newest

Dec 14th, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define PCRE2_CODE_UNIT_WIDTH 8
  5.  
  6. #include <pcre2.h>
  7.  
  8. #define ANSI_COLOR_RED     "\x1b[31m"
  9. #define ANSI_COLOR_GREEN   "\x1b[32m"
  10. #define ANSI_COLOR_YELLOW  "\x1b[33m"
  11. #define ANSI_COLOR_BLUE    "\x1b[34m"
  12. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  13. #define ANSI_COLOR_CYAN    "\x1b[36m"
  14. #define ANSI_COLOR_RESET   "\x1b[0m"
  15.  
  16. void cleanFile(void);
  17.  
  18. char *checkLineWithRegex(char *pattern, char *subject, char *replacement);
  19.  
  20. void finalizeCleaning(void);
  21.  
  22. void filterLine(char *line, char *result, int runCount);
  23.  
  24.  
  25. FILE *inputFile;
  26. FILE *outputFile;
  27. FILE *resultFile;
  28. PCRE2_UCHAR *returnLine;
  29.  
  30. char PLIK_TXT[70];
  31.  
  32.  
  33. char paternDeletWholeLine[] =
  34.         "^(?>\\s*)\\\\(?>hline|centerline|medskip|noindent|documentclass|usepackage|begin|maketitle|begin|end|includegraphics|nodeconnect|hspace|setlength|frac|(?>re)?newcommand|epsfxsize).*$";
  35. char patternEmbeddedCommands[] = "(.*?)\\\\(.*?)\\{(.*?)\\(.*?)\\}(.*?)\\}(.*)";
  36. char patternPreserveTextInsideBrackets[] =
  37.         "(.*?)\\\\(?>title|author|date|enumsentence|section|subsection|emph|href|url|item|subfigure|leftline|epsffile|caption|label|footnote|text).?\\{?(.*?)\\}?(.*?)?";
  38. char patternNoBrackets[] = "(.*?)\\\\(?:\\btt\\b|left|item|noindent|right|medskip|qquad|over|partial|right|small|tiny|smallskip|large|bigskip|mathop|longrightarrow|propto|\\bem\\b|sum|vec|cdot|\\bverb\\b|hfill|bigr|sinh)(.*?)";
  39. char patternDeleteInlineEntirely[] = "(.*)\\\\(?>end|bibitem|mbox|vspace|ref|scalebox|includegraphics|limits|log|textbf|textsl|texttt|textit|bigl|eqref|mathbf|nabla|verbatiminput).?\\{(?:.*?)\\}?(.*)?";
  40. char patternCustomColor[] = "(.*)\\\\color.*?\\}(.*)";
  41. char patternSpaces[] = "(\\s\\s)";
  42. char patternGreekLetters[] = "(.*?)\\\\([Aa]lpha|[Dd]igamma|[Kk]appa|[Oo]micron|[Uu]psilon|[Bb]eta|[Zz]eta|[Ll]ambda|[Pp]i|[Pp]hi|[Gg]amma|[Ee]ta|[Mm]u|[Rr]ho|[Cc]hi|[Dd]elta|[Tt]heta|[Nn]u|[Ss]igma|[Pp]si|[Ee]psilon|[Ii]ota|[Xx]i|[Tt]au|[Oo]mega)(.*)";
  43.  
  44.  
  45. int main(int argc, char **argv) {
  46.     int isEnd = 0;
  47.     int option = 0;
  48.  
  49.     while (!isEnd) {
  50.         printf(ANSI_COLOR_GREEN"Witaj w Czyścicielu!"ANSI_COLOR_RESET"\n");
  51.         printf("[1] Wyczyść plik .tex\n");
  52.         printf(ANSI_COLOR_MAGENTA"[0] Wyjdź z aplikacji"ANSI_COLOR_RESET"\n\n");
  53.         printf("Wybierz opcję: \n");
  54.  
  55.         scanf("%d", &option);
  56.  
  57.         while (getchar() != '\n');
  58.  
  59.         switch (option) {
  60.             case 1:
  61.                 returnLine = malloc(sizeof(PCRE2_UCHAR) * 1024);
  62.                 cleanFile();
  63.                 finalizeCleaning();
  64.                 break;
  65.  
  66.             case 0:
  67.             default:
  68.                 isEnd = 1;
  69.                 break;
  70.         }
  71.  
  72.     }
  73.     return 0;
  74. }
  75.  
  76. void cleanFile(void) {
  77.     char buffer[255];
  78.     char *temp = malloc(sizeof(char) * 255);
  79.     char outName[100];
  80.  
  81.     printf("Podaj pełną ścieżkę do pliku .tex\n");
  82.     scanf("%70s", PLIK_TXT);
  83.     if ((inputFile = fopen(PLIK_TXT, "r")) != NULL) {
  84.         fprintf(stdout, "Otwarto plik %s w trybie odczytu tekstowego.\n", PLIK_TXT);
  85.         strcpy(outName, PLIK_TXT);
  86.         outputFile = fopen(strcat(outName, "_cleaned.txt.tmp"), "w");
  87.         printf("Utworzono plik tymczasowy %s\n", outName);
  88.  
  89.         while (fgets(buffer, 255, (FILE *) inputFile)) {
  90.             strcpy(temp, buffer);
  91.             if (strcmp(temp, "\n") == 0) {
  92.                 continue;
  93.             }
  94.             filterLine(temp, returnLine, 3);
  95.             fputs(returnLine, outputFile);
  96.         }
  97.  
  98.  
  99.         fclose(outputFile);
  100.         free(temp);
  101.         fclose(inputFile);
  102.     } else {
  103.         printf("Nie moge odczytac pliku %s!\n", PLIK_TXT);
  104.     }
  105. }
  106.  
  107. void filterLine(char *line, char *result, int runCount) {
  108.     checkLineWithRegex(paternDeletWholeLine, line, "");
  109.     for (int i = 0; i < runCount; i++) {
  110.  
  111.         checkLineWithRegex(patternEmbeddedCommands, result, "\\${4} ${5}");
  112.         checkLineWithRegex(patternEmbeddedCommands, result, "\\${4} ${5}");
  113.         checkLineWithRegex(patternEmbeddedCommands, result, "\\${4} ${5}");
  114.  
  115.         checkLineWithRegex(patternGreekLetters, result, "${1} ${2} ${3}");
  116.         checkLineWithRegex(patternGreekLetters, result, "${1} ${2} ${3}");
  117.         checkLineWithRegex(patternGreekLetters, result, "${1} ${2} ${3}");
  118.         checkLineWithRegex(patternGreekLetters, result, "${1} ${2} ${3}");
  119.         checkLineWithRegex(patternGreekLetters, result, "${1} ${2} ${3}");
  120.  
  121.         checkLineWithRegex(patternPreserveTextInsideBrackets, result, "${1} ${2} ${3}");
  122.         checkLineWithRegex(patternPreserveTextInsideBrackets, result, "${1} ${2} ${3}");
  123.  
  124.         checkLineWithRegex(patternDeleteInlineEntirely, result, "${1} ${2}");
  125.         checkLineWithRegex(patternDeleteInlineEntirely, result, "${1} ${2}");
  126.         checkLineWithRegex(patternDeleteInlineEntirely, result, "${1} ${2}");
  127.         checkLineWithRegex(patternDeleteInlineEntirely, result, "${1} ${2}");
  128.  
  129.  
  130.         checkLineWithRegex(patternNoBrackets, result, "${1} ${2}");
  131.         checkLineWithRegex(patternNoBrackets, result, "${1} ${2}");
  132.         checkLineWithRegex(patternNoBrackets, result, "${1} ${2}");
  133.         checkLineWithRegex(patternNoBrackets, result, "${1} ${2}");
  134.         checkLineWithRegex(patternNoBrackets, result, "${1} ${2}");
  135.  
  136.         checkLineWithRegex(patternCustomColor, result, "${1} ${2}");
  137.         checkLineWithRegex(patternCustomColor, result, "${1} ${2}");
  138.         checkLineWithRegex(patternCustomColor, result, "${1} ${2}");
  139.  
  140.         checkLineWithRegex(patternSpaces, result, " ");
  141.         checkLineWithRegex(patternSpaces, result, " ");
  142.         checkLineWithRegex(patternSpaces, result, " ");
  143.         checkLineWithRegex(patternSpaces, result, " ");
  144.  
  145.     }
  146. }
  147.  
  148. void finalizeCleaning() {
  149.     char resultName[100];
  150.     char tmpName[100];
  151.     strcat(strcpy(tmpName, PLIK_TXT), "_cleaned.txt.tmp");
  152.     strcat(strcpy(resultName, PLIK_TXT), "_cleaned.txt");
  153.  
  154.     if ((outputFile = fopen(tmpName, "r")) != NULL) {
  155.         resultFile = fopen(resultName, "w");
  156.         printf("Utworzono plik wynikowy %s\n", resultName);
  157.  
  158.         int curr;
  159.         while ((curr = fgetc(outputFile)) != EOF) {
  160.             if (curr != '{' && curr != '}' && curr != '\\') {
  161.                 fputc(curr, resultFile);
  162.             }
  163.         }
  164.  
  165.  
  166.         fclose(outputFile);
  167.         fclose(resultFile);
  168.         remove(tmpName);
  169.         printf("Usunięto plik tymczasowy %s\n", tmpName);
  170.  
  171.     } else {
  172.         printf("Nie moge odczytac pliku %s!\n", tmpName);
  173.     }
  174.     printf(ANSI_COLOR_GREEN"Gotowe!"ANSI_COLOR_RESET"\n");
  175.     free(returnLine);
  176. }
  177.  
  178. char *checkLineWithRegex(char *patternIn, char *subjectIn, char *replacementIn) {
  179.     int error;
  180.     PCRE2_SIZE erroffset;
  181.  
  182.     const PCRE2_SPTR pattern = (PCRE2_SPTR) patternIn;
  183.     const PCRE2_SPTR subject = (PCRE2_SPTR) subjectIn;
  184.     const PCRE2_SPTR replacement = (PCRE2_SPTR) replacementIn;
  185.     int subject_length = (PCRE2_SIZE) strlen((char *) subject);
  186.  
  187.     pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
  188.     if (re == 0) {
  189.         return "";
  190.     }
  191.  
  192.     pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
  193.  
  194.     PCRE2_UCHAR output[1024] = "";
  195.     PCRE2_SIZE outlen = sizeof(output) / sizeof(PCRE2_UCHAR);
  196.  
  197.     pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
  198.  
  199.  
  200.     int matched = pcre2_match(
  201.             re,                   /* the compiled pattern */
  202.             subject,              /* the subject string */
  203.             subject_length,       /* the length of the subject */
  204.             0,                    /* start at offset 0 in the subject */
  205.             0,                    /* default options */
  206.             match_data,           /* block for storing the result */
  207.             NULL);
  208.     if (matched == 1) {
  209.         pcre2_code_free(re);
  210.  
  211.         return strcpy((char *) returnLine, (char *) output);
  212.     }
  213.  
  214.     int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0,
  215.                               PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement,
  216.                               PCRE2_ZERO_TERMINATED, output, &outlen);
  217.     pcre2_code_free(re);
  218.     if (rc > 0) {
  219.         return strcpy((char *) returnLine, (char *) output);
  220.  
  221.     } else {
  222.         return strcpy((char *) returnLine, subjectIn);
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement