void search_buffer(struct search_state *state, const char *pattern, int pattern_size, char *buffer, int buffer_size, const char *file_name, ullong buffer_offset) { const char *buffer_start = buffer; /* Where to stop matching */ const char *buffer_end = buffer + buffer_size - pattern_size; int buffer_hash = hash_code(buffer, pattern_size); int pattern_hash = state->pattern_hash; int most_significant_factor = state->most_significant_factor; /* Loop through the buffer... */ while(buffer < buffer_end) { /* Compare hashes, and strings if necessary */ if(buffer_hash == pattern_hash && !strncmp(buffer, pattern, pattern_size)) { print_match(file_name, buffer_offset + (buffer - buffer_start)); } /* Update hash */ buffer_hash = ((buffer_hash - ((unsigned char) *buffer) * most_significant_factor) << 1) + (unsigned char) *(buffer + pattern_size); buffer++; } /* Comparison for the last position */ if(buffer_hash == pattern_hash && !strncmp(buffer, pattern, pattern_size)) { print_match(file_name, buffer_offset + (buffer - buffer_start)); } }
int main() { int xmatch[MAX], ymatch[MAX]; bipartite b1(4, 5); b1.b_g[0][0] = 1; b1.b_g[0][1] = 1; b1.b_g[0][2] = 1; b1.b_g[0][3] = 1; b1.b_g[1][0] = 1; b1.b_g[1][1] = 1; b1.b_g[1][3] = 1; b1.b_g[1][4] = 1; b1.b_g[2][1] = 1; b1.b_g[2][2] = 1; b1.b_g[2][3] = 1; b1.b_g[2][4] = 1; b1.b_g[3][0] = 1; b1.b_g[3][1] = 1; b1.b_g[3][2] = 1; b1.b_g[3][4] = 1; cout << "hungarian 1 max binary match: " << hungarian1(b1, xmatch, ymatch) << endl; //1 print_match(b1, ymatch); cout << "hungarian 2 max binary match: " << hungarian2(b1, ymatch) << endl; print_match(b1, ymatch); cout << "hopcroft karp max binary match: " << hopcroft_karp(b1, xmatch, ymatch) << endl; //2 print_match(b1, ymatch); bipartite b2(5, 5); b2.b_g[0][0] = 3; b2.b_g[0][1] = 5; b2.b_g[0][2] = 5; b2.b_g[0][3] = 4; b2.b_g[0][4] = 1; b2.b_g[1][0] = 2; b2.b_g[1][1] = 2; b2.b_g[1][2] = 0; b2.b_g[1][3] = 2; b2.b_g[1][4] = 2; b2.b_g[2][0] = 2; b2.b_g[2][1] = 4; b2.b_g[2][2] = 4; b2.b_g[2][3] = 1; b2.b_g[2][4] = 0; b2.b_g[3][0] = 0; b2.b_g[3][1] = 1; b2.b_g[3][2] = 1; b2.b_g[3][3] = 0; b2.b_g[3][4] = 0; b2.b_g[4][0] = 1; b2.b_g[4][1] = 2; b2.b_g[4][2] = 1; b2.b_g[4][3] = 3; b2.b_g[4][4] = 3; cout << "kuhn munkres: " << kuhn_munkres(b2, ymatch) << endl; //5 print_match(b2, ymatch); graph_matrix g(6); g.g_m[0][0] = 0; g.g_m[0][1] = 1; g.g_m[0][2] = 1; g.g_m[0][3] = 1; g.g_m[0][4] = INF; g.g_m[0][5] = INF; g.g_m[1][0] = INF; g.g_m[1][1] = 0; g.g_m[1][2] = INF; g.g_m[1][3] = 1; g.g_m[1][4] = 1; g.g_m[1][5] = INF; g.g_m[2][0] = INF; g.g_m[2][1] = INF; g.g_m[2][2] = 0; g.g_m[2][3] = 1; g.g_m[2][4] = INF; g.g_m[2][5] = 1; g.g_m[3][0] = 0; g.g_m[3][1] = INF; g.g_m[3][2] = INF; g.g_m[3][3] = 0; g.g_m[3][4] = 1; g.g_m[3][5] = 1; g.g_m[4][0] = INF; g.g_m[4][1] = INF; g.g_m[4][2] = INF; g.g_m[4][3] = INF; g.g_m[4][4] = 0; g.g_m[4][5] = 1; g.g_m[5][0] = INF; g.g_m[5][1] = INF; g.g_m[5][2] = INF; g.g_m[5][3] = INF; g.g_m[5][4] = INF; g.g_m[5][5] = 0; cout << "disjoint path covering: " << minimum_disjoint_path_covering(g) << endl;//3 cout << "joint path covering: " << minimum_joint_path_covering(g) << endl; //4 return(0); }
void libwacom_print_device_description(int fd, const WacomDevice *device) { const WacomMatch **match; WacomClass class; const char *class_name; class = libwacom_get_class(device); switch(class) { case WCLASS_UNKNOWN: class_name = "Unknown"; break; case WCLASS_INTUOS3: class_name = "Intuos3"; break; case WCLASS_INTUOS4: class_name = "Intuos4"; break; case WCLASS_INTUOS5: class_name = "Intuos5"; break; case WCLASS_CINTIQ: class_name = "Cintiq"; break; case WCLASS_BAMBOO: class_name = "Bamboo"; break; case WCLASS_GRAPHIRE: class_name = "Graphire";break; case WCLASS_ISDV4: class_name = "ISDV4"; break; case WCLASS_INTUOS: class_name = "Intuos"; break; case WCLASS_INTUOS2: class_name = "Intuos2"; break; case WCLASS_PEN_DISPLAYS: class_name = "PenDisplay"; break; case WCLASS_REMOTE: class_name = "Remote"; break; default: g_assert_not_reached(); break; } dprintf(fd, "[Device]\n"); dprintf(fd, "Name=%s\n", libwacom_get_name(device)); dprintf(fd, "DeviceMatch="); for (match = libwacom_get_matches(device); *match; match++) print_match(fd, *match); dprintf(fd, "\n"); if (libwacom_get_paired_device(device)) { dprintf(fd, "PairedID="); print_match(fd, libwacom_get_paired_device(device)); dprintf(fd, "\n"); } dprintf(fd, "Class=%s\n", class_name); dprintf(fd, "Width=%d\n", libwacom_get_width(device)); dprintf(fd, "Height=%d\n", libwacom_get_height(device)); print_integrated_flags_for_device(fd, device); print_layout_for_device(fd, device); print_styli_for_device(fd, device); dprintf(fd, "\n"); dprintf(fd, "[Features]\n"); dprintf(fd, "Reversible=%s\n", libwacom_is_reversible(device) ? "true" : "false"); dprintf(fd, "Stylus=%s\n", libwacom_has_stylus(device) ? "true" : "false"); dprintf(fd, "Ring=%s\n", libwacom_has_ring(device) ? "true" : "false"); dprintf(fd, "Ring2=%s\n", libwacom_has_ring2(device) ? "true" : "false"); dprintf(fd, "Touch=%s\n", libwacom_has_touch(device) ? "true" : "false"); dprintf(fd, "TouchSwitch=%s\n", libwacom_has_touchswitch(device)? "true" : "false"); print_supported_leds(fd, device); dprintf(fd, "NumStrips=%d\n", libwacom_get_num_strips(device)); dprintf(fd, "Buttons=%d\n", libwacom_get_num_buttons(device)); print_buttons_for_device(fd, device); }
/* * Searches through a nucleic acid sequence and matchs the K-mer and the K-mer reverse complement. * Uses a binary AND operation to limit the number of conditionals within the for loop. */ void search_nucleic_acid(Sequence* currentSequence, char* k_mer, char* reverseComplementKmer, int k) { int i; int j; float gcContent; int gc = 0; for (i = 0; i <= currentSequence->sequenceLength - k; i++) { if (k_mer[0] & currentSequence->data[i]) { for (j = 1; j < k; j++) { if (!(k_mer[j] & currentSequence->data[ i + j ])) { break; } } if (j == k) { print_match(currentSequence, i, i + k, '+'); } } if (reverseComplementKmer[0] & currentSequence->data[i]) { for (j = 1; j < k; j++) { if (!(reverseComplementKmer[j] & currentSequence->data[ i + j ])) { break; } } if (j == k) { print_match(currentSequence, i, i + k, '-'); } } if (currentSequence->data[i] & 6) { gc++; } } for (; i <= currentSequence->sequenceLength; i++) { if (currentSequence->data[i] & 6) { gc++; } } gcContent = 100 * ((float)gc / currentSequence->sequenceLength); printf("GC content for sequence %s: %.2f\%\n\n", currentSequence->sequenceName, gcContent); }
static void set_save_v1(const void *ip, const struct xt_entry_match *match) { const struct xt_set_info_match_v1 *info = (const void *)match->data; print_match("--match-set", &info->match_set); }
/* Prints out the matchinfo. */ static void set_print_v1(const void *ip, const struct xt_entry_match *match, int numeric) { const struct xt_set_info_match_v1 *info = (const void *)match->data; print_match("match-set", &info->match_set); }
/* Saves the matchinfo in parsable form to stdout. */ static void set_save(const void *ip, const struct xt_entry_match *match) { struct ipt_set_info_match *info = (struct ipt_set_info_match *) match->data; print_match("--set", &info->match_set); }
/* Prints out the matchinfo. */ static void set_print(const void *ip, const struct xt_entry_match *match, int numeric) { struct ipt_set_info_match *info = (struct ipt_set_info_match *) match->data; print_match("set", &info->match_set); }
static void statistic_print(const void *ip, const struct xt_entry_match *match, int numeric) { struct xt_statistic_info *info = (struct xt_statistic_info *)match->data; printf("statistic "); print_match(info, ""); }
/* Prints the matches that takes place within the timespan of the range */ void print_range_matches(match_s *match_range, int matches_in_range) { int match_number = 0; printf("Weekday\tDate & Time\tHome Out\tGoals\tSpectators\n"); for(match_number = 0; match_number < matches_in_range; match_number++) { print_match(match_range, match_number); } }
static void set_save_v2(const void *ip, const struct xt_entry_match *match) { const struct xt_set_info_match_v1 *info = (const void *)match->data; print_match("--match-set", &info->match_set); if (info->match_set.flags & IPSET_RETURN_NOMATCH) printf(" --return-nomatch"); }
/* Task one - performs all work for task one */ void task_one(match_s *match, int number_of_matches) { int match_number = 0; printf("The following matches have a total of 7 or more goals:\n"); printf("Weekday\tDate & Time\tHome Out\tGoals\tSpectators\n"); for(match_number = 0; match_number < number_of_matches; match_number++) { if(match[match_number].home_goal + match[match_number].out_goal >= 7) { print_match(match, match_number); } } }
static void set_print_v4_matchinfo(const struct xt_set_info_match_v4 *info, const char *opt, const char *sep) { print_match(opt, &info->match_set); if (info->flags & IPSET_FLAG_RETURN_NOMATCH) printf(" %sreturn-nomatch", sep); if ((info->flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) printf(" ! %supdate-counters", sep); if ((info->flags & IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE)) printf(" ! %supdate-subcounters", sep); set_printv4_counter(&info->packets, "packets", sep); set_printv4_counter(&info->bytes, "bytes", sep); }
int main(int argc, char **argv) { if (argc != 4) { printUsage(); } int number = atoi(argv[1]); int pos_1 = atoi(argv[2]); int pos_2 = atoi(argv[3]); char *pointer = dec_to_bin(number); int check = check_bits(pointer, pos_1, pos_2); print_match(check); free(pointer); return 0; }
void printer::run() { for (;;) { { std::unique_lock<std::mutex> lock(contents_mtx); contents_cv.wait(lock, [this] { return !this->contents.empty(); }); while (!contents.empty()) { auto&& top = contents.front(); switch (top.t) { case tag::finish: return; case tag::match: print_match(os, *top.result); break; } contents.pop(); } } } }
void print_align_list_segments (alignel* alignList) { alignel* a; unspos beg1, end1, beg2, end2; unspos height, width, i, j, prevI, prevJ, run; u32 opIx; score s; for (a=alignList ; a!=NULL ; a=a->next) { beg1 = a->beg1; end1 = a->end1; beg2 = a->beg2; end2 = a->end2; height = end1 - beg1 + 1; width = end2 - beg2 + 1; snoopGenpaf_1; // print the alignment's segments opIx = 0; for (i=j=0 ; (i< height)||(j<width) ; ) { prevI = i; prevJ = j; run = edit_script_run_of_subs (a->script, &opIx); i += run; j += run; if ((i < height) || (j < width)) edit_script_indel_len (a->script, &opIx, &i, &j); s = score_match (currParams->scoring, currParams->seq1, beg1-1+prevI, currParams->seq2, beg2-1+prevJ, run); print_match (beg1-1+prevI, beg2-1+prevJ, run, s); } } }
/* * Searches through an amino acid sequence to match a given K-mer. * Uses exact matching to locate a match. */ void search_amino_acid(Sequence* currentSequence, char* k_mer, int k) { int i; int j; for (i = 0; i < currentSequence->sequenceLength - k; i++) { if (k_mer[0] == currentSequence->data[i]) { for (j = 1; j < k; j++) { if (k_mer[j] != currentSequence->data[i+j]) { break; } } if (j == k) { print_match(currentSequence, i, i + k, '.'); } } } }
static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match) { #ifdef IPTABLES_SAVE print_match((const struct ipt_web_info *)match->data); #endif }
int main(int argc, char *const argv[]) { size_t before = 16; size_t after = 16; int color = 0; bool hexlify = true; int ch; long ia, ib; while ((ch = getopt(argc, argv, "a:b:chH")) != -1) { switch(ch) { case 'a': ia = strtol(optarg, NULL, 10); if ((ia > 0) && (ia < 1024)) { after = (size_t)ia; } break; case 'b': ib = strtol(optarg, NULL, 10); if ((ib > 0) && (ib < 1024)) { before = (size_t)ib; } break; case 'c': color++; break; case 'H': hexlify = !hexlify; break; case 'h': default: usage(); break; } } if (!isatty(STDOUT_FILENO) && color) { color--; } argc -= optind; argv += optind; if (argc < 2) { usage(); } size_t pattern_size = 0; uint8_t *pattern; if (hexlify) { pattern = hex_decode(argv[0], &pattern_size); } else { pattern_size = strlen(argv[0]); pattern = (uint8_t*)strndup(argv[0], pattern_size); } if (!pattern) { fprintf(stderr, "Invalid pattern\n"); } // initialize boyer-moore patterns int *delta1 = (int*)malloc(ALPHABET_LEN * sizeof(int)); int *delta2 = (int*)malloc(pattern_size * sizeof(int)); make_delta1(delta1, pattern, pattern_size); make_delta2(delta2, pattern, pattern_size); size_t count = 0; size_t errors = 0; for (int f=1; f<argc; f++) { int first = 1; const char *file_name = argv[f]; int fd = open(file_name, O_RDONLY); if (fd < 0) { fprintf(stderr, "Open error "); perror(file_name); errors++; continue; } // Get the file size, to make mmap search the whole thing struct stat file_stat; if (fstat(fd, &file_stat) != 0) { fprintf(stderr, "Stat error "); perror(file_name); errors++; close(fd); // ignore error continue; } size_t file_size = (size_t)file_stat.st_size; if ((file_size == 0) || !S_ISREG(file_stat.st_mode)) { close(fd); // ignore error continue; } const uint8_t *file = mmap(0, file_size, PROT_READ, MAP_SHARED, fd, 0); if (file == MAP_FAILED) { fprintf(stderr, "Mmap error "); perror(file_name); errors++; close(fd); // ignore error continue; } // Find all matches, don't worry about overlaps size_t last = 0; size_t next = 0; while ((next = bm_search(file + last, file_size - last, pattern, pattern_size, delta1, delta2)) != NOT_FOUND) { if (first) { first = 0; printf("---- %s ----\n", file_name); } print_match(file, last + next, pattern_size, before, after, color); last += next + pattern_size; count++; } if (munmap((void*)file, file_size) != 0) { fprintf(stderr, "Unmap error "); perror(file_name); errors++; } if (close(fd) != 0) { fprintf(stderr, "Close error "); perror(file_name); errors++; } } free(delta2); free(delta1); free(pattern); if (count > 0) { return 0; } if (errors > 0) { return 2; } return 1; }
int main(int argc, char *argv[]) { kseq_t *seq; int l, index, shift, min=5, min_keep=35; int debug=0, verbose=1; int contaminated=0, total=0; quality_type qual_type=SANGER; match *best_match; float *qprobs, prior=default_prior; adapter_array *aa; gzFile adapter_fp=NULL, fp; FILE *output_fp=stdout, *matches_fp=NULL; int optc; int add_tag = 0; extern char *optarg; while (1) { int option_index = 0; optc = getopt_long(argc, argv, "dtfp:a:o:q:m:o:n:M:", long_options, &option_index); if (optc == -1) break; switch (optc) { if (long_options[option_index].flag != 0) break; case 'a': adapter_fp = gzopen(optarg, "r"); if (!adapter_fp) { fprintf(stderr, "Could not open adapter file '%s'.\n", optarg); return EXIT_FAILURE; } break; case 'd': debug = 1; break; case 't': add_tag = 1; break; case 'Q': verbose = 0; break; case 'o': output_fp = fopen(optarg, "w"); if (!output_fp) { fprintf(stderr, "Could not open output file '%s'.\n", optarg); return EXIT_FAILURE; } break; case 'm': matches_fp = fopen(optarg, "w"); if (!matches_fp) { fprintf(stderr, "Could not open matches file '%s'.\n", optarg); return EXIT_FAILURE; } break; case 'n': min = atoi(optarg); break; case 'M': min_keep = atoi(optarg); break; case 'q': if (strcmp(optarg, "illumina") == 0) qual_type = ILLUMINA; else if (strcmp(optarg, "solexa") == 0) qual_type = SOLEXA; else if (strcmp(optarg, "sanger") == 0) qual_type = SANGER; else { fprintf(stderr, "Unknown quality type '%s'.\n", optarg); usage(EXIT_FAILURE); } break; case 'p': /* This truncation is acceptable... priors need not be doubles */ prior = (float) atof(optarg); if (prior > 1 || prior < 0) { fprintf(stderr, "Prior must be between 0 and 1\n"); usage(EXIT_FAILURE); } break; case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR(PROGRAM_NAME, VERSION, AUTHORS); case '?': break; default: usage(EXIT_FAILURE); } } if (debug) { matches_fp = stdout; output_fp = stdout; } if ((index = optind) == argc) { fprintf(stderr, "No FASTQ file specified.\n"); usage(EXIT_FAILURE); } /* load all adapter sequences into memory */ if (!adapter_fp) { fprintf(stderr, "No adapter file specified.\n"); usage(EXIT_FAILURE); } aa = load_adapters(adapter_fp); gzclose(adapter_fp); fp = strcmp(argv[index], "-") ? gzopen(argv[index], "r") : gzdopen(fileno(stdin), "r"); if (!fp) { fprintf(stderr, "FASTQ file '%s' not found.\n", argv[index]); return EXIT_FAILURE; } seq = kseq_init(fp); /* Loop through entire sequence file. Write trimmed sequences to file (or stdout), and record matches in a match file if specifed. */ while ((l = kseq_read(seq)) >= 0) { shift = -1; if (!seq->qual.s) { fputs("Sequence file missing or has malformed quality line.\n", stderr); usage(EXIT_FAILURE); } qprobs = qual_to_probs(seq->qual.s, qual_type); best_match = find_best_match(aa, seq->seq.s, qprobs, prior, 0.25, min); if (best_match && best_match->ps->is_contam) { contaminated++; shift = best_match->shift; if (matches_fp) print_match(seq, best_match, matches_fp, aa, qual_type); /* TODO */ /* aa->adapters[best_match->adapter_index].occurrences[best_match->n-1]++; */ } write_fastq(output_fp, seq, add_tag, shift, min_keep); if (best_match) destroy_match(best_match); free(qprobs); total++; } if (verbose) print_summary(aa, prior, total-contaminated, contaminated, total); kseq_destroy(seq); destroy_adapters(aa, MAX_ADAPTERS); gzclose(fp); return 0; }
void _match_pattern_indels(const cachedCharSeq *P, const cachedCharSeq *S, int max_nmis, int fixedP, int fixedS) { int i0, j0, max_nmis1, nedit1, width1; char c0; cachedCharSeq P1; if (P->length <= 0) error("empty pattern"); _select_nmismatch_at_Pshift_fun(fixedP, fixedS); if (!fixedP || !fixedS) error("'fixed' must be TRUE when 'algorithm=\"indels\"' (for now)"); // Before we can support fixedP=FALSE or fixedS=FALSE in // _match_pattern_indels(), we need to support them in // _init_byte2offset_with_cachedCharSeq() and _nedit_for_Ploffset(). _init_byte2offset_with_cachedCharSeq(byte2offset, P, 0); provisory_match_nedit = -1; // means no provisory match yet j0 = 0; while (j0 < S->length) { while (1) { c0 = S->seq[j0]; i0 = byte2offset[(unsigned char) c0]; if (i0 != NA_INTEGER) break; j0++; if (j0 >= S->length) goto done; } P1.seq = P->seq + i0 + 1; P1.length = P->length - i0 - 1; max_nmis1 = max_nmis - i0; /* #ifdef DEBUG_BIOSTRINGS if (debug) { Rprintf("[DEBUG] _match_pattern_indels(): " "j0=%d c0=%c i0=%d max_nmis1=%d\n", j0, c0, i0, max_nmis1); } #endif */ if (max_nmis1 >= 0) { if (max_nmis1 == 0) { nedit1 = _selected_nmismatch_at_Pshift_fun(&P1, S, j0 + 1, max_nmis1); width1 = P1.length; } else { nedit1 = _nedit_for_Ploffset(&P1, S, j0 + 1, max_nmis1, 1, &width1); } if (nedit1 <= max_nmis1) { #ifdef DEBUG_BIOSTRINGS if (debug) { Rprintf("[DEBUG] _match_pattern_indels(): " "provisory match found at "); print_match(j0 + 1, width1 + 1, P, S); } #endif report_provisory_match(j0 + 1, width1 + 1, nedit1 + i0); } } j0++; } done: if (provisory_match_nedit != -1) _report_match(provisory_match_start, provisory_match_width); return; }
static void print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric) { printf("web "); print_match((const struct ipt_web_info *)match->data); }
void _match_pattern_indels(const Chars_holder *P, const Chars_holder *S, int max_nmis, int fixedP, int fixedS) { int i0, j0, max_nmis1, nedit1, width1; char c0; const BytewiseOpTable *bytewise_match_table; Chars_holder P1; if (P->length <= 0) error("empty pattern"); bytewise_match_table = _select_bytewise_match_table(fixedP, fixedS); _init_byte2offset_with_Chars_holder(&byte2offset, P, bytewise_match_table); provisory_match_nedit = -1; // means no provisory match yet j0 = 0; while (j0 < S->length) { while (1) { c0 = S->ptr[j0]; i0 = byte2offset.byte2code[(unsigned char) c0]; if (i0 != NA_INTEGER) break; j0++; if (j0 >= S->length) goto done; } P1.ptr = P->ptr + i0 + 1; P1.length = P->length - i0 - 1; max_nmis1 = max_nmis - i0; /* #ifdef DEBUG_BIOSTRINGS if (debug) { Rprintf("[DEBUG] _match_pattern_indels(): " "j0=%d c0=%c i0=%d max_nmis1=%d\n", j0, c0, i0, max_nmis1); } #endif */ if (max_nmis1 >= 0) { if (max_nmis1 == 0) { nedit1 = _nmismatch_at_Pshift(&P1, S, j0 + 1, max_nmis1, bytewise_match_table); width1 = P1.length; } else { nedit1 = _nedit_for_Ploffset(&P1, S, j0 + 1, max_nmis1, 1, &width1, bytewise_match_table); } if (nedit1 <= max_nmis1) { #ifdef DEBUG_BIOSTRINGS if (debug) { Rprintf("[DEBUG] _match_pattern_indels(): " "provisory match found at "); print_match(j0 + 1, width1 + 1, P, S, bytewise_match_table); } #endif report_provisory_match(j0 + 1, width1 + 1, nedit1 + i0); } } j0++; } done: if (provisory_match_nedit != -1) _report_match(provisory_match_start, provisory_match_width); return; }
static void statistic_save(const void *ip, const struct xt_entry_match *match) { struct xt_statistic_info *info = (struct xt_statistic_info *)match->data; print_match(info, "--"); }
int main (int argc, char *argv[]) { int i, j, n, res; regex_t regex; char buffer[1024]; regmatch_t matches[MAX_MATCHES]; /* At least one argument, -h | --help | <regex> */ if (argc < 2) { usage (argv[0]); exit (1); } /* Help */ if ( (strcmp (argv[1], "-h") == 0) || (strcmp (argv[1], "--help") == 0) ) { usage (argv[0]); exit (0); } /* Show version */ if ( (strcmp (argv[1], "-v") == 0) || (strcmp (argv[1], "--version") == 0) ) { printf ("Pcre version is %s\n", pcre_version()); exit (0); } /* Compile regex */ res = regcomp (®ex, argv[1], 0); if (res != 0) { (void) regerror (res, ®ex, buffer, sizeof(buffer)); fprintf (stderr, "ERROR: Compilation has failed with error: %s\n", buffer); exit (1); } /* Check strings */ for (i = 3; i <= argc; i++) { /* Compile */ res = regexec (®ex, argv[i-1], MAX_MATCHES, matches, 0); if (res == REG_NOMATCH) { printf ("No match\n"); continue; } if (res != 0) { (void) regerror (res, ®ex, buffer, sizeof(buffer)); fprintf (stderr, "ERROR: Execution has failed with error: %s\n", buffer); exit (1); } /* Print result */ print_match (matches[0]); /* Count matching substrings */ n = 0; for (j = MAX_MATCHES - 1; j >= 0; j--) { if (matches[j].rm_so != -1) { n = j; break; } } /* Print matching substrings */ for (j = 1; j <= n; j++) { print_match (matches[j]); } } /* Cleanup */ regfree (®ex); /* Done */ exit (0); }
//TODO: append matches to some data structure instead of just printing them out // then there can be sweet summaries of matches/files scanned/time/etc int search_dir(pcre *re, const char* path, const int depth) { //TODO: don't just die. also make max depth configurable if(depth > MAX_SEARCH_DEPTH) { log_err("Search depth greater than %i, giving up.", depth); exit(1); } struct dirent **dir_list = NULL; struct dirent *dir = NULL; int results = 0; FILE *fp = NULL; int f_len; size_t r_len; char *buf = NULL; int rv = 0; char *dir_full_path = NULL; size_t path_length = 0; results = scandir(path, &dir_list, &ignorefile_filter, &alphasort); if (results > 0) { for (int i = 0; i < results; i++) { dir = dir_list[i]; path_length = (size_t)(strlen(path) + strlen(dir->d_name) + 2); // 2 for slash and null char dir_full_path = malloc(path_length); dir_full_path = strncpy(dir_full_path, path, path_length); dir_full_path = strncat(dir_full_path, "/", path_length); dir_full_path = strncat(dir_full_path, dir->d_name, path_length); load_ignore_patterns(dir_full_path); free(dir); free(dir_full_path); } } free(dir_list); results = scandir(path, &dir_list, &filename_filter, &alphasort); if (results == 0) { log_debug("No results found"); free(dir_list); return(0); } else if (results == -1) { log_err("Error opening directory %s", path); return(0); } for (int i=0; i<results; i++) { dir = dir_list[i]; // XXX: this is copy-pasted from about 30 lines above path_length = (size_t)(strlen(path) + strlen(dir->d_name) + 2); // 2 for slash and null char dir_full_path = malloc(path_length); dir_full_path = strncpy(dir_full_path, path, path_length); dir_full_path = strncat(dir_full_path, "/", path_length); dir_full_path = strncat(dir_full_path, dir->d_name, path_length); log_debug("dir %s type %i", dir_full_path, dir->d_type); //TODO: scan files in current dir before going deeper if (dir->d_type == DT_DIR && opts.recurse_dirs) { log_debug("searching dir %s", dir_full_path); rv = search_dir(re, dir_full_path, depth + 1); goto cleanup; continue; } fp = fopen(dir_full_path, "r"); if (fp == NULL) { log_err("Error opening file %s. Skipping...", dir_full_path); goto cleanup; continue; } rv = fseek(fp, 0, SEEK_END); if (rv != 0) { log_err("Error fseek()ing file %s. Skipping...", dir_full_path); goto cleanup; } f_len = ftell(fp); //TODO: behave differently if file is HUGE. on 32 bit, anything > 2GB will screw up this program if (f_len == 0) { log_debug("file is empty. skipping"); goto cleanup; } rewind(fp); buf = (char*) malloc(sizeof(char) * f_len + 1); r_len = fread(buf, 1, f_len, fp); buf[r_len] = '\0'; int buf_len = (int)r_len; int buf_offset = 0; int offset_vector[MAX_MATCHES_PER_FILE * 2]; //XXXX int rc = 0; char *match_start = NULL; char *match_end = NULL; int first_match = 1; // In my profiling, most of the execution time is spent in this pcre_exec while(buf_offset < buf_len && (rc = pcre_exec(re, NULL, buf, r_len, buf_offset, 0, offset_vector, sizeof(offset_vector))) >= 0 ) { log_debug("Match found. File %s, offset %i bytes.", dir_full_path, offset_vector[0]); match_start = buf + offset_vector[0]; match_end = buf + offset_vector[1]; buf_offset = offset_vector[1]; print_match(dir_full_path, buf, match_start, match_end, first_match); first_match = 0; } free(buf); cleanup: if (fp != NULL) { fclose(fp); } free(dir); free(dir_full_path); } free(dir_list); return(0); }
void fix_adrb(struct tree **root) { FILE *fp; char chose[MAXLEN]; char items[2][MAXLEN]; struct adrb *data[MAXLEN]; struct tree **view; struct tree *temp; int loop_main, loop_sub, flag_x; int i, j, num; data[0] = NULL; loop_main = 0; while( loop_main != 1 ) { loop_sub = 0; while(loop_sub != 1) { loop_sub = 1; printf("修正する住所録を検索します…\n"); printf("修正したい住所録の「氏名」を入力してください。終了するには「Ctrlキーを押しながらDを押してください」。\n"); printf("> "); if( scanf("%s", items[0] ) == EOF ) { printf("住所録の修正を終了します…\n\n"); return; } } i = 0; print_match(*root, data, items[0], 0, &i); if( data[0] == NULL ) { printf("一致する住所録はありませんでした…\n\n"); continue; } else { printf("一致する住所録があります…\n"); for(j = 0; j < i; j++) printf("・%2d: %s\t%s\t%s\t%s\t%s\n", j+1, data[j]->name, data[j]->posc, data[j]->adre, data[j]->tell, data[j]->mail); } loop_sub = 0; while( loop_sub != 1 ) { printf("\nどの住所録を修正しますか?左の番号を入力してください。修正せずに終了するには「Ctrlキーを押しながらDを押してください」。\n"); printf("> "); if( scanf("%s", chose) == EOF ) { printf("住所録の修正を終了します…\n\n"); return; }; if(strlen(chose) != 1) { printf("入力エラーです!\n"); continue; } if( atoi(chose) < 1 || i < atoi(chose) ) { printf("入力エラーです!\n"); continue; } printf("次の住所録が選択されました。\n"); printf("・%2d: %s\t%s\t%s\t%s\t%s\n", atoi(chose), data[atoi(chose)-1]->name, data[atoi(chose)-1]->posc, data[atoi(chose)-1]->adre, data[atoi(chose)-1]->tell, data[atoi(chose)-1]->mail); num = atoi(chose) - 1; loop_sub = 1; } loop_sub = 0; while( loop_sub != 1 ) { printf("\nどの項目を修正しますか?番号を入力してください。修正せずに終了するには5を入力してください。\n"); printf("0: 氏名\n1: 郵便番号\n2: 住所\n3: 電話番号\n4: メールアドレス\n"); printf("> "); if( scanf("%s", chose) == EOF ) { printf("入力エラーです!\n"); continue; }; if(strlen(chose) != 1) { printf("入力エラーです!\n"); continue; } switch(chose[0]) { case '0': loop_sub = 1; break; case '1': loop_sub = 1; break; case '2': loop_sub = 1; break; case '3': loop_sub = 1; break; case '4': loop_sub = 1; break; case '5': printf("住所録の修正を終了します…\n\n"); return; default: printf("入力エラーです!\n"); break; } } loop_sub = 0; while(loop_sub != 1) { loop_sub = 1; printf("修正してください。\n"); printf("> "); if( scanf("%s", items[1]) == EOF ) { printf("入力エラーです!\n\n"); loop_sub = 0; continue; } } fix_item( data[num], items[1], atoi(chose) ); printf("修正完了\n"); print_adrb(*root); printf("\n"); fp = fopen( "adrb.dat", "w"); if( fp == NULL ) { return; } save_file( fp, root ); fclose(fp); loop_sub = 0; while( loop_sub != 1 ) { printf("住所録の修正を続けますか?番号を入力してください!\n"); printf("0: Yes\n1: No\n"); printf("> "); if( scanf("%s", chose) == EOF ) { printf("入力エラーです!\n"); continue; }; if( strlen(chose) != 1) { printf("入力エラーです。\n"); continue; } switch(chose[0]) { case '0': loop_sub = 1; break; case '1': loop_sub = loop_main = 1; break; default: printf("入力エラーです!\n"); break; } } } printf("住所録の修正を終了します…\n\n"); }