/*
Copyright (c) 2000                      RIPE NCC


All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the author not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-------------------------------------------------------------------------------
Module Header
Filename          : checkrvec.c
Purpose           : Read RVEC files and verbosely report inconsitencies
Author            : Manuel Valente <manuel@ripe.net>
Date              : 20000911
Description       : 
Language Version  : gcc version 2.95.2
OSs Tested        : Solaris 2.6
Command Line      : checkrvec.c path
Input Files       : /ncc/ttpro/raw_data/
Output Files      :
Comments          : 

THIS PROGRAM NEEDS UPDATING, USE A NEW READ_LINE IN VOID MODE, NO AS QUERIES ETC.

$Id: checkrvec.c,v 1.2 2002/10/24 20:31:31 wilhelm Exp $
-------------------------------------------------------------------------------
*/

#include "common.h"
#include "read_line.h"
#include "digest.h"

/*
-------------------------------------------------------------------------------
Purpose           : The important function: process data from an RVEC file,
Params            : filename
Returns           : Number of lines processed & verbose output on errors
Comments          :
*/
void process_file (char* filename) 
{
	gzFile fdz;
	FILE *fd;

	unsigned int src, dst, tt, npoints, gz, t;

	char buf[4096];
	char bufcopy[4096];
	char vector[512];     /* 16 bytes per IP -> maximum 32 hops */


	char *p, *link;

	/* Allocate memory to store fullrec, empty it */

	gz = 0;


	fd = fdz = NULL;

	/* Check if filename starts with RVEC */
	link = strrchr(filename,'/');
	if (link == NULL)  return;
	if (strncmp(link,"/RVEC",5) != 0) return;
	
	
	/* Check if filename ends with gz */
	p = strrchr(filename, '.');
	if (p && strcmp(p+1,"gz") == 0) gz = 1;
	    
	if(gz)
	{
		/* use gzopen */
		if((fdz = gzopen(filename, "rb")) == NULL)
		{
			fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
			return;
		};
	}
	else
	{
		/* use regular open */
		if((fd = fopen(filename,"rb")) == NULL)
		{
			fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
			return;
		};
	};

	
	/* read file */
	while((gz && !gzeof(fdz)) || (!gz && !feof(fd)))
	{
		if(gz)
		{
			if(gzgets(fdz, buf, sizeof(buf)) == Z_NULL) continue;
		}
		else
		{
			if(fgets(buf, sizeof(buf), fd) == NULL) continue;
		}
	
		/* copy line buffer, since read_line will modify it */
		memccpy(bufcopy, buf, '\0', sizeof(buf));
		
		/* Parse the line */
		t = read_line(buf,&src,&dst,&tt,&npoints,vector);

		/* Check if function returned 0 */
		if (t != 0) 
		{
			fprintf(stderr, "Error decoding line:\n%s\n",bufcopy);
			continue; 
		};
	}


	/* Close file, report errors */
	if (gz) gzclose(fdz);
	else fclose (fd);

	return;
};

/*
-------------------------------------------------------------------------------
Purpose           : Main function - Initialize and process files
Params            : filename containing the RVEC files
Returns           : 0
Comments          :
*/
int main (int argc, char *argv[])
{
	char *path;

	/* Check arguments */
	if (argc < 2) 
	{ 
		fprintf (stderr,"Usage: %s <filename>\n",argv[0]); 
		exit (1); 
	};

	/* Initialization: open connection, read routes and ranges */

	path = argv[1];

        process_file (path);

	exit (0);
};



