Beispiel #1
0
/*                                                                                                   
 * We've kept track of how many lines were input and have those lines stored in                      
 * a string array so to swap lines we just need to traverse the string array                         
 * and call the swaplines method above                                                               
 */
void swaplineorder(char** l, int numLines){
  char** r = l + numLines - 1;
  while(l < r){
    swaplines(l++,r--);
  }
}
Beispiel #2
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);
}