Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define PCRE2_CODE_UNIT_WIDTH 8
  5. #include <pcre2.h>
  6.  
  7. int main(int argc, char const *argv[])
  8. {
  9.     pcre2_code *compileCode;
  10.     PCRE2_SPTR pattern;     /* PCRE2_SPTR is a pointer to unsigned code units of */
  11.     PCRE2_SPTR subject;
  12.     PCRE2_SPTR replace;
  13.     size_t subjectLength;
  14.     size_t replaceLength;
  15.     int subsCount;    
  16.    
  17.     PCRE2_UCHAR *output = (PCRE2_UCHAR *)malloc(sizeof(PCRE2_UCHAR));
  18.     PCRE2_SIZE outputLength;
  19.    
  20.     int errornumber;
  21.     PCRE2_SIZE errorOffset;
  22.  
  23.     if (argc != 4) {
  24.         printf("Usage: ./app <pattern> <replace> <subject>\n");
  25.         return EXIT_FAILURE;
  26.     }
  27.  
  28.     pattern = (PCRE2_SPTR)argv[1];
  29.     replace = (PCRE2_SPTR)argv[2];
  30.     subject = (PCRE2_SPTR)argv[3];
  31.  
  32.     subjectLength = strlen((char *)subject);
  33.     replaceLength = strlen((char *)replace);
  34.  
  35.     compileCode = pcre2_compile(
  36.         pattern,               /* the pattern */
  37.         PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
  38.         0,                     /* default options */
  39.         &errornumber,          /* for error number */
  40.         &errorOffset,          /* for error offset */
  41.         NULL                   /* use default compile context */
  42.     );
  43.    
  44.     if (compileCode == NULL) {
  45.         PCRE2_UCHAR buffer[256];
  46.         pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
  47.         printf("PCRE2 compilation failed at offset %d: %s\n", (int)errorOffset, buffer);
  48.         pcre2_code_free(compileCode);
  49.         free(output);
  50.         return EXIT_FAILURE;
  51.     }
  52.  
  53.     subsCount = pcre2_substitute(
  54.         compileCode,           /*Points to the compiled pattern*/
  55.         subject,               /*Points to the subject string*/
  56.         PCRE2_ZERO_TERMINATED, /*subjectLength,*/ /*Length of the subject string*/
  57.         0,                     /*Offset in the subject at which to start matching*/
  58.         PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH | PCRE2_SUBSTITUTE_EXTENDED,               /*Option bits*/
  59.         NULL,                     /*Points to a match data block, or is NULL*/
  60.         NULL,                     /*Points to a match context, or is NULL*/
  61.         replace,               /*Points to the replacement string*/
  62.         PCRE2_ZERO_TERMINATED, /*replaceLength,*/ /*Length of the replacement string*/
  63.         output,               /*Points to the output buffer*/
  64.         &outputLength          /*Points to the length of the output buffer*/
  65.     );
  66.  
  67.     if (subsCount > 0) {
  68.         printf("%s\ncount: %i\n", output, subsCount);        
  69.     } else if (subsCount == 0) {
  70.         printf("No matches\n");        
  71.     } else {        
  72.         PCRE2_UCHAR buffer[256];
  73.         pcre2_get_error_message(subsCount, buffer, sizeof(buffer));
  74.         printf("PCRE2 substitute failed. ErrorCode %i: %s\n", (int)subsCount, buffer);
  75.         pcre2_code_free(compileCode);
  76.         free(output);
  77.         return EXIT_FAILURE;
  78.     }
  79.  
  80.     pcre2_code_free(compileCode);
  81.     free(output);
  82.  
  83.     return EXIT_SUCCESS;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement