| | 1 | /* |
| | 2 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| | 3 | * |
| | 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| | 5 | * on using and copying this software. |
| | 6 | */ |
| | 7 | /* |
| | 8 | Name |
| | 9 | test_regex.cpp - regular expression parser tester |
| | 10 | Function |
| | 11 | Simple interactive regular expression tester. Asks for patterns and |
| | 12 | strings to test from the keyboard and displays the results. |
| | 13 | Notes |
| | 14 | |
| | 15 | Modified |
| | 16 | 11/11/01 MJRoberts - Creation |
| | 17 | */ |
| | 18 | |
| | 19 | #include <stdio.h> |
| | 20 | #include <stdlib.h> |
| | 21 | |
| | 22 | #include "vmregex.h" |
| | 23 | #include "t3test.h" |
| | 24 | |
| | 25 | int main() |
| | 26 | { |
| | 27 | CRegexParser regex; |
| | 28 | CRegexSearcherSimple searcher(®ex); |
| | 29 | |
| | 30 | /* initialize for testing */ |
| | 31 | test_init(); |
| | 32 | |
| | 33 | /* read patterns and match strings interactively */ |
| | 34 | for (;;) |
| | 35 | { |
| | 36 | char pat[128]; |
| | 37 | char str[128]; |
| | 38 | size_t len; |
| | 39 | |
| | 40 | /* read the pattern - stop on EOF */ |
| | 41 | printf("Enter pattern: "); |
| | 42 | if (fgets(pat, sizeof(pat), stdin) == 0) |
| | 43 | break; |
| | 44 | |
| | 45 | /* trim the trailing newline, if any */ |
| | 46 | if ((len = strlen(pat)) != 0 && pat[len-1] == '\n') |
| | 47 | pat[len-1] = '\0'; |
| | 48 | |
| | 49 | /* read strings to match against the pattern */ |
| | 50 | for (;;) |
| | 51 | { |
| | 52 | int idx; |
| | 53 | int reslen; |
| | 54 | |
| | 55 | /* read the string - stop on EOF or an empty line */ |
| | 56 | printf("Enter string: "); |
| | 57 | if (fgets(str, sizeof(str), stdin) == 0 |
| | 58 | || str[0] == '\0' |
| | 59 | || str[0] == '\n') |
| | 60 | break; |
| | 61 | |
| | 62 | /* trim the trailing newline, if any */ |
| | 63 | if ((len = strlen(str)) != 0 && str[len-1] == '\n') |
| | 64 | str[len-1] = '\0'; |
| | 65 | |
| | 66 | /* match it */ |
| | 67 | idx = searcher.compile_and_search(pat, strlen(pat), |
| | 68 | str, str, strlen(str), &reslen); |
| | 69 | |
| | 70 | /* report the results */ |
| | 71 | if (idx == -1) |
| | 72 | printf("[Not found]\n"); |
| | 73 | else |
| | 74 | printf("Found: index=%d, %.*s\n", idx, reslen, str + idx); |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | return 0; |
| | 79 | } |