/* Copyright 2002 Daniel Egnor.  See LICENSE file.
 *
 * This program is part of the processing chain to convert TIGER/Line data
 * (and FIPS-55 data, in this case) to an index ("map") for rapid address
 * lookup.  It reads a FIPS-55 text file describing locations, and writes
 * "step2" data as consumed by "geo-2-to-2a" and "geo-2-to-3".  */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

/* We're really just reformatting the input to associate city and state
 * names with ZIP code ranges, which will get eventually turned into zone
 * range offsets in the index. */
int main(int argc,const char *argv[]) {
	char line[256];

	if (argc != 1) {
		fprintf(stderr,"usage: %s < fips-data > step2-data\n",argv[0]);
		return 2;
	}

	while (NULL != fgets(line,sizeof line,stdin)) {
		int first,last;
		char *end;

		if (strlen(line) < 111) continue;
		if (!memcmp(line + 102,"     ",5)) continue;

		first = strtoul(line + 102,&end,10);
		if (line + 107 != end) {
			fputs("warning: invalid input line\n",stderr);
			continue;
		}

		if (!memcmp(line + 109,"  ",2))
			last = first;
		else {
			line[105] = line[109];
			line[106] = line[110];
			last = strtoul(line + 102,&end,10);
			if (line + 107 != end) {
				fputs("warning: invalid input line\n",stderr);
				continue;
			}
		}

		printf("NC%.52s%05d          %05d          \n",
		       line + 15,first,last + 1);
		printf("NS%.2s                                                  %05d          %05d          \n",
		       line + 7,first,last + 1);
	}

	return 0;
}

