regexDemo.c (6124B)
1 //#!/usr/bin/tcc -run 2 // qwd 3 4 #include "../release/libsheepy.h" 5 #include "../release/libsheepy.c" 6 #include "pcre.h" 7 //include regex.h 8 9 #define internal static 10 11 #include <stdlib.h> 12 #include <stdbool.h> 13 #include <string.h> 14 #include <stdio.h> 15 16 void replace(); 17 #ifndef unitTest 18 #endif 19 int MAIN(int ARGC, char** ARGV); 20 21 int argc; char **argv; 22 23 // unsed function 24 void replace() { 25 char **replace_configuration = NULL; 26 char **search_what = NULL; 27 char **replace_with = NULL; 28 char **config = NULL; 29 30 printf("\nLoading config...\n"); 31 printf("\n"); 32 33 replace_configuration = readText("replace_configuration.txt"); 34 35 exitFailure(replace_configuration); 36 37 search_what = split(replace_configuration[1], replace_configuration[0]); 38 replace_with = split(replace_configuration[2], replace_configuration[0]); 39 40 enumerateCharP(search_what, e, i) { 41 printf("Replace: "); 42 printf("\n"); 43 printf("%s\n", *e); 44 printf("With:"); 45 printf("\n"); 46 printf("%s\n", replace_with[i]); 47 } 48 49 printf("\n\nProcess files...\n"); 50 printf("\n"); 51 52 config = readText("files.txt"); 53 54 forEachCharP(config, f) { 55 if (!isBlankS(*f)) { 56 if (*f[0] != '#') { 57 pErrorNULL(iTrimS(f)); 58 char **src = readText(*f); 59 if (!src) { 60 printf("Cant read file %s", *f); 61 printf("\n"); 62 continue; 63 } 64 65 forEachCharP(src, l) { 66 enumerateCharP(search_what, a, i) { 67 pErrorNULL(iReplaceS_max(l, *a, replace_with[i])); 68 } 69 } 70 71 bool status = writeText(*f, src); 72 if (status) { 73 printf("CHANGED: %s", *f); 74 printf("\n"); 75 } 76 else { 77 printf("DIDNT CHANGE: %s", *f); 78 printf("\n"); 79 } 80 } 81 } 82 } 83 84 listFreeS(config); 85 } 86 87 // ------------------------------------------------------------------------------------- 88 89 #ifndef unitTest 90 // Remove main when running the unit tests 91 #define MAIN main 92 #endif 93 int MAIN(int ARGC, char** ARGV) { 94 int dum UNUSED; 95 96 argc = ARGC; argv = ARGV;;// compile a regular expression and run on a list of strings 97 98 //replace(); 99 100 // PERL REGEX 101 // example 102 // http://www.mitchr.me/SS/exampleCode/AUPG/pcre_example.c.html 103 104 char *aStrRegex; 105 aStrRegex = "\\bhello\\b"; 106 printf("Regex to use: %s\n", aStrRegex); 107 108 pcre *reCompiled; 109 const char *pcreErrorStr; 110 int pcreErrorOffset; 111 reCompiled = pcre_compile(aStrRegex, 0, &pcreErrorStr, &pcreErrorOffset, NULL); 112 113 if (!reCompiled) { 114 printf("ERROR: Could not study >%s<: %s", aStrRegex, pcreErrorStr); 115 printf("\n"); 116 XFAILURE; 117 } 118 119 pcre_extra *pcreExtra; 120 // Optimize the regex 121 pcreExtra = pcre_study(reCompiled, 0, &pcreErrorStr);; 122 123 if (!pcreExtra) { 124 printf("ERROR: Could not study >%s<: %s", aStrRegex, pcreErrorStr); 125 printf("\n"); 126 XFAILURE; 127 } 128 129 char **strings = listCreateS("This should match... helloqwe","This could match... hello!","More than one hello.. hello","No chance of a match..."); 130 131 forEachCharP(strings, aLineToMatch) { 132 printf("String: %s", *aLineToMatch); 133 printf("\n"); 134 printf(" %s", "0123456789012345678901234567890123456789"); 135 printf("\n"); 136 printf(" %s", "0 1 2 3"); 137 printf("\n"); 138 139 int r; 140 int subStrVec[30]; 141 size_t len = strlen(*aLineToMatch); 142 size_t offset = 0; 143 r = pcre_exec(reCompiled, pcreExtra, *aLineToMatch, len, 0, 0, subStrVec, 30); 144 145 if (r < 0) { 146 printf("Something bad happened.."); 147 printf("\n"); 148 switch (r) 149 case PCRE_ERROR_NOMATCH: 150 printf("String did not match the pattern\n"); 151 } 152 else { 153 printf("Result: We have a match!\n"); 154 } 155 156 while ((offset < len) && (r >= 0)) { 157 158 if (r == 0) { 159 printf("But too many substrings were found to fit in subStrVec!\n"); 160 r = 30/3; 161 } 162 163 const char *psubStrMatchStr; 164 for (int j=0; j < r; j++) { 165 pcre_get_substring(*aLineToMatch, subStrVec, r, j, &(psubStrMatchStr)); 166 printf("Match(%2d/%2d): (%2d,%2d): >%s<\n", j, r-1, subStrVec[j*2], subStrVec[j*2+1], psubStrMatchStr); 167 168 // replace word 169 char *sBefore = sliceS(*aLineToMatch, 0, subStrVec[j*2]); 170 char *sAfter = sliceS(*aLineToMatch, subStrVec[j*2+1], 0); 171 printf("BEFORE >%s<", sBefore); 172 printf("\n"); 173 printf("AFTER >%s<", sAfter); 174 printf("\n"); 175 freeManyS(sBefore, sAfter); 176 } 177 178 offset = subStrVec[1]; 179 r = pcre_exec(reCompiled, pcreExtra, *aLineToMatch, len, offset, 0, subStrVec, 30); 180 } 181 } 182 183 184 pcre_free(reCompiled); 185 if (pcreExtra) { 186 #ifdef PCRE_CONFIG_JIT 187 pcre_free_study(pcreExtra); 188 #else 189 pcre_free(pcreExtra); 190 #endif 191 } 192 193 // POSIX REGEX 194 /* regex_t regex; */ 195 /* int r; */ 196 /* char *regS = "str" */ 197 /* r = regcomp(®ex, regS, 0); */ 198 /* if r */ 199 /* print 'regex failed to compile' */ 200 /* XFAILURE; */ 201 /* */ 202 /* regmatch_t m[30]; */ 203 /* char *s = "asasd str string sdfasas" */ 204 /* r = regexec(®ex, "asasd str sdfasas", 30, m, 0) */ 205 /* if !r */ 206 /* print 'match' */ 207 /* for int i = 0; m[i].rm_so != -1; i++ */ 208 /* char * sli = sliceS(s, m[i].rm_so, m[i].rm_eo) */ 209 /* print '<%d,%d> %s', m[i].rm_so, m[i].rm_eo, sli */ 210 /* free sli */ 211 /* */ 212 /* else if r = REG_NOMATCH */ 213 /* print 'no match' */ 214 /* else */ 215 /* print 'regex error' */ 216 /* */ 217 /* regfree(®ex); */ 218 219 XSUCCESS; 220 }