Advertisement
michciu

pcre2

Dec 13th, 2019
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 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. void odczyt_pliku_tekstowego(void);
  9.  
  10. char *checkLineWithRegex(char *pattern, char *subject, char *replacement);
  11.  
  12. FILE *inputFile;
  13. FILE *outputFile;
  14. char PLIK_TXT[] = "/home/rocket/CLionProjects/zpcre2/sample2.tex";
  15. char patternPreserveTextInsideBrackets[] =
  16.         "(.*)\\\\(?>title|author|date|enumsentence|section|subsection|emph|href|url).?\\{?(.*?)\\}(.*)?";
  17. char patternDeleteAllOccurences[] = "";
  18. char paternDeletWholeLine[] =
  19.         "^(?>\\s*)\\\\(?>documentclass|usepackage|begin|maketitle|begin|end|includegraphics|nodeconnect|hspace).*$";
  20. PCRE2_UCHAR *returnLine;
  21.  
  22.  
  23. int main(int argc, char **argv) {
  24.     returnLine = malloc(sizeof(PCRE2_UCHAR) * 1024);
  25.     odczyt_pliku_tekstowego();
  26.  
  27.     return 0;
  28. }
  29.  
  30. void odczyt_pliku_tekstowego(void) {
  31.     char buffer[255];
  32.     char *temp = malloc(sizeof(char) * 255);
  33.     char outName[60];
  34.  
  35.  
  36.     if ((inputFile = fopen(PLIK_TXT, "r")) != NULL) {
  37.         fprintf(stdout, "Otwarto plik %s w trybie odczytu tekstowego.\n", PLIK_TXT);
  38.         strcpy(outName, PLIK_TXT);
  39.         outputFile = fopen(strcat(outName, "_cleaned.txt"), "w");
  40.         printf("Utworzono plik tekstowy %s\n", outName);
  41.  
  42.         while (fgets(buffer, 255, (FILE *) inputFile)) {
  43.             strcpy(temp, buffer);
  44.             if (strcmp(temp, "\n") == 0) {
  45.                 continue;
  46.             }
  47.             checkLineWithRegex(paternDeletWholeLine, temp, "");
  48.             checkLineWithRegex(patternPreserveTextInsideBrackets, returnLine, "${1} ${2} ${3}");
  49.  
  50.             fputs(returnLine, outputFile);
  51.             printf("%s\n", returnLine);
  52.         }
  53.  
  54.  
  55.         fclose(outputFile);
  56.         free(temp);
  57.         fclose(inputFile);
  58.     } else {
  59.         printf("Nie moge odczytac pliku %s!\n", PLIK_TXT);
  60.     }
  61. }
  62.  
  63. char *checkLineWithRegex(char *patternIn, char *subjectIn, char *replacementIn) {
  64.     int error;
  65.     PCRE2_SIZE erroffset;
  66.  
  67.     const PCRE2_SPTR pattern = (PCRE2_SPTR) patternIn;
  68.     const PCRE2_SPTR subject = (PCRE2_SPTR) subjectIn;
  69.     const PCRE2_SPTR replacement = (PCRE2_SPTR) replacementIn;
  70.     int subject_length = (PCRE2_SIZE) strlen((char *) subject);
  71.  
  72.     pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
  73.     if (re == 0) {
  74.         return "";
  75.     }
  76.  
  77.     pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
  78.  
  79.     PCRE2_UCHAR output[1024] = "";
  80.     PCRE2_SIZE outlen = sizeof(output) / sizeof(PCRE2_UCHAR);
  81.  
  82.     pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);;
  83.  
  84.  
  85.     int matched = pcre2_match(
  86.             re,                   /* the compiled pattern */
  87.             subject,              /* the subject string */
  88.             subject_length,       /* the length of the subject */
  89.             0,                    /* start at offset 0 in the subject */
  90.             0,                    /* default options */
  91.             match_data,           /* block for storing the result */
  92.             NULL);
  93.     if (matched == 1) {
  94.         pcre2_code_free(re);
  95.         printf("returnLine: %s, output: %s\n", returnLine, output);
  96.  
  97.         return strcpy((char *) returnLine, (char *) output);
  98.     }
  99.  
  100.     int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0,
  101.                               PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement,
  102.                               PCRE2_ZERO_TERMINATED, output, &outlen);
  103.  
  104.     if (rc > 0) {
  105.         printf("returnLine: %s, output: %s\n", returnLine, output);
  106.  
  107.         return strcpy((char *) returnLine, (char *) output);
  108.  
  109.     } else {
  110.         printf("returnLine: %s, subjectIn: %s\n", returnLine, subjectIn);
  111.  
  112.         return strcpy((char *) returnLine, subjectIn);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement