/* -*- linux-c -*-
 * Translator of connection tables into more dense format, copyright 2000 Pavel Machek, GPLv2 or later
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

int pstate = 0, curtime, curkm, lasttime, lastkm;

typedef unsigned char uchar;

#define isdigit(c) 0

static inline int
pozchar(uchar c)
{
	return ((c == 0240) || (c=='T') || (c=='!') || (c=='x') || (c=='<') || (c=='>') || (isdigit(c)) | (c == ','));
}

struct town *
decodeline(struct id *id, uchar *buf, struct town *prev)
{
	int i;
	char townname[10240], remark[10240];
	int from1 = -1, from2 = -1, to1 = -1, to2 = -1, km = -1;

	for (i=0; i<strlen(buf); i++) {
		townname[i] = buf[i];
		if ((buf[i]==' ') && ((buf[i+1]==' '))) {
			townname[i]=0;
			break;
		}
		if (!buf[i]) {
			fprintf( stderr, "Could not find townname on %s\n", buf );
			exit(1);
		}
	}

	while (buf[i]==' ')
		i++;

	for (; i<strlen(buf); i++) {
		remark[i] = buf[i];
		if ((buf[i]==' ') && ((buf[i+1]==' '))) {
			remark[i]=0;
			break;
		}
		if (!buf[i]) {
			fprintf( stderr, "Could not find remark on %s\n", buf );
			exit(1);
		}
	}

	while (buf[i]==' ')
		i++;

	for (; i<strlen(buf); i++) {
		if (buf[i]==0240)
			break;
		if (buf[i]!=' ') {
			if (sscanf( buf+i, "%d:%d", &from1, &from2 ) != 2) {
				fprintf( stderr, "Could not find from time on %s\n", buf );
				exit(1);
			}
			i+=5;
			break;
		}
	}
	i++;

	for (; i<strlen(buf); i++) {
		if (buf[i]==0240)
			break;
		if (buf[i]!=' ') {
			if (sscanf( buf+i, "%d:%d", &to1, &to2 ) != 2) {
				to1 = to2 = -1;
				i -= 1;
				pstate = 0;		/* This must be last */
				break;
			}
			i+=5;
			break;
		}
	}
	i++;

	while (buf[i]==' ') i++;
	if (sscanf(buf+i, "%d", &km ) != 1) {
		fprintf( stderr, "Kilometers not right on %s *%s\n", buf, buf+i );
	}

	if (1) {
		if (from1 != -1) {
			if ((lasttime != -1) && ((from1*60+from2 - lasttime)>0))
				printf( "+%d", from1*60+from2 - lasttime );
			else
				printf( "%d:%02d", from1, from2 );
			lasttime = from1*60+from2;
		}
		printf( "\t" );
		if (to1 != -1) {
			if ((lasttime != -1) && ((to1*60+to2 - lasttime)>0))
				printf( "+%d", to1*60+to2 - lasttime );
			else
				printf( "%d:%02d", to1, to2 );
			lasttime = to1*60+to2;
		}
		printf( "\t%s\t%d\t%s\n", remark, km-lastkm, townname );
		lastkm = km;
	}
}

void
readit(void)
{
	uchar buf[10240];
	struct id *id = NULL;
	FILE *f = stdin;
	int lineno = 0;
	struct town *prev = NULL;

	while (!feof(f)) {
		lineno++;
		if (!(lineno % 10000))
			fprintf( stderr, "Reading: %dK lines\r", lineno/1000 );
		fgets(buf, 10230, f);
		buf[strlen(buf)-1]=0;
		if (*buf == '#') {
			if (1)
				puts(buf);
			lasttime = -1;
			lastkm = 0;
			pstate = 1;
			prev = NULL;
			continue;
		}
		if (*buf == '[') {
			if (1)
				puts(buf);
			pstate = 0;
			continue;
		}
		if (pstate == 1)
			prev = decodeline(id, buf, prev);
		else {
/*			fprintf( stderr, "Line in unexpected state: %s\n", buf ); */
		}
	}
}

int
main(int argc, char *argv[])
{
	readit();
}

