void ajRegTrace(const AjPRegexp rexp) { ajint isub; ajint ilen; ajint ipos; ajint istart; ajint iend; static AjPStr str = NULL; ajDebug(" REGEXP trace\n"); if (!rexp->orig) ajDebug("original string not saved - unable to trace string values\n"); for(isub=0; isub < rexp->ovecsize; isub++) { istart = 2*isub; iend = istart+1; if (!rexp->orig) { if(!isub) ajDebug("original string from %d .. %d\n", rexp->ovector[istart], rexp->ovector[iend]); else ajDebug("substring %2d from %d .. %d\n", isub, rexp->ovector[istart], rexp->ovector[iend]); continue; } if(rexp->ovector[iend] >= rexp->ovector[istart]) { ilen = rexp->ovector[iend] - rexp->ovector[istart]; ajStrSetRes(&str, ilen+1); memmove(str->Ptr, &rexp->orig[rexp->ovector[istart]], ilen); str->Len = ilen; str->Ptr[ilen] = '\0'; if(!isub) { ajDebug(" original string '%s'\n", rexp->orig); ajDebug(" string match '%S'\n", str); } else { ipos = rexp->ovector[istart]; ajDebug(" substring %2d '%S' at %d\n", isub, str, ipos); } } } ajDebug("\n"); ajStrDel(&str); return; }
AjBool ajRegPre(const AjPRegexp rp, AjPStr* dest) { ajint ilen; ilen = rp->ovector[0]; ajStrSetRes(dest, ilen+1); if(ilen) { memmove((*dest)->Ptr, rp->orig, ilen); (*dest)->Len = ilen; (*dest)->Ptr[ilen] = '\0'; return ajTrue; } ajStrDelStatic(dest); return ajFalse; }
AjBool ajRegSubI(const AjPRegexp rp, ajint isub, AjPStr* dest) { ajint ilen; ajint istart; ajint iend; istart = 2*isub; iend = istart+1; if(isub < 0) { ajStrDelStatic(dest); return ajFalse; } if(isub >= rp->ovecsize) { ajStrDelStatic(dest); return ajFalse; } if(rp->ovector[istart] < 0) { ajStrDelStatic(dest); return ajFalse; } ilen = rp->ovector[iend] - rp->ovector[istart]; ajStrSetRes(dest, ilen+1); if(ilen) memmove((*dest)->Ptr, &rp->orig[rp->ovector[istart]], ilen); (*dest)->Len = ilen; (*dest)->Ptr[ilen] = '\0'; return ajTrue; }
AjBool ajReadlinePos(AjPFile file, AjPStr* Pdest, ajlong* Ppos) { const char *cp; char *buff; ajint isize; ajint ilen; ajint jlen; ajint ipos; ajuint buffsize; size_t iread; const char* pnewline = NULL; MAJSTRDEL(Pdest); if(file->Buffsize) buffsize = file->Buffsize; else buffsize = ajFileValueBuffsize(); if(!file->Buff) ajStrAssignResC(&file->Buff, buffsize, ""); else if(buffsize > MAJSTRGETRES(file->Buff)) ajStrSetRes(&file->Buff, buffsize); if(MAJSTRGETUSE(file->Buff) == 1) buff = MAJSTRGETPTR(file->Buff); else buff = ajStrGetuniquePtr(&file->Buff); isize = MAJSTRGETRES(file->Buff); ilen = 0; ipos = 0; if(!file->fp) ajWarn("ajFileGets file not found"); *Ppos = file->Filepos; while(buff) { if(file->End) { ajStrAssignClear(Pdest); ajDebug("at EOF: File already read to end %F\n", file); return ajFalse; } #ifndef __ppc__ if(file->Readblock) { if(file->Blockpos >= file->Blocklen) { iread = fread(file->Readblock, 1, file->Blocksize, file->fp); if(!iread && ferror(file->fp)) ajFatal("fread failed with error:%d '%s'", ferror(file->fp), strerror(ferror(file->fp))); file->Blockpos = 0; file->Blocklen = iread; file->Readblock[iread] = '\0'; /*ajDebug("++ fread %u Ppos:%Ld\n", iread, *Ppos);*/ } if(file->Blockpos < file->Blocklen) { /* we know we have something in Readblock to process */ pnewline = strchr(&file->Readblock[file->Blockpos], '\n'); if(pnewline) jlen = pnewline - &file->Readblock[file->Blockpos] + 1; else jlen = file->Blocklen - file->Blockpos; /*ajDebug("ipos:%d jlen:%d pnewline:%p " "Readblock:%p blockpos:%d blocklen:%d\n", ipos, jlen, pnewline, file->Readblock, file->Blockpos, file->Blocklen);*/ memmove(&buff[ipos], &file->Readblock[file->Blockpos], jlen); buff[ipos+jlen]='\0'; cp = &buff[ipos]; file->Blockpos += jlen; } else { jlen = 0; cp = NULL; } } else { cp = fgets(&buff[ipos], isize, file->fp); jlen = strlen(&buff[ipos]); } #else cp = ajSysFuncFgets(&buff[ipos], isize, file->fp); jlen = strlen(&buff[ipos]); #endif if(!cp && !ipos) { if(feof(file->fp)) { file->End = ajTrue; ajStrAssignClear(Pdest); ajDebug("EOF ajFileGetsL file %F\n", file); return ajFalse; } else ajFatal("Error reading from file '%S'\n", ajFileGetNameS(file)); } ilen += jlen; file->Filepos += jlen; /* ** We need to read again if: ** We have read the entire buffer ** and we don't have a newline at the end ** (must be careful about that - we may just have read enough) */ if(((file->Readblock && !pnewline) ||(jlen == (isize-1))) && (buff[ilen-1] != '\n')) { MAJSTRSETVALIDLEN(&file->Buff, ilen); /* fix before resizing! */ ajStrSetResRound(&file->Buff, ilen+buffsize+1); /*ajDebug("more to do: jlen: %d ipos: %d isize: %d ilen: %d " "Size: %d\n", jlen, ipos, isize, ilen, ajStrGetRes(file->Buff));*/ ipos += jlen; buff = ajStrGetuniquePtr(&file->Buff); isize = ajStrGetRes(file->Buff) - ipos; /*ajDebug("expand to: ipos: %d isize: %d Size: %d\n", ipos, isize, ajStrGetRes(file>Buff));*/ } else buff = NULL; } MAJSTRSETVALIDLEN(&file->Buff, ilen); if (ajStrGetCharLast(file->Buff) != '\n') { /*ajDebug("Appending missing newline to '%S'\n", file->Buff);*/ ajStrAppendK(&file->Buff, '\n'); } ajStrAssignRef(Pdest, file->Buff); /* if(file->Readblock) ajDebug("ajFileGetsL done blocklen:%d blockpos:%d readlen:%u\n", file->Blocklen, file->Blockpos, ajStrGetLen(file->Buff)); */ return ajTrue; }
ajint ajUserGet(AjPStr* pthis, const char* fmt, ...) { AjPStr thys; const char *cp; char *buff; va_list args; ajint ipos; ajint isize; ajint ilen; ajint jlen; ajint fileBuffSize = ajFileValueBuffsize(); va_start(args, fmt); ajFmtVError(fmt, args); va_end(args); if(ajFileValueRedirectStdin()) { ajUser("(Standard input in use: using default)"); ajStrAssignC(pthis, ""); return ajStrGetLen(*pthis); } ajStrSetRes(pthis, fileBuffSize); buff = ajStrGetuniquePtr(pthis); thys = *pthis; isize = ajStrGetRes(thys); ilen = 0; ipos = 0; /*ajDebug("ajUserGet buffer len: %d res: %d ptr: %x\n", ajStrGetLen(thys), ajStrGetRes(thys), thys->Ptr);*/ while(buff) { #ifndef __ppc__ cp = fgets(&buff[ipos], isize, stdin); #else cp = ajSysFuncFgets(&buff[ipos], isize, stdin); #endif if(!cp && !ipos) { if(feof(stdin)) { ajErr("Unable to get reply from user - end of standard input"); ajExitBad(); } else ajFatal("Error reading from user: '******'\n", strerror(errno)); } jlen = strlen(&buff[ipos]); ilen += jlen; /* ** We need to read again if: ** We have read the entire buffer ** and we don't have a newline at the end ** (must be careful about that - we may just have read enough) */ ajStrSetValidLen(pthis, ilen); thys = *pthis; if((jlen == (isize-1)) && (ajStrGetCharLast(thys) != '\n')) { ajStrSetRes(pthis, ajStrGetRes(thys)+fileBuffSize); thys = *pthis; /*ajDebug("more to do: jlen: %d ipos: %d isize: %d ilen: %d " "Size: %d\n", jlen, ipos, isize, ilen, ajStrGetRes(thys));*/ ipos += jlen; buff = ajStrGetuniquePtr(pthis); isize = ajStrGetRes(thys) - ipos; /* ajDebug("expand to: ipos: %d isize: %d Size: %d\n", ipos, isize, ajStrGetRes(thys)); */ } else buff = NULL; } ajStrSetValidLen(pthis, ilen); if(ajStrGetCharLast(*pthis) == '\n') ajStrCutEnd(pthis, 1); /* PC files have \r\n Macintosh files have just \r : this fixes both */ if(ajStrGetCharLast(*pthis) == '\r') { /*ajDebug("Remove carriage-return characters from PC-style files\n");*/ ajStrCutEnd(pthis, 1); } ajStrTrimWhite(pthis); return ajStrGetLen(*pthis); }
static EmbPEntry dbiblast_nextblastentry(PBlastDb db, ajint ifile, const AjPStr idformat, AjBool systemsort, AjPStr const * fields, ajint * maxFieldLen, ajuint* maxidlen, ajuint* countfield, AjPFile elistfile, AjPFile * alistfile) { ajint i; static ajint lastfile = -1; static ajint iparser = -1; static ajint called = 0; static ajuint tabhdr[TABLESIZE]; static ajint iload = TABLESIZE-1; static ajint irest = 0; static ajint ipos = 0; static ajint jpos = 0; ajint ir; ajint j; static ajint is = 0; char* token; static ajint nfields; ajint ifield; if(!called) { for(i=0; parser[i].Name; i++) if(ajStrMatchC(idformat, parser[i].Name)) { iparser = i; break; } if(iparser < 0) ajFatal("idformat '%S' unknown", idformat); ajDebug("idformat '%S' Parser %d\n", idformat, iparser); ajStrSetRes(&id, HDRSIZE); ajStrSetRes(&acc, HDRSIZE); ajStrSetRes(&hline, HDRSIZE); called = 1; } if(!fdl) { nfields=0; while(fields[nfields]) nfields++; if(nfields) AJCNEW(fdl, nfields); for(i=0; i < nfields; i++) fdl[i] = ajListNew(); } if(lastfile != ifile) { lastfile = ifile; ipos = 1; /* isize = 0;*/ irest = 0; iload = TABLESIZE-1; } if(!dbiblastEntry || !systemsort) dbiblastEntry = embDbiEntryNew(nfields); /* pick up the next entry, parse it and dump it */ if(ipos > db->Size) return NULL; if( ipos >= irest) { ajDebug("ipos: %d iload: %d irest: %d\n", ipos, iload, irest); irest = ipos + TABLESIZE - 2; if(irest > db->Size) { iload = db->Size - ipos + 1; irest = db->Size; } jpos=0; j = dbiblast_loadtable(tabhdr, iload, db, db->TopHdr, ipos-1); if(!j) ajDebug("No elements read"); } j = dbiblast_ncblreadhdr(&hline, db, tabhdr[jpos], tabhdr[jpos+1]); if(!parser[iparser].Parser(hline, alistfile, systemsort, fields, maxFieldLen, countfield, &id, fdl)) ajFatal("failed to parse '%S'", hline); ir = ipos; if(ajStrGetLen(id) > *maxidlen) *maxidlen = ajStrGetLen(id); if(systemsort) ajFmtPrintF(elistfile, "%S %d %d %d\n", id, ir, is, ifile+1); else { dbiblastEntry->entry = ajCharNewS(id); dbiblastEntry->rpos = ir; dbiblastEntry->spos = is; dbiblastEntry->filenum = ifile+1; /* field tokens as list, then move to dbiblastEntry->field */ for(ifield=0; ifield < nfields; ifield++) { dbiblastEntry->nfield[ifield] = ajListGetLength(fdl[ifield]); if(dbiblastEntry->nfield[ifield]) { AJCNEW(dbiblastEntry->field[ifield], dbiblastEntry->nfield[ifield]); i = 0; while(ajListPop(fdl[ifield], (void**) &token)) dbiblastEntry->field[ifield][i++] = token; } else dbiblastEntry->field[ifield] = NULL; } } ipos++; jpos++; return dbiblastEntry; }