|
| 1 | +package sample; |
| 2 | + |
| 3 | + |
| 4 | +import com.gliwka.hyperscan.jni.ch_capture_t; |
| 5 | +import com.gliwka.hyperscan.jni.ch_compile_error_t; |
| 6 | +import com.gliwka.hyperscan.jni.ch_database_t; |
| 7 | +import com.gliwka.hyperscan.jni.ch_error_event_handler; |
| 8 | +import com.gliwka.hyperscan.jni.ch_match_event_handler; |
| 9 | +import com.gliwka.hyperscan.jni.ch_scratch_t; |
| 10 | +import com.gliwka.hyperscan.jni.hyperscan; |
| 11 | +import org.bytedeco.javacpp.BytePointer; |
| 12 | +import org.bytedeco.javacpp.IntPointer; |
| 13 | +import org.bytedeco.javacpp.Loader; |
| 14 | +import org.bytedeco.javacpp.Pointer; |
| 15 | +import org.bytedeco.javacpp.PointerPointer; |
| 16 | +import org.bytedeco.javacpp.annotation.Cast; |
| 17 | +import org.bytedeco.javacpp.annotation.Const; |
| 18 | + |
| 19 | +import java.util.LinkedList; |
| 20 | + |
| 21 | +import static com.gliwka.hyperscan.jni.hyperscan.HS_FLAG_SINGLEMATCH; |
| 22 | +import static com.gliwka.hyperscan.jni.hyperscan.HS_MODE_BLOCK; |
| 23 | + |
| 24 | + |
| 25 | +public class ChScanExample { |
| 26 | + |
| 27 | + public static void main(String... args) { |
| 28 | + Loader.load(hyperscan.class); |
| 29 | + |
| 30 | + String[] patterns = {"abc1", "asa", "dab"}; |
| 31 | + ch_database_t database_t = null; |
| 32 | + ch_match_event_handler matchEventHandler = null; |
| 33 | + ch_error_event_handler errorEventHandler = null; |
| 34 | + ch_scratch_t scratchSpace = new ch_scratch_t(); |
| 35 | + ch_compile_error_t compile_error_t; |
| 36 | + final LinkedList<long[]> matchedIds = new LinkedList<>(); |
| 37 | + try (PointerPointer<ch_database_t> database_t_p = new PointerPointer<>(1); |
| 38 | + PointerPointer<ch_compile_error_t> compile_error_t_p = new PointerPointer<>(1); |
| 39 | + IntPointer compileFlags = new IntPointer(HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH, HS_FLAG_SINGLEMATCH); |
| 40 | + IntPointer patternIds = new IntPointer(1, 2, 3); |
| 41 | + PointerPointer expressionsPointer = new PointerPointer<BytePointer>(patterns); |
| 42 | + ) { |
| 43 | + matchEventHandler = new ch_match_event_handler() { |
| 44 | + @Override |
| 45 | + public int call(@Cast("unsigned int") int id, |
| 46 | + @Cast("unsigned long long") long from, |
| 47 | + @Cast("unsigned long long") long to, |
| 48 | + @Cast("unsigned int") int flags, |
| 49 | + @Cast("unsigned int") int size, |
| 50 | + @Const ch_capture_t captured, |
| 51 | + Pointer ctx) { |
| 52 | + |
| 53 | + System.out.println("id" + id + ": " + from + "-" + to); |
| 54 | + matchedIds.add(new long[]{from, to}); |
| 55 | + return 0; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + errorEventHandler = new ch_error_event_handler() { |
| 60 | + @Override |
| 61 | + public int call(@Cast("ch_error_event_t") int error_type, |
| 62 | + @Cast("unsigned int") int id, Pointer info, |
| 63 | + Pointer ctx) { |
| 64 | + |
| 65 | + System.out.println("Error for id" + id + ":" + error_type); |
| 66 | + return 0; |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + int result = hyperscan.ch_compile_multi( |
| 71 | + expressionsPointer, |
| 72 | + compileFlags, |
| 73 | + patternIds, |
| 74 | + 3, |
| 75 | + HS_MODE_BLOCK, |
| 76 | + null, |
| 77 | + database_t_p, |
| 78 | + compile_error_t_p |
| 79 | + ); |
| 80 | + |
| 81 | + database_t = new ch_database_t(database_t_p.get(0)); |
| 82 | + compile_error_t = new ch_compile_error_t(compile_error_t_p.get(0)); |
| 83 | + if (result != 0) { |
| 84 | + System.out.println(compile_error_t.message().getString()); |
| 85 | + System.exit(1); |
| 86 | + } |
| 87 | + result = hyperscan.ch_alloc_scratch(database_t, scratchSpace); |
| 88 | + if (result != 0) { |
| 89 | + System.out.println("Error during scratch space allocation"); |
| 90 | + System.exit(1); |
| 91 | + } |
| 92 | + |
| 93 | + String textToSearch = "-21dasaaadabcaaa"; |
| 94 | + |
| 95 | + hyperscan.ch_scan( |
| 96 | + database_t, |
| 97 | + textToSearch, |
| 98 | + textToSearch.length(), |
| 99 | + 0, |
| 100 | + scratchSpace, |
| 101 | + matchEventHandler, |
| 102 | + errorEventHandler, |
| 103 | + expressionsPointer |
| 104 | + ); |
| 105 | + |
| 106 | + System.out.println("Count: " + matchedIds.size()); |
| 107 | + |
| 108 | + } catch (Exception e) { |
| 109 | + throw new RuntimeException(e); |
| 110 | + } finally { |
| 111 | + hyperscan.ch_free_scratch(scratchSpace); |
| 112 | + if (database_t != null) { |
| 113 | + hyperscan.ch_free_database(database_t); |
| 114 | + } |
| 115 | + if (matchEventHandler != null) { |
| 116 | + matchEventHandler.close(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments