Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define PCRE2_CODE_UNIT_WIDTH 8
- #include <pcre2.h>
- void odczyt_pliku_tekstowego(void);
- char *checkLineWithRegex(char *pattern, char *subject, char *replacement);
- FILE *inputFile;
- FILE *outputFile;
- char PLIK_TXT[] = "/home/rocket/CLionProjects/zpcre2/sample2.tex";
- char patternPreserveTextInsideBrackets[] =
- "(.*)\\\\(?>title|author|date|enumsentence|section|subsection|emph|href|url).?\\{?(.*?)\\}(.*)?";
- char patternDeleteAllOccurences[] = "";
- char paternDeletWholeLine[] =
- "^(?>\\s*)\\\\(?>documentclass|usepackage|begin|maketitle|begin|end|includegraphics|nodeconnect|hspace).*$";
- PCRE2_UCHAR *returnLine;
- int main(int argc, char **argv) {
- returnLine = malloc(sizeof(PCRE2_UCHAR) * 1024);
- odczyt_pliku_tekstowego();
- return 0;
- }
- void odczyt_pliku_tekstowego(void) {
- char buffer[255];
- char *temp = malloc(sizeof(char) * 255);
- char outName[60];
- if ((inputFile = fopen(PLIK_TXT, "r")) != NULL) {
- fprintf(stdout, "Otwarto plik %s w trybie odczytu tekstowego.\n", PLIK_TXT);
- strcpy(outName, PLIK_TXT);
- outputFile = fopen(strcat(outName, "_cleaned.txt"), "w");
- printf("Utworzono plik tekstowy %s\n", outName);
- while (fgets(buffer, 255, (FILE *) inputFile)) {
- strcpy(temp, buffer);
- if (strcmp(temp, "\n") == 0) {
- continue;
- }
- checkLineWithRegex(paternDeletWholeLine, temp, "");
- checkLineWithRegex(patternPreserveTextInsideBrackets, returnLine, "${1} ${2} ${3}");
- fputs(returnLine, outputFile);
- printf("%s\n", returnLine);
- }
- fclose(outputFile);
- free(temp);
- fclose(inputFile);
- } else {
- printf("Nie moge odczytac pliku %s!\n", PLIK_TXT);
- }
- }
- char *checkLineWithRegex(char *patternIn, char *subjectIn, char *replacementIn) {
- int error;
- PCRE2_SIZE erroffset;
- const PCRE2_SPTR pattern = (PCRE2_SPTR) patternIn;
- const PCRE2_SPTR subject = (PCRE2_SPTR) subjectIn;
- const PCRE2_SPTR replacement = (PCRE2_SPTR) replacementIn;
- int subject_length = (PCRE2_SIZE) strlen((char *) subject);
- pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
- if (re == 0) {
- return "";
- }
- pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
- PCRE2_UCHAR output[1024] = "";
- PCRE2_SIZE outlen = sizeof(output) / sizeof(PCRE2_UCHAR);
- pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);;
- int matched = pcre2_match(
- re, /* the compiled pattern */
- subject, /* the subject string */
- subject_length, /* the length of the subject */
- 0, /* start at offset 0 in the subject */
- 0, /* default options */
- match_data, /* block for storing the result */
- NULL);
- if (matched == 1) {
- pcre2_code_free(re);
- printf("returnLine: %s, output: %s\n", returnLine, output);
- return strcpy((char *) returnLine, (char *) output);
- }
- int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0,
- PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement,
- PCRE2_ZERO_TERMINATED, output, &outlen);
- if (rc > 0) {
- printf("returnLine: %s, output: %s\n", returnLine, output);
- return strcpy((char *) returnLine, (char *) output);
- } else {
- printf("returnLine: %s, subjectIn: %s\n", returnLine, subjectIn);
- return strcpy((char *) returnLine, subjectIn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement