int findsub(char* substr, char* eldstr) { char* peldstr = malloc(5*sizeof(char)); char* tempstr = malloc(5*sizeof(char)); peldstr = eldstr; int lensub = strlength(substr); int leneld = strlength(eldstr); while (peldstr - eldstr + lensub <= leneld) { linecpy(peldstr++, tempstr); if (!linecmp(substr, _linecut(tempstr, lensub))) { return peldstr - eldstr; } } return 0; }
static void join(FILE *fa, FILE *fb, size_t jfa, size_t jfb) { struct span spa, spb; int cmp, eofa, eofb; initspan(&spa); initspan(&spb); cmp = eofa = eofb = 0; addtospan(&spa, fa, RESET); addtospan(&spb, fb, RESET); while (spa.nl && spb.nl) { if ((cmp = linecmp(spa.lines[0], spb.lines[0], jfa, jfb)) < 0) { if (unpairsa) prline(spa.lines[0]); if (!addtospan(&spa, fa, RESET)) { if (unpairsb) { /* a is EOF'd; print the rest of b */ do prline(spb.lines[0]); while (addtospan(&spb, fb, RESET)); } eofa = eofb = 1; } else { continue; } } else if (cmp > 0) { if (unpairsb) prline(spb.lines[0]); if (!addtospan(&spb, fb, RESET)) { if (unpairsa) { /* b is EOF'd; print the rest of a */ do prline(spa.lines[0]); while (addtospan(&spa, fa, RESET)); } eofa = eofb = 1; } else { continue; } } else if (cmp == 0) { /* read all consecutive matching lines from a */ do { if (!addtospan(&spa, fa, EXPAND)) { eofa = 1; spa.nl++; break; } } while (linecmp(spa.lines[spa.nl-1], spb.lines[0], jfa, jfb) == 0); /* read all consecutive matching lines from b */ do { if (!addtospan(&spb, fb, EXPAND)) { eofb = 1; spb.nl++; break; } } while (linecmp(spa.lines[0], spb.lines[spb.nl-1], jfa, jfb) == 0); if (pairs) prspanjoin(&spa, &spb, jfa, jfb); } else { /* FIELD_ERROR: both lines lacked join fields */ if (unpairsa) prline(spa.lines[0]); if (unpairsb) prline(spb.lines[0]); eofa = addtospan(&spa, fa, RESET) ? 0 : 1; eofb = addtospan(&spb, fb, RESET) ? 0 : 1; if (!eofa && !eofb) continue; } if (eofa) { spa.nl = 0; } else { swaplines(spa.lines[0], spa.lines[spa.nl - 1]); /* ugly */ spa.nl = 1; } if (eofb) { spb.nl = 0; } else { swaplines(spb.lines[0], spb.lines[spb.nl - 1]); /* ugly */ spb.nl = 1; } } freespan(&spa); freespan(&spb); }