| 1 | #ifndef FILTEROBJ_H |
| 2 | #define FILTEROBJ_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include "sharedObj.H" |
| 7 | #include "util++.H" |
| 8 | |
| 9 | // |
| 10 | // Object that will filter and output hits. If no sharedObj is |
| 11 | // supplied, the default behavior is output all hits. |
| 12 | // |
| 13 | |
| 14 | // The default filter here inserts lots of '#'s into the output string, |
| 15 | // then replaces those with the real match id on output. An alternative |
| 16 | // (and probably better idea) is to build a vector of structs of things |
| 17 | // to output. |
| 18 | |
| 19 | class filterObj { |
| 20 | public: |
| 21 | filterObj(sharedObj *so, char *soOpts); |
| 22 | ~filterObj(); |
| 23 | |
| 24 | void addHit(char direction, |
| 25 | uint32 id1, |
| 26 | uint32 pos1, |
| 27 | uint32 len1, |
| 28 | uint32 id2, |
| 29 | uint32 pos2, |
| 30 | uint32 len2, |
| 31 | uint32 filled); |
| 32 | void filter(void); |
| 33 | uint64 output(FILE *, uint64); |
| 34 | |
| 35 | private: |
| 36 | char *soOpts; |
| 37 | sharedObj *so; |
| 38 | |
| 39 | void *handle; |
| 40 | |
| 41 | void * (*soconstruct)(char *); |
| 42 | void (*sodestruct)(void *); |
| 43 | void (*soaddHit)(void *, char, uint32, uint32, uint32, uint32, uint32, uint32, uint32); |
| 44 | void (*sofilter)(void *); |
| 45 | uint64 (*sooutput)(void *, FILE *, uint64); |
| 46 | |
| 47 | uint32 theOutputPos; |
| 48 | uint32 theOutputMax; |
| 49 | char *theOutput; |
| 50 | |
| 51 | char name1[32]; |
| 52 | char name2[32]; |
| 53 | |
| 54 | friend class statObj; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | |
| 59 | inline |
| 60 | filterObj::filterObj(sharedObj *so_, char *op_) { |
| 61 | soOpts = 0L; |
| 62 | so = so_; |
| 63 | |
| 64 | handle = 0L; |
| 65 | |
| 66 | soconstruct = 0L; |
| 67 | sodestruct = 0L; |
| 68 | soaddHit = 0L; |
| 69 | sofilter = 0L; |
| 70 | sooutput = 0L; |
| 71 | |
| 72 | theOutputPos = 0; |
| 73 | theOutputMax = 0; |
| 74 | theOutput = 0L; |
| 75 | |
| 76 | strcpy(name1, "UNK"); |
| 77 | strcpy(name2, "UNK"); |
| 78 | |
| 79 | if (op_) { |
| 80 | soOpts = new char [strlen(op_) + 1]; |
| 81 | strcpy(soOpts, op_); |
| 82 | |
| 83 | splitToWords W(soOpts); |
| 84 | |
| 85 | for (uint32 arg = 0; arg < W.numWords(); arg++) { |
| 86 | if (strcmp(W.getWord(arg), "-1") == 0) { |
| 87 | strncpy(name1, W.getWord(++arg), 31); |
| 88 | } else if (strcmp(W.getWord(arg), "-2") == 0) { |
| 89 | strncpy(name2, W.getWord(++arg), 31); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (so) { |
| 95 | soconstruct = (void* (*)(char *))so->get("construct"); |
| 96 | sodestruct = (void (*)(void*))so->get("destruct"); |
| 97 | soaddHit = (void (*)(void *, char, uint32, uint32, uint32, uint32, uint32, uint32, uint32))so->get("addHit"); |
| 98 | sofilter = (void (*)(void*))so->get("filter"); |
| 99 | sooutput = (uint64 (*)(void*,FILE*,uint64))so->get("output"); |
| 100 | |
| 101 | if (!soconstruct) fprintf(stderr, "construct not found!\n"); |
| 102 | if (!sodestruct) fprintf(stderr, "destruct not found!\n"); |
| 103 | if (!soaddHit) fprintf(stderr, "addHit not found!\n"); |
| 104 | if (!sofilter) fprintf(stderr, "filter not found!\n"); |
| 105 | if (!sooutput) fprintf(stderr, "output not found!\n"); |
| 106 | |
| 107 | handle = (*soconstruct)(soOpts); |
| 108 | } |
| 109 | |
| 110 | if (!so) { |
| 111 | theOutputPos = 0; |
| 112 | theOutputMax = 1048576; |
| 113 | theOutput = new char [theOutputMax]; |
| 114 | theOutput[0] = 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | inline |
| 119 | filterObj::~filterObj() { |
| 120 | if (sodestruct) |
| 121 | (*sodestruct)(handle); |
| 122 | delete [] soOpts; |
| 123 | delete [] theOutput; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | inline |
| 128 | void |
| 129 | filterObj::addHit(char orientation, |
| 130 | uint32 id1, |
| 131 | uint32 pos1, |
| 132 | uint32 len1, |
| 133 | uint32 id2, |
| 134 | uint32 pos2, |
| 135 | uint32 len2, |
| 136 | uint32 filled) { |
| 137 | |
| 138 | if (soaddHit) { |
| 139 | (*soaddHit)(handle, orientation, id1, pos1, len1, id2, pos2, len2, filled); |
| 140 | } else { |
| 141 | if (theOutputPos + 128 >= theOutputMax) { |
| 142 | theOutputMax <<= 1; |
| 143 | char *o = 0L; |
| 144 | try { |
| 145 | o = new char [theOutputMax]; |
| 146 | } catch (std::bad_alloc) { |
| 147 | fprintf(stderr, "hitMatrix::filter()-- caught std::bad_alloc in %s at line %d\n", __FILE__, __LINE__); |
| 148 | fprintf(stderr, "hitMatrix::filter()-- tried to extend output string from "uint32FMT" to "uint32FMT" bytes.\n", theOutputPos, theOutputMax); |
| 149 | exit(1); |
| 150 | } |
| 151 | memcpy(o, theOutput, theOutputPos); |
| 152 | delete [] theOutput; |
| 153 | theOutput = o; |
| 154 | } |
| 155 | |
| 156 | sprintf(theOutput + theOutputPos, |
| 157 | "M x ############ . %s:"uint32FMT" "uint32FMT" "uint32FMT" 1 %s:"uint32FMT" "uint32FMT" "uint32FMT" %s "uint32FMT"\n", |
| 158 | name1, id1, pos1, len1, name2, id2, pos2, len2, (orientation == 'f') ? "1" : "-1", filled); |
| 159 | |
| 160 | while (theOutput[theOutputPos]) |
| 161 | theOutputPos++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | inline |
| 168 | void |
| 169 | filterObj::filter(void) { |
| 170 | |
| 171 | if (sofilter) { |
| 172 | (*sofilter)(handle); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | inline |
| 177 | uint64 |
| 178 | filterObj::output(FILE *F, uint64 matchid) { |
| 179 | if (sooutput) { |
| 180 | matchid = (*sooutput)(handle, F, matchid); |
| 181 | } else { |
| 182 | char matchIDstring[32] = {0}; |
| 183 | |
| 184 | // Insert the match id's for all these matches. We have to |
| 185 | // do this here (not during searches) because we're threaded. |
| 186 | // |
| 187 | char *pos = theOutput; |
| 188 | while (*pos) { |
| 189 | |
| 190 | // Construct a string holding the text version of the match id. |
| 191 | // |
| 192 | matchid++; |
| 193 | sprintf(matchIDstring, uint64FMT, matchid); |
| 194 | |
| 195 | // At the start of an output record. Skip the row type and |
| 196 | // sub type, 'M x ', which should put us at the start of |
| 197 | // the match id. |
| 198 | // |
| 199 | pos += 4; |
| 200 | |
| 201 | // Copy the number into the space, removing any extra # |
| 202 | // marks, warning if we run out of space. |
| 203 | // |
| 204 | char *matchIDiterator = matchIDstring; |
| 205 | while ((*pos == '#') && (*matchIDiterator != 0)) |
| 206 | *pos++ = *matchIDiterator++; |
| 207 | |
| 208 | while (*pos == '#') |
| 209 | *pos++ = ' '; |
| 210 | |
| 211 | if (*matchIDiterator != 0) |
| 212 | fprintf(stderr, "WARNING: there isn't enough space in the match to insert the match id "uint64FMT" '%s'!\n", |
| 213 | matchid, matchIDstring); |
| 214 | |
| 215 | // Skip to the next record |
| 216 | // |
| 217 | while (*pos++ != '\n') |
| 218 | ; |
| 219 | } |
| 220 | |
| 221 | fwrite(theOutput, sizeof(char), theOutputPos, F); |
| 222 | } |
| 223 | |
| 224 | return(matchid); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | #endif // FILTEROBJ_H |