/* * Search forward. Get a search string from the user, and search, beginning at * ".", for the string. If found, reset the "." to be just after the match * string, and [perhaps] repaint the display. Bound to "C-S" */ int forwsearch (int f, int n) { int status=FALSE; if (n == 0) /* resolve the repeat count */ n = 1; if (n < 1) /* search backwards */ return (backsearch (f, -n)); /* ask the user for the text of a pattern */ if ((status = readpattern ("Search")) != TRUE) return (status); /* search for the pattern */ while (n-- > 0) { if ((status = forscan (&pat[0], PTEND)) == FALSE) break; } /* and complain if not there */ if (status == FALSE) mlwrite ("Not found"); return (status); }
/* ARGSUSED */ int replstr(int f, int n) { char news[NPAT]; int s, plen, rcnt = 0; char *r; if ((s = readpattern("Replace string")) != TRUE) return s; r = eread("Replace string %s with: ", news, NPAT, EFNUL | EFNEW | EFCR, pat); if (r == NULL) return (ABORT); plen = strlen(pat); while (forwsrch() == TRUE) { update(); if (lreplace((RSIZE)plen, news) == FALSE) return (FALSE); rcnt++; } curwp->w_rflag |= WFFULL; update(); if (rcnt == 1) ewprintf("Replaced 1 occurrence"); else ewprintf("Replaced %d occurrences", rcnt); return (TRUE); }
/* ARGSUSED */ int scrsearchpat(int f GCC_UNUSED, int n GCC_UNUSED) { int status; TBUFF *temp = 0; status = readpattern("", &searchpat, &gregexp, EOS, TRUE); temp = tb_visbuf(tb_values(searchpat), tb_length(searchpat)); mlwrite("Search pattern is now %s", temp ? tb_values(temp) : "null"); tb_free(&temp); last_srch_direc = FORWARD; return status; }
/*ARGSUSED*/ forwsearch(f, n) { register int s; if ((s=readpattern("Search")) != TRUE) return s; if (forwsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); return FALSE; } srch_lastdir = SRCH_FORW; return TRUE; }
/*ARGSUSED*/ backsearch(f, n) { register int s; if ((s=readpattern("Search backward")) != TRUE) return (s); if (backsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); return FALSE; } srch_lastdir = SRCH_BACK; return TRUE; }
/* * Reverse search. Get a search string from the user, and search, starting at * "." and proceeding toward the front of the buffer. If found "." is left * pointing at the first character of the pattern [the last character that was * matched]. Bound to "C-R" */ int backsearch (int f, int n) { int s; if (n == 0) /* resolve null and negative arguments */ n = 1; if (n < 1) return (forwsearch (f, -n)); if ((s = readpattern("Reverse search")) != TRUE)/* get a pattern to search */ return (s); return bsearch (f, n); /* and go search for it */ }
/* ARGSUSED */ int backsearch(int f, int n) { int s; if ((s = readpattern("Search backward")) != TRUE) return (s); if (backsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); return (FALSE); } srch_lastdir = SRCH_BACK; return (TRUE); }
/* ARGSUSED */ int forwsearch(int f, int n) { int s; if ((s = readpattern("Search")) != TRUE) return (s); if (forwsrch() == FALSE) { ewprintf("Search failed: \"%s\"", pat); return (FALSE); } srch_lastdir = SRCH_FORW; return (TRUE); }
/* * replaces: search for a string and replace it with another string. query * might be enabled (according to kind) */ int replaces (int kind, int f, int n) { LINE *origline; /* original "." position */ char tmpc; /* temporary character */ char c; /* input char for query */ char tpat[NPAT]; /* temporary to hold search pattern */ int i; /* loop index */ int s; /* success flag on pattern inputs */ int slength, rlength; /* length of search and replace strings */ int numsub; /* number of substitutions */ int nummatch; /* number of found matches */ int nlflag; /* last char of search string a <NL>? */ int nlrepl; /* was a replace done on the last line? */ int origoff; /* and offset (for . query option) */ /* check for negative repititions */ if (f && n < 0) return (FALSE); /* ask the user for the text of a pattern */ if ((s = readpattern ((kind == FALSE ? "Replace" : "Query replace"))) != TRUE) return (s); strncpy (&tpat[0], &pat[0], NPAT); /* salt it away */ /* ask for the replacement string */ strncpy (&pat[0], &rpat[0], NPAT); /* set up default string */ if ((s = readpattern ("with")) == ABORT) return (s); /* move everything to the right place and length them */ strncpy (&rpat[0], &pat[0], NPAT); strncpy (&pat[0], &tpat[0], NPAT); slength = strlen (&pat[0]); rlength = strlen (&rpat[0]); /* set up flags so we can make sure not to do a recursive replace on the * last line */ nlflag = (pat[slength - 1] == '\n'); nlrepl = FALSE; /* build query replace question string */ strncpy (tpat, "Replace '", 10); expandp (&pat[0], &tpat[strlen (tpat)], NPAT / 3); strncat (tpat, "' with '", 9); expandp (&rpat[0], &tpat[strlen (tpat)], NPAT / 3); strncat (tpat, "'? ", 4); /* save original . position */ origline = curwp->w_dotp; origoff = curwp->w_doto; /* scan through the file */ numsub = 0; nummatch = 0; while ((f == FALSE || n > nummatch) && (nlflag == FALSE || nlrepl == FALSE)) { /* search for the pattern */ if (forscan (&pat[0], PTBEG) != TRUE) break; /* all done */ ++nummatch; /* increment # of matches */ /* check if we are on the last line */ nlrepl = (lforw (curwp->w_dotp) == curwp->w_bufp->b_linep); /* check for query */ if (kind) { /* get the query */ mlwrite (&tpat[0], &pat[0], &rpat[0]); qprompt: update (); /* show the proposed place to change */ c = (*term.t_getchar) (); /* and input */ mlwrite (""); /* and clear it */ /* and respond appropriately */ switch (c) { case 'y': /* yes, substitute */ case ' ': break; case 'n': /* no, onword */ forwchar (FALSE, 1); continue; case '!': /* yes/stop asking */ kind = FALSE; break; case '.': /* abort! and return */ /* restore old position */ curwp->w_dotp = origline; curwp->w_doto = origoff; curwp->w_flag |= WFMOVE; case BELL: /* abort! and stay */ mlwrite ("Aborted!"); return (FALSE); case 0x0d: /* controlled exit */ case 'q': return (TRUE); default: /* bitch and beep */ (*term.t_beep) (); case '?': /* help me */ mlwrite ("(Y)es, (N)o, (!)Do the rest, (^G,RET,q)Abort, (.)Abort back, (?)Help: "); goto qprompt; } } /* delete the sucker */ if (ldelete (slength, FALSE) != TRUE) { /* error while deleting */ mlwrite ("ERROR while deleteing"); return (FALSE); } /* and insert its replacement */ for (i = 0; i < rlength; i++) { tmpc = rpat[i]; s = (tmpc == '\n' ? lnewline () : linsert (1, tmpc)); if (s != TRUE) { /* error while inserting */ mlwrite ("Out of memory while inserting"); return (FALSE); } } numsub++; /* increment # of substitutions */ } /* and report the results */ mlwrite ("%d substitutions", numsub); return (TRUE); }
/* extra args -- marking if called from globals, and should mark lines, and fromscreen, if the searchpattern is on the screen, so we don't need to ask for it. */ int fsearch(int f, int n, int marking, int fromscreen) { int status = TRUE; int wrapok; MARK curpos; int didmark = FALSE; int didwrap; assert(curwp != 0); if (f && n < 0) return rsearch(f, -n, FALSE, FALSE); if (n == 0) n = 1; wrapok = marking || window_b_val(curwp, MDWRAPSCAN); last_srch_direc = FORWARD; /* ask the user for a regular expression to search for. if * "marking", then we were called to do line marking for the * global command. */ if (!marking) { status = readpattern("Search: ", &searchpat, &gregexp, lastkey, fromscreen); if (status != TRUE) return status; } ignorecase = window_b_val(curwp, MDIGNCASE); if (curwp == 0) return FALSE; curpos = DOT; scanboundry(wrapok, curpos, FORWARD); didwrap = FALSE; while (marking || n--) { movenext(&(DOT), FORWARD); status = scanner(gregexp, FORWARD, wrapok, &didwrap); if (status == ABORT) { mlwarn("[Aborted]"); DOT = curpos; return status; } /* if found, mark the line */ if (status && marking) { /* if we were on a match when we started, then scanner returns TRUE, even though it's on a boundary. quit if we find ourselves marking a line twice */ if (lismarked(DOT.l)) break; lsetmarked(DOT.l); /* and, so the next movenext gets to next line */ DOT.o = llength(DOT.l); didmark = TRUE; } if (!marking && didwrap) { mlwrite("[Search wrapped past end of buffer]"); didwrap = FALSE; } if (status != TRUE) break; } if (!marking && !status) movenext(&(DOT), REVERSE); if (marking) { /* restore dot and offset */ DOT = curpos; } else if (status) { savematch(DOT, gregexp->mlen); if (samepoint(DOT, curpos)) { mlwrite(onlyonemsg); } } /* Complain if not there. */ if ((marking && didmark == FALSE) || (!marking && status == FALSE)) { not_found_msg(wrapok, FORWARD); return FALSE; } attrib_matches(); return TRUE; }
/* ARGSUSED */ static int rsearch(int f, int n, int dummy GCC_UNUSED, int fromscreen) { int status; int wrapok; MARK curpos; int didwrap; assert(curwp != 0); if (f && n < 0) /* reverse direction */ return fsearch(f, -n, FALSE, fromscreen); if (n == 0) n = 1; wrapok = window_b_val(curwp, MDWRAPSCAN); last_srch_direc = REVERSE; /* ask the user for the regular expression to search for, and * find n'th occurrence. */ status = readpattern("Reverse search: ", &searchpat, &gregexp, EOS, fromscreen); if (status != TRUE) return status; ignorecase = window_b_val(curwp, MDIGNCASE); if (curwp == 0) return FALSE; curpos = DOT; scanboundry(wrapok, DOT, REVERSE); didwrap = FALSE; while (n--) { movenext(&(DOT), REVERSE); status = scanner(gregexp, REVERSE, wrapok, &didwrap); if (didwrap) { mlwrite( "[Search wrapped past start of buffer]"); didwrap = FALSE; } if (status != TRUE) break; } if (status == TRUE) { savematch(DOT, gregexp->mlen); if (samepoint(DOT, curpos)) { mlwrite(onlyonemsg); } } else if (status == FALSE) { movenext(&(DOT), FORWARD); not_found_msg(wrapok, REVERSE); } else if (status == ABORT) { mlwarn("[Aborted]"); DOT = curpos; return status; } attrib_matches(); return status; }
/* ARGSUSED */ int queryrepl(int f, int n) { int s; int rcnt = 0; /* replacements made so far */ int plen; /* length of found string */ char news[NPAT], *rep; /* replacement string */ #ifndef NO_MACRO if (macrodef) { ewprintf("Can't query replace in macro"); return (FALSE); } #endif /* !NO_MACRO */ if ((s = readpattern("Query replace")) != TRUE) return (s); if ((rep = eread("Query replace %s with: ", news, NPAT, EFNUL | EFNEW | EFCR, pat)) == NULL) return (ABORT); else if (rep[0] == '\0') news[0] = '\0'; ewprintf("Query replacing %s with %s:", pat, news); plen = strlen(pat); /* * Search forward repeatedly, checking each time whether to insert * or not. The "!" case makes the check always true, so it gets put * into a tighter loop for efficiency. */ while (forwsrch() == TRUE) { retry: update(); switch (getkey(FALSE)) { case 'y': case ' ': if (lreplace((RSIZE)plen, news) == FALSE) return (FALSE); rcnt++; break; case '.': if (lreplace((RSIZE)plen, news) == FALSE) return (FALSE); rcnt++; goto stopsearch; /* ^G, CR or ESC */ case CCHR('G'): (void)ctrlg(FFRAND, 0); goto stopsearch; case CCHR('['): case CCHR('M'): goto stopsearch; case '!': do { if (lreplace((RSIZE)plen, news) == FALSE) return (FALSE); rcnt++; } while (forwsrch() == TRUE); goto stopsearch; case 'n': case CCHR('H'): /* To not replace */ case CCHR('?'): break; default: ewprintf("y/n or <SP>/<DEL>: replace/don't, [.] repl-end, [!] repl-rest, <CR>/<ESC> quit"); goto retry; } } stopsearch: curwp->w_rflag |= WFFULL; update(); if (rcnt == 1) ewprintf("Replaced 1 occurrence"); else ewprintf("Replaced %d occurrences", rcnt); return (TRUE); }
int main(int argc, char* argv[]){ int i, j, k, count = 0, pah, paw, flag = 0; char s[256]; char *fileName = s; /* malloc the space */ for (i = 0; i<11; i++) { prototype[i].data = (int**)malloc(SIZE * sizeof(int*)); for (j = 0; j < SIZE; j++) { prototype[i].data[j] = (int*)malloc(SIZE * sizeof(int)); } } /* initialization */ for (i = 0; i<10; i++) { for (j = 0; j<SIZE; j++) for (k = 0; k<SIZE; k++) prototype[i].data[j][k] = 0; } /* open the directory */ DIR * dirp; struct dirent * p; /* open pwd or sub */ if (1 == argc){ dirp = opendir("."); } else { dirp = opendir(argv[1]); } /* read the directory */ while ((p=readdir(dirp)) != NULL){ /* if the file is a .dat file(8) */ if (p->d_name[0] != '.' && p->d_type == DT_REG && ends_in_dat(p->d_name)) { /* read the data in */ if (1 == argc){ strcpy(s, "."); } else { sprintf(s, "./%s", argv[1]); } sprintf(s, "%s/%s", s, p->d_name); //default: relative path if (readpattern(fileName) && !flag) { count++; } else if(readpattern(argv[1])) { //absolute path flag = 1; count++; } } } closedir(dirp); /* roubust: if nothing read, end */ if (!count) { return -1; } /* normalization and make prototype */ for (i = 0; i<10*100; i++) { int num = mojidata[i].value; /* initialization */ for (j = 0; j < SIZE; j++) { for (k = 0; k < SIZE; k++) prototype[10].data[j][k] = 0; } /* standardization */ if ((mojidata[i].height % SIZE == 0) && (mojidata[i].width % SIZE == 0)) { pah = mojidata[i].height / SIZE; paw = mojidata[i].width / SIZE; for (j = 0; j < mojidata[i].height; j++) { for (k = 0; k < mojidata[i].width; k++) { if (mojidata[i].data[j][k]) { prototype[10].data[j/pah][k/paw]++; } } } } else { double ratioh; double ratiow; /* set pah, paw, ratioh, ratiow according to % */ if (mojidata[i].height % SIZE == 0) { pah = mojidata[i].height / SIZE; ratioh = 1; paw = mojidata[i].width / SIZE + 1; ratiow = (double)mojidata[i].width / (paw * SIZE) ; } else if(mojidata[i].width % SIZE == 0) { paw = mojidata[i].width / SIZE; ratiow = 1; pah = mojidata[i].height / SIZE + 1; ratioh = (double)mojidata[i].height / (pah * SIZE); } else { pah = mojidata[i].height / SIZE + 1; paw = mojidata[i].width / SIZE + 1; ratioh = (double)mojidata[i].height / (pah * SIZE); ratiow = (double)mojidata[i].width / (paw * SIZE) ; } for (j = 0; j < mojidata[i].height; j++) { for (k = 0; k < mojidata[i].width; k++) { if (mojidata[i].data[j][k]) { int jprime = (int)floor(j/ratioh)/pah; int kprime = (int)floor(k/ratiow)/paw; prototype[10].data[jprime][kprime]++; } } } } /* normalization */ for (j = 0; j < SIZE; j++) { for (k = 0; k < SIZE; k++) { prototype[10].data[j][k] *= 10000/ (pah * paw * SIZE * SIZE); } } /* show */ printf("class is %d\n", num); for (j = 0; j < SIZE; j++) { for (k = 0; k < SIZE; k++) { printf("%d ", prototype[10].data[j][k]); } printf("\n"); } printf("\n"); /* add to prototype */ for (j = 0; j < SIZE; j++) { for (k = 0; k < SIZE; k++) { prototype[num].data[j][k] += prototype[10].data[j][k]; } } } /* write the prototype */ for(i = 0; i < 10; i++) { writeToFile(i); } /* free the space */ for (i = 0; i < 11; i++) { for (j = 0; j < SIZE; j++) { free(prototype[i].data[j]); } free(prototype[i].data); } for (i = 0; i < 10 * 100; i++) { for (j = 0; j < mojidata[i].height; j++) { free(mojidata[i].data[j]); } free(mojidata[i].data); } }
int forwsearch(int f, int n) { int status; int wrapt = FALSE, wrapt2 = FALSE; int repl_mode = FALSE; UCS defpat[NPAT]; int search = FALSE; EML eml; /* resolve the repeat count */ if (n == 0) n = 1; if (n < 1) /* search backwards */ FWS_RETURN(0); defpat[0] = '\0'; /* ask the user for the text of a pattern */ while(1){ if (gmode & MDREPLACE) status = srpat("Search", defpat, NPAT, repl_mode); else status = readpattern("Search", TRUE); switch(status){ case TRUE: /* user typed something */ search = TRUE; break; case HELPCH: /* help requested */ if(Pmaster){ VARS_TO_SAVE *saved_state; saved_state = save_pico_state(); (*Pmaster->helper)(Pmaster->search_help, _("Help for Searching"), 1); if(saved_state){ restore_pico_state(saved_state); free_pico_state(saved_state); } } else pico_help(SearchHelpText, _("Help for Searching"), 1); case (CTRL|'L'): /* redraw requested */ pico_refresh(FALSE, 1); update(); break; case (CTRL|'V'): gotoeob(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'Y'): gotobob(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'T') : switch(status = readnumpat(_("Search to Line Number : "))){ case -1 : emlwrite(_("Search to Line Number Cancelled"), NULL); FWS_RETURN(FALSE); case 0 : emlwrite(_("Line number must be greater than zero"), NULL); FWS_RETURN(FALSE); case -2 : emlwrite(_("Line number must contain only digits"), NULL); FWS_RETURN(FALSE); case -3 : continue; default : gotoline(0, status); mlerase(); FWS_RETURN(TRUE); } break; case (CTRL|'W'): { LINE *linep = curwp->w_dotp; int offset = curwp->w_doto; gotobop(0, 1); gotobol(0, 1); /* * if we're asked to backup and we're already * */ if((lastflag & CFSRCH) && linep == curwp->w_dotp && offset == curwp->w_doto && !(offset == 0 && lback(linep) == curbp->b_linep)){ backchar(0, 1); gotobop(0, 1); gotobol(0, 1); } } mlerase(); FWS_RETURN(TRUE); case (CTRL|'O'): if(curwp->w_dotp != curbp->b_linep){ gotoeop(0, 1); forwchar(0, 1); } mlerase(); FWS_RETURN(TRUE); case (CTRL|'U'): fillbuf(0, 1); mlerase(); FWS_RETURN(TRUE); case (CTRL|'R'): /* toggle replacement option */ repl_mode = !repl_mode; break; default: if(status == ABORT) emlwrite(_("Search Cancelled"), NULL); else mlerase(); FWS_RETURN(FALSE); } /* replace option is disabled */ if (!(gmode & MDREPLACE)){ ucs4_strncpy(defpat, pat, NPAT); defpat[NPAT-1] = '\0'; break; } else if (search){ /* search now */ ucs4_strncpy(pat, defpat, NPAT); /* remember this search for the future */ pat[NPAT-1] = '\0'; break; } } /* * This code is kind of dumb. What I want is successive C-W 's to * move dot to successive occurences of the pattern. So, if dot is * already sitting at the beginning of the pattern, then we'll move * forward a char before beginning the search. We'll let the * automatic wrapping handle putting the dot back in the right * place... */ status = 0; /* using "status" as int temporarily! */ while(1){ if(defpat[status] == '\0'){ forwchar(0, 1); break; /* find next occurence! */ } if(status + curwp->w_doto >= llength(curwp->w_dotp) || !eq(defpat[status],lgetc(curwp->w_dotp, curwp->w_doto + status).c)) break; /* do nothing! */ status++; } /* search for the pattern */ while (n-- > 0) { if((status = forscan(&wrapt,defpat,NULL,0,PTBEG)) == FALSE) break; } /* and complain if not there */ if (status == FALSE){ char *utf8; UCS x[1]; x[0] = '\0'; utf8 = ucs4_to_utf8_cpystr(defpat ? defpat : x); /* TRANSLATORS: reporting the result of a failed search */ eml.s = utf8; emlwrite(_("\"%s\" not found"), &eml); if(utf8) fs_give((void **) &utf8); } else if((gmode & MDREPLACE) && repl_mode == TRUE){ status = replace_pat(defpat, &wrapt2); /* replace pattern */ if (wrapt == TRUE || wrapt2 == TRUE){ eml.s = (status == ABORT) ? "cancelled but wrapped" : "Wrapped"; emlwrite("Replacement %s", &eml); } } else if(wrapt == TRUE){ emlwrite("Search Wrapped", NULL); } else if(status == TRUE){ emlwrite("", NULL); } FWS_RETURN(status); }