Advertisement
Guest User

Untitled

a guest
Mar 19th, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include "pcre.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <errno.h>
  6.  
  7. unsigned int filesize(const char *filename) {
  8.     struct stat sb;
  9.  
  10.     if (stat(filename, &sb)) {
  11.         printf("Stat error: %s\n", strerror(errno));
  12.         return 0;
  13.     }
  14.  
  15.     return sb.st_size;
  16. }
  17.  
  18. int main() {
  19.     char *regexp = "(?s)(?:\\pL+\\n){5000}";
  20.  
  21.     const char *error;
  22.     int erroffset;
  23.  
  24.     pcre16 *re = pcre16_compile(
  25.         (PCRE_SPTR16) regexp,
  26.         PCRE_UTF16 | PCRE_UCP,
  27.         &error,
  28.         &erroffset,
  29.         NULL
  30.     );
  31.  
  32.     printf("PCRE version: %s\n", pcre16_version());
  33.  
  34.     if (!re) {
  35.         printf("Compile error: %s\n", error);
  36.         return 1;
  37.     }
  38.  
  39.     char *filename = "utf16.txt";
  40.  
  41.     size_t size = filesize(filename);
  42.     char *str = malloc(size + 1);
  43.     FILE *fp = fopen(filename, "r");
  44.  
  45.     fread(str, sizeof(char), size, fp);
  46.     fclose(fp);
  47.  
  48.     int match;
  49.     int offsets[3];
  50.  
  51.     for (int i = 0; i<10; i++) {
  52.         match = pcre16_exec(re, NULL, (PCRE_SPTR16) str, size, 0, 0, offsets, 3);
  53.         printf(match == PCRE_ERROR_NOMATCH ? "No!\n" : "Yes!\n");
  54.     }
  55.  
  56.     free(str);
  57.  
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement