예제 #1
0
void tokenizePatternFile(std::ifstream& in) {
    // tokenize a line from the pattern file.  The first part will be the pattern and the second
    // part is the file to write to.

    std::string lineptr;

    while(in.good()) {
        std::getline(in, lineptr);
        if(lineptr.empty()) {
            continue;
        }
        std::vector<std::string> fields;
        split(fields, lineptr, "\t");
        switch(fields.size()) {
            case 0:
                break;
            case 1:
                manager.add(fields[0]);
                if(opts.r_flag) {
                    std::string rcpattern = fields[0];
                    reverseComplement(rcpattern);
                    manager.add(rcpattern);
                }
                break;
            default:
                manager.add(fields[0], fields[1]);
                if(opts.r_flag) {
                    std::string rcpattern = fields[0];
                    reverseComplement(rcpattern);
                    manager.add(rcpattern, fields[1]);
                }
                break;
        }
    }
}
예제 #2
0
int main(int argc, char * argv[])
{
    int opt_idx = parseOptions(argc, argv);
    std::string pattern;
    if(opts.f_flag == NULL) {

        if( opt_idx >= argc) {
            puts("Please provide a pattern (or pattern file) and at least one input file");
            usage(usage_msg);
        } else if (opt_idx >= argc - 1) {
            puts("Please provide an input file");
            usage(usage_msg);
        }
        pattern = argv[opt_idx];
        ++opt_idx;
        manager.add(pattern);
        if(opts.r_flag) {
          std::string rcpattern = pattern;
            reverseComplement(rcpattern);
            manager.add(rcpattern);
        }


    } else {
        if (opt_idx > argc - 1) {
            puts("Please provide an input file");
            usage(usage_msg);
        }
        std::ifstream in (opts.f_flag);
        if(!in.good()) {
            fprintf(stderr, "problem opening pattern file\n");
            exit(1);
        } else {
            tokenizePatternFile(in);
        }
    }

    while(opt_idx < argc) {
        Fxstream stream;
        int stream_state = 1;
        if(! opts.S_flag && ! opts.I_flag) {
            // two read files
            stream_state = stream.open(argv[opt_idx], argv[opt_idx+1], opts.I_flag);

        } else {
            // one read file
            stream_state = stream.open(argv[opt_idx], NULL, opts.I_flag);
        }
        if(stream_state != 0) {
            fprintf(stderr, "Failed to open stream\n");
            return 1;
        }

        if(opts.E_flag | opts.G_flag) {
            // Posix  regex
            regex_t px_regex;
            int flags = REG_NOSUB;  //only need to report success or failure
            if(opts.E_flag) {
                flags |= REG_EXTENDED;
            } else {
                flags |= REG_BASIC;
            }
            int ret = regcomp(&px_regex, pattern.c_str(), flags);
            if(ret) {
                char * errorbuf = get_regerror(ret, &px_regex);
                fprintf(stderr, "%s\n", errorbuf);
                free(errorbuf);
                return 1;
            }
            ret = posix_regex_search(stream, &px_regex);
            regfree(&px_regex);
        }
#ifdef HAVE_PCRE
        else if (opts.P_flag) {

            // PCRE regex
            pcre *re;
            const char *error;
            int erroffset;

            re = pcre_compile(
                pattern.c_str(),              /* the pattern */
                0,                    /* default options */
                &error,               /* for error message */
                &erroffset,           /* for error offset */
                NULL);                /* use default character tables */
            if (re == NULL) {
                printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
                return 1;
            }

            pcre_search(stream, re);
            pcre_free(re);
        }
#endif
        else if(opts.X_flag) {
            hash_search(stream);
        }
        else if(opts.f_flag) {
            multipattern_search(stream);

        } else {
            simple_string_search(stream, pattern.c_str());
        }

        if(opts.c_flag) {
            printf("%d\n", matched_records);
        }

        if(! opts.S_flag && ! opts.I_flag) {
            opt_idx+=2;
        } else {
            opt_idx++;
        }
    }
    return 0;
}