/* Recover output if possible */ void recover_output() { off_t lastout = -1; int outvsiz, recsiz; char *outvfmt; int i, j; MODCONT *mp; int ofl; char oname[1024]; LUENT *oent; STREAMOUT sout; off_t nvals; int xr, yr; switch (outfmt) { case 'a': error(USER, "cannot recover ASCII output"); return; case 'f': outvsiz = sizeof(float)*3; break; case 'd': outvsiz = sizeof(double)*3; break; case 'c': outvsiz = sizeof(COLR); break; default: error(INTERNAL, "botched output format"); return; } outvfmt = formstr(outfmt); /* check modifier outputs */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot recover from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot recover from command"); for (j = 0; ; j++) { /* check each bin's file */ ofl = ofname(oname, mp->outspec, mp->modname, j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { sout.reclen = 0; sout.outpipe = 0; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ if (ofl & OF_BIN) continue; break; } /* open output */ sout.ofp = fopen(oname, "rb+"); if (sout.ofp == NULL) { if (j == mp->nbins) break; /* assume end of modifier */ sprintf(errmsg, "missing recover file '%s'", oname); error(WARNING, errmsg); break; } nvals = lseek(fileno(sout.ofp), 0, SEEK_END); if (nvals <= 0) { lastout = 0; /* empty output, quit here */ fclose(sout.ofp); break; } if (!sout.reclen) { if (!(ofl & OF_BIN)) { sprintf(errmsg, "need -bn to recover file '%s'", oname); error(USER, errmsg); } recsiz = outvsiz; } else recsiz = outvsiz * sout.reclen; lseek(fileno(sout.ofp), 0, SEEK_SET); if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } sout.xr = xres; sout.yr = yres; if ((sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; if ((lastout < 0) | (nvals < lastout)) lastout = nvals; if (oent->key == NULL) /* new entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); if (oent->data == NULL) oent->data = (char *)malloc(sizeof(STREAMOUT)); *(STREAMOUT *)oent->data = sout; if (!(ofl & OF_BIN)) break; /* no bin separation */ } if (!lastout) { /* empty output */ error(WARNING, "no previous data to recover"); lu_done(&ofiletab); /* reclose all outputs */ return; } if (j > mp->nbins) { /* check modifier size */ sprintf(errmsg, "mismatched -bn setting for recovering '%s'", modname[i]); error(USER, errmsg); } } if (lastout < 0) { error(WARNING, "no output files to recover"); return; } if (raysleft && lastout >= raysleft/accumulate) { error(WARNING, "output appears to be complete"); /* XXX should read & discard input? */ quit(0); } /* seek on all files */ nvals = lastout * outvsiz; lu_doall(&ofiletab, &myseeko, &nvals); /* skip repeated input */ lastout *= accumulate; for (nvals = 0; nvals < lastout; nvals++) { FVECT vdummy; if (getvec(vdummy) < 0 || getvec(vdummy) < 0) error(USER, "unexpected EOF on input"); } lastray = lastdone = (RNUMBER)lastout; if (raysleft) raysleft -= lastray; }
/* Load previously accumulated values */ void reload_output() { int i, j; MODCONT *mp; int ofl; char oname[1024]; char *fmode = "rb"; char *outvfmt; LUENT *oent; int xr, yr; STREAMOUT sout; DCOLOR rgbv; if (outfmt == 'a') fmode = "r"; outvfmt = formstr(outfmt); /* reload modifier values */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot reload from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot reload from command"); for (j = 0; ; j++) { /* load each modifier bin */ ofl = ofname(oname, mp->outspec, mp->modname, j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { sout.reclen = 0; sout.outpipe = 0; sout.xr = xres; sout.yr = yres; sout.ofp = NULL; } if (sout.ofp == NULL) { /* open output as input */ sout.ofp = fopen(oname, fmode); if (sout.ofp == NULL) { if (j == mp->nbins) break; /* assume end of modifier */ sprintf(errmsg, "missing reload file '%s'", oname); error(WARNING, errmsg); break; } #ifdef getc_unlocked flockfile(sout.ofp); #endif if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } if ((sout.xr > 0) & (sout.yr > 0) && (!fscnresolu(&xr, &yr, sout.ofp) || (xr != sout.xr) | (yr != sout.yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } } /* read in RGB value */ if (!get_contrib(rgbv, sout.ofp)) { if (!j) { fclose(sout.ofp); break; /* ignore empty file */ } if (j < mp->nbins) { sprintf(errmsg, "missing data in '%s'", oname); error(USER, errmsg); } break; } if (j >= mp->nbins) { /* check modifier size */ sprintf(errmsg, "mismatched -bn setting for reloading '%s'", modname[i]); error(USER, errmsg); } copycolor(mp->cbin[j], rgbv); if (oent->key == NULL) /* new file entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); if (oent->data == NULL) oent->data = (char *)malloc(sizeof(STREAMOUT)); *(STREAMOUT *)oent->data = sout; } } lu_doall(&ofiletab, &myclose, NULL); /* close all files */ }
int digger (char fname[], unsigned juid_per_ip, float factor1, long long lifetime) { time_t start, end; int ip_cur=0; long long diff, diff_total; vector <long long> vec; means result; ifstream fin(fname); string ofname(fname), type0_1_5, type3_4, line; ip_filter ip_f (3, 0, 256); /* When `all' was used, make sure that we have one IP or more. */ unsigned all = 0, old = 0; ofstream ferr(DBG_LOG); size_t pos = ofname.find_first_of("."); ofname = ofname.substr(0, pos); type0_1_5 = ofname + POSTFIX0_1_5; type3_4 = ofname + POSTFIX3_4; ofname += POSTFIX2; ofstream fout(ofname.c_str()), fout0_1_5(type0_1_5.c_str()), fout3_4(type3_4.c_str()); if (!fin.is_open()){ cerr << "Can't open file " << fname << end; ferr << "Can't open file " << fname << end; exit(-1); } time(&start); while(getline(fin, line).good()){ // Get the first IP data that we expect. long long time_juid, vtime; int ip; if(sscanf(line.c_str(), "%d\t%13lld%*[^\t]\t%lld", &ip, &time_juid, &vtime)){ //Three values are scanned. if(!ip_f.ok (ip)){ ip_cur = ip; diff = vtime - time_juid; all += 1; diff_total = diff; vec.push_back(diff); if(diff >= lifetime) old += 1; break;//The first IP was got. } else{//skip a line //cerr << "The line will be skipped as for the IP of this line is not `ok' \n" // << line << endl; continue; } } else{ cerr << "Failed to parse the line as follow" << endl << line << endl; continue; } } if(!all){ cerr << "Can't get a line as we expect from the input file `" << fname << endl << "Do NOTHING and exit" << endl; exit(-1); } while(getline(fin, line).good()){ //Do for all. long long time_juid, vtime; int ip; if(sscanf(line.c_str(), "%d\t%13lld%*[^\t]\t%lld", &ip, &time_juid, &vtime) == 3){//skip the last 4 random digits (dec) if(!ip_f.ok (ip)){ diff = vtime - time_juid; if(ip == ip_cur){ all += 1; diff_total += diff; vec.push_back(diff); if(diff >= lifetime) old += 1; } else{ result = fetchmeans(vec); if(all > juid_per_ip){ if((float)old/all < factor1) fout << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; else fout3_4 << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; } else{ fout0_1_5 << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; } all = 1; diff_total = diff; vec.clear(); vec.push_back(diff); if(diff >= lifetime) old = 1; else old = 0; ip_cur = ip; } } } } result = fetchmeans(vec); if(all > juid_per_ip){ if((float)old/all < factor1) fout << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; else fout3_4 << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; } else{ fout0_1_5 << ip_cur << '\t' << all << '\t' << old << '\t' << diff_total/all << '\t' << result.lower << '\t' << result.upper << '\n'; } time (&end); cout << "Cost " << difftime (end, start) << " seconds totally." << endl; return 0; }
/* Get output stream pointer (open and write header if new and noopen==0) */ STREAMOUT * getostream(const char *ospec, const char *mname, int bn, int noopen) { static STREAMOUT stdos; char info[1024]; int ofl; char oname[1024]; LUENT *lep; STREAMOUT *sop; char *cp; if (ospec == NULL) { /* use stdout? */ if (!noopen & !using_stdout) { if (outfmt != 'a') SET_FILE_BINARY(stdout); if (header) { cp = info; if (yres > 0) { sprintf(cp, "NROWS=%d\n", yres * (xres + !xres) ); while (*cp) ++cp; } if ((xres <= 0) | (stdos.reclen > 1)) sprintf(cp, "NCOLS=%d\n", stdos.reclen); printheader(stdout, info); } if (stdos.reclen == 1) printresolu(stdout, xres, yres); if (waitflush > 0) fflush(stdout); stdos.xr = xres; stdos.yr = yres; #ifdef getc_unlocked flockfile(stdout); /* avoid lock/unlock overhead */ #endif using_stdout = 1; } stdos.ofp = stdout; stdos.reclen += noopen; return(&stdos); } ofl = ofname(oname, ospec, mname, bn); /* get output name */ if (ofl < 0) { sprintf(errmsg, "bad output format '%s'", ospec); error(USER, errmsg); } lep = lu_find(&ofiletab, oname); /* look it up */ if (lep->key == NULL) /* new entry */ lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); sop = (STREAMOUT *)lep->data; if (sop == NULL) { /* allocate stream */ sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); if (sop == NULL) error(SYSTEM, "out of memory in getostream"); sop->outpipe = oname[0] == '!'; sop->reclen = 0; sop->ofp = NULL; /* open iff noopen==0 */ sop->xr = xres; sop->yr = yres; lep->data = (char *)sop; if (!sop->outpipe & !force_open & !recover && access(oname, F_OK) == 0) { errno = EEXIST; /* file exists */ goto openerr; } } if (!noopen && sop->ofp == NULL) { /* open output stream */ if (oname[0] == '!') /* output to command */ sop->ofp = popen(oname+1, "w"); else /* else open file */ sop->ofp = fopen(oname, "w"); if (sop->ofp == NULL) goto openerr; if (outfmt != 'a') SET_FILE_BINARY(sop->ofp); #ifdef getc_unlocked flockfile(sop->ofp); /* avoid lock/unlock overhead */ #endif if (accumulate > 0) { /* global resolution */ sop->xr = xres; sop->yr = yres; } if (header) { cp = info; if (ofl & OF_MODIFIER || sop->reclen == 1) { sprintf(cp, "MODIFIER=%s\n", mname); while (*cp) ++cp; } if (ofl & OF_BIN) { sprintf(cp, "BIN=%d\n", bn); while (*cp) ++cp; } if (sop->yr > 0) { sprintf(cp, "NROWS=%d\n", sop->yr * (sop->xr + !sop->xr) ); while (*cp) ++cp; } if ((sop->xr <= 0) | (sop->reclen > 1)) sprintf(cp, "NCOLS=%d\n", sop->reclen); printheader(sop->ofp, info); } if (sop->reclen == 1) printresolu(sop->ofp, sop->xr, sop->yr); if (waitflush > 0) fflush(sop->ofp); } sop->reclen += noopen; /* add to length if noopen */ return(sop); /* return output stream */ openerr: sprintf(errmsg, "cannot open '%s' for writing", oname); error(SYSTEM, errmsg); return(NULL); /* pro forma return */ }
/* Load previously accumulated values */ void reload_output() { int i, j; MODCONT *mp; int ofl; char oname[1024]; char *fmode = "rb"; char *outvfmt; LUENT *oent; int xr, yr; STREAMOUT *sop; DCOLOR rgbv; if (outfmt == 'a') fmode = "r"; outvfmt = formstr(outfmt); /* reload modifier values */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; if (mp->outspec == NULL) error(USER, "cannot reload from stdout"); if (mp->outspec[0] == '!') error(USER, "cannot reload from command"); for (j = 0; j < mp->nbins; j++) { /* load each modifier bin */ ofl = ofname(oname, mp->outspec, mp->modname, mp->bin0+j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); if (oent->data == NULL) error(INTERNAL, "unallocated stream in reload_output()"); sop = (STREAMOUT *)oent->data; if (sop->ofp == NULL) { /* open output as input */ sop->ofp = fopen(oname, fmode); if (sop->ofp == NULL) { sprintf(errmsg, "missing reload file '%s'", oname); error(WARNING, errmsg); break; } #ifdef getc_unlocked flockfile(sop->ofp); #endif if (header && checkheader(sop->ofp, outvfmt, NULL) != 1) { sprintf(errmsg, "format mismatch for '%s'", oname); error(USER, errmsg); } if ((sop->reclen == 1) & (sop->xr > 0) & (sop->yr > 0) && (!fscnresolu(&xr, &yr, sop->ofp) || (xr != sop->xr) | (yr != sop->yr))) { sprintf(errmsg, "resolution mismatch for '%s'", oname); error(USER, errmsg); } } /* read in RGB value */ if (!get_contrib(rgbv, sop->ofp)) { if (!j) { fclose(sop->ofp); break; /* ignore empty file */ } if (j < mp->nbins) { sprintf(errmsg, "missing data in '%s'", oname); error(USER, errmsg); } break; } copycolor(mp->cbin[j], rgbv); } } lu_doall(&ofiletab, &myclose, NULL); /* close all files */ }