#include "decode.h"

main(int argc, char **argv)
{
	char			sample[2048];
	struct chunks 	*c;
	int				bs;
	char			*s;

	if (argc > 1)
	{
		c=convert(argv[1]);
		if (c != NULL)
		{
			if (c->data != NULL)
			{
				decode_type(c);
				if (c->formatted != NULL)
				{
					printf("%s\t%s\t%s\t%s\t%s\n", c->head, c->serial, c->ctype, c->data,c->formatted);
				}
				else
				{
					printf("%s\t%s\t%s\t%s\t\n", c->head, c->serial, c->ctype, c->data);
				};
			};
			free(c->serial);
			free(c->data);
			free(c);
		};
	}
	else
	{
		while(1)
		{
			bs=read(0, sample, 2047);
			sample[bs]=0;
			c=convert(sample);
			if (c != NULL)
			{
				if (c->data != NULL)
				{
					decode_type(c);
					if (c->formatted != NULL)
					{
						printf("%s\t%s\t%s\t%s\t%s\n", c->head, c->serial, c->ctype, c->data,c->formatted);
					}
					else
					{
						printf("%s\t%s\t%s\t%s\t\n", c->head, c->serial, c->ctype, c->data);
					};
				};
				free(c->serial);
				free(c->data);
				if (c->formatted != NULL)
				{
					free(c->formatted);
				};
				free(c);
			};
		};
	};	
};
