Advertisement
michciu

proj lepszy

Dec 12th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.73 KB | None | 0 0
  1. /*************************************************
  2. *           PCRE DEMONSTRATION PROGRAM           *
  3. *************************************************/
  4.  
  5. /* This is a demonstration program to illustrate the most straightforward ways
  6. of calling the PCRE regular expression library from a C program. See the
  7. pcresample documentation for a short discussion ("man pcresample" if you have
  8. the PCRE man pages installed).
  9.  
  10. In Unix-like environments, if PCRE is installed in your standard system
  11. libraries, you should be able to compile this program using this command:
  12.  
  13. gcc -Wall pcredemo.c -lpcre -o pcredemo
  14.  
  15. If PCRE is not installed in a standard place, it is likely to be installed with
  16. support for the pkg-config mechanism. If you have pkg-config, you can compile
  17. this program using this command:
  18.  
  19. gcc -Wall pcredemo.c `pkg-config --cflags --libs libpcre` -o pcredemo
  20.  
  21. If you do not have pkg-config, you may have to use this:
  22.  
  23. gcc -Wall pcredemo.c -I/usr/local/include -L/usr/local/lib \
  24.   -R/usr/local/lib -lpcre -o pcredemo
  25.  
  26. Replace "/usr/local/include" and "/usr/local/lib" with wherever the include and
  27. library files for PCRE are installed on your system. Only some operating
  28. systems (e.g. Solaris) use the -R option.
  29.  
  30. Building under Windows:
  31.  
  32. If you want to statically link this program against a non-dll .a file, you must
  33. define PCRE_STATIC before including pcre.h, otherwise the pcre_malloc() and
  34. pcre_free() exported functions will be declared __declspec(dllimport), with
  35. unwanted results. So in this environment, uncomment the following line. */
  36.  
  37. #define PCRE_STATIC
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <pcre.h>
  42.  
  43. #define OVECCOUNT 30    /* should be a multiple of 3 */
  44.  
  45. FILE *fu;
  46. char PLIK_TXT[] = "sample.tex";
  47.  
  48. int checkLineWithRegex(char *pattern, char *subject);
  49.  
  50. int main() {
  51.  
  52.  
  53.     checkLineWithRegex("char* pattern", "char* subject");
  54.  
  55.  
  56.     return 0;
  57. }
  58.  
  59. int checkLineWithRegex(char *pattern, char *subject) {
  60.     pcre *re;
  61.     const char *error;
  62.     int erroffset;
  63.     int ovector[OVECCOUNT];
  64.     int subject_length;
  65.     int rc, i;
  66.  
  67.  
  68.     pattern = "\\\\.*\\{.*\\}(.*)";
  69.     subject = "\\bibitem{latex}Helmut Kopka and Patrick W. Daly, \\textsl{A Guide to";
  70.     subject_length = (int) strlen(subject);
  71.  
  72.  
  73. /*************************************************************************
  74. * Now we are going to compile the regular expression pattern, and handle *
  75. * and errors that are detected.                                          *
  76. *************************************************************************/
  77.  
  78.     re = pcre_compile(
  79.             pattern,              /* the pattern */
  80.             0,                    /* default options */
  81.             &error,               /* for error message */
  82.             &erroffset,           /* for error offset */
  83.             NULL);                /* use default character tables */
  84.  
  85. /* Compilation failed: print the error message and exit */
  86.  
  87.     if (re == NULL) {
  88.         printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
  89.         return 1;
  90.     }
  91.  
  92.  
  93. /*************************************************************************
  94. * If the compilation succeeded, we call PCRE again, in order to do a     *
  95. * pattern match against the subject string. This does just ONE match. If *
  96. * further matching is needed, it will be done below.                     *
  97. *************************************************************************/
  98.  
  99.     rc = pcre_exec(
  100.             re,                   /* the compiled pattern */
  101.             NULL,                 /* no extra data - we didn't study the pattern */
  102.             subject,              /* the subject string */
  103.             subject_length,       /* the length of the subject */
  104.             0,                    /* start at offset 0 in the subject */
  105.             0,                    /* default options */
  106.             ovector,              /* output vector for substring information */
  107.             OVECCOUNT);           /* number of elements in the output vector */
  108.  
  109. /* Matching failed: handle error cases */
  110.  
  111.     if (rc < 0) {
  112.         switch (rc) {
  113.             case PCRE_ERROR_NOMATCH:
  114.                 printf("No match\n");
  115.                 break;
  116.                 /*
  117.                 Handle other special cases if you like
  118.                 */
  119.             default:
  120.                 printf("Matching error %d\n", rc);
  121.                 break;
  122.         }
  123.         pcre_free(re);     /* Release memory used for the compiled pattern */
  124.         return 1;
  125.     }
  126.  
  127. /* Match succeded */
  128.  
  129.     printf("\nMatch succeeded at offset %d\n", ovector[0]);
  130.  
  131.  
  132. /*************************************************************************
  133. * We have found the first match within the subject string. If the output *
  134. * vector wasn't big enough, say so. Then output any substrings that were *
  135. * captured.                                                              *
  136. *************************************************************************/
  137.  
  138. /* The output vector wasn't big enough */
  139.  
  140.     if (rc == 0) {
  141.         rc = OVECCOUNT / 3;
  142.         printf("ovector only has room for %d captured substrings\n", rc - 1);
  143.     }
  144.  
  145. /* Show substrings stored in the output vector by number. Obviously, in a real
  146. application you might want to do things other than print them. */
  147.  
  148.     for (i = 0; i < rc; i++) {
  149.         char *substring_start = subject + ovector[2 * i];
  150.         int substring_length = ovector[2 * i + 1] - ovector[2 * i];
  151.         printf("%2d: %.*s\n", i, substring_length, substring_start);
  152.     }
  153.  
  154.     // wyciety kod
  155.  
  156.  
  157.     printf("\n");
  158.     pcre_free(re);       /* Release memory used for the compiled pattern */
  159.     return 0;
  160. }
  161.  
  162.  
  163. void odczyt_pliku_tekstowego(void) {
  164.  
  165.     char buffer[255];
  166.     char *rp = malloc(sizeof(char) * 255);
  167.     char *temp = malloc(sizeof(char) * 255);
  168.  
  169.  
  170.     if ((fu = fopen(PLIK_TXT, "r")) !=
  171.         NULL)    // Probuje otworzyc plik w trybie tekstowym do odczytu - ta operacja moze sie nie udac. Kiedy?
  172.     {
  173.         fprintf(stdout, "Otwarto plik %s w trybie odczytu tekstowego.\n",
  174.                 PLIK_TXT);  // W taki sposob rowniez mozna wyswietlac informacje w konsoli
  175.         fgets(buffer, sizeof(buffer),
  176.               fu);    // Wczytanie z pliku pelnej linijki tekstu do wskazanej tablicy, ale nie wiecej niz podana liczba znakow
  177.         // sizeof(tablica) - to nas zabezpiecza przed przekroczeniem tablicy
  178.         printf("Wczytalem linijke z pliku: %s", buffer);   // Wczytany lancuch juz zawiera znak konca wiersza
  179.  
  180.  
  181.         while (fgets(buffer, 255, (FILE *) fu)) {
  182.             strcpy(temp, buffer);
  183.  
  184.             printf("%s\n", temp);
  185.         }
  186.  
  187.  
  188.         free(temp);
  189.         fclose(fu);
  190.     } else {
  191.         printf("Nie moge odczytac pliku %s!\n", PLIK_TXT);
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement