vparse.c 4.53 KB
#include <stdio.h>
#include <string.h>
#include <strings.h>

char *InFileName=NULL, *OutputBaseName=NULL;
char OutputFileName[80];
FILE *InFile, *OutputFile;
char InData[80] = "foo";
int StartingFile = 0;
int FieldsPerFile = 1;
int CurrentLine = 0;
char *ReadOk = "foo";
unsigned int vbus_data, vbus_sync;

void usage(void)
{
    fprintf(stderr, "usage: vparse -i foo.tab -o bar.tab [-s n] [-f n] \n");
    fprintf(stderr, "       outputs will be f fields per file          \n");
    fprintf(stderr, "       in files named: bar.tab{s,s+1,s+2,...}     \n");
    exit(1);
}

void GetData(void)
{

    ReadOk = fgets(InData, 80, InFile);
    CurrentLine++;

    while (InData[0] == '#')
	{
	    ReadOk = fgets(InData, 80, InFile);
	    CurrentLine++;
	}
    
    sscanf(InData, "%x %d", &vbus_data, &vbus_sync);    
}

void PrintHeaderInfo(void)
{
    fprintf(OutputFile, "# \n");
    fprintf(OutputFile, "#  Created by vparse.c from input %s \n", OutputFileName);
    fprintf(OutputFile, "# \n");
    fprintf(OutputFile, "vclk           @C 1(8) 0(8) \n");
    fprintf(OutputFile, "vbus_data[6:0] @I @E 2      \n");
    fprintf(OutputFile, "vbus_sync      @I @E 2      \n");
    fprintf(OutputFile, "\n");
    fprintf(OutputFile, "0x00 0 \n");
    fprintf(OutputFile, "0x0f 0 \n");
    fprintf(OutputFile, "0x0f 0 \n");
}

void ReadAndProcessInFile(void)
{    
    int CurrentFile = StartingFile;
    int ValidData = 0;
    int FieldBegin = 0;
    int VSyncFinished;
    int FieldNumber;

    if ((InFile = fopen(InFileName, "r")) == NULL)
	{
	    fprintf(stderr, "Error, could not open input file %s \n", InFileName);
	    exit(1);
	}

    while (!ValidData)
	{
	    GetData();
	    if (vbus_data == 0x0f)
		{
		    unsigned int Next_data[3];
		    unsigned int Next_sync[3];
		    int i;

		    for (i=0; i<3; i++)
			{
			    GetData();
			    Next_data[i] = vbus_data;
			    Next_sync[i] = vbus_sync;
			}

		    if ((Next_data[0] == 0x00) && (Next_sync[0] == 1) &&
			(Next_data[1] == 0x00) && (Next_sync[1] == 1) &&
			(Next_data[2] == 0x00) && (Next_sync[2] == 1))
			{
			    ValidData = 1;
			    /* fprintf(stderr, "Finished valid data sequence on line %d \n", CurrentLine); */
			}
		}
	}
    ValidData = 0;

    while (!feof(InFile))
	{
	    /* Loop until get vertical sync */
	    while (!FieldBegin && !feof(InFile))
		{
		    GetData();
		    if ((!vbus_sync) && !(vbus_data & (1 << 3)))
			FieldBegin = 1;	    
		}

	    if (!feof(InFile)) 
		{
		    /* fprintf(stderr, "Got vertical sync on line %d \n", CurrentLine);   */
		    sprintf(OutputFileName, "%s%.3d", OutputBaseName, CurrentFile++);
		    fprintf(stderr, "Opening output file %s \n", OutputFileName);
		    if ((OutputFile = fopen(OutputFileName, "w")) == NULL)
			{
			    fprintf(stderr, "Error, could not open outputfile file %s \n", 
				    OutputFileName);
			    exit(1);
			}       
 		    PrintHeaderInfo();
		}

	    for (FieldNumber=0; FieldNumber < FieldsPerFile; FieldNumber++)
		{
		    /* Loop until out of vertical sync */
		    
		    FieldBegin    = 0;
		    VSyncFinished = 0;	    
		    while (!VSyncFinished && !feof(InFile))
			{		    
			    /* printf("0x%.2x %d\n", vbus_data, vbus_sync); */
			    fprintf(OutputFile, "0x%.2x %d\n", vbus_data, vbus_sync);
			    GetData();
			    if ((!vbus_sync) && (vbus_data & (1 << 3)))
				VSyncFinished = 1;
			}

		    /* if (!feof(InFile)) 
			fprintf(stderr, "Finished vertical sync on line %d \n", CurrentLine); */

		    /* Now loop until get next vsync */
		    while (!FieldBegin && !feof(InFile))
			{
			    GetData();
			    if ((!vbus_sync) && !(vbus_data & (1 << 3)))
				FieldBegin = 1;	    
			    else
				fprintf(OutputFile, "0x%.2x %d\n", vbus_data, vbus_sync);
			}

		    if (!feof(InFile)) 
			{
			    /* fprintf(stderr, "Finished field data on line %d \n", CurrentLine); */
			}
		}

	    if (!feof(InFile)) {
		fclose(OutputFile);
		printf("%s ", OutputFileName);
	    }

	}
    
    /* fprintf(stderr, "Finished file on line %d \n", CurrentLine); */

    fclose(InFile);

    printf("\n");
}

int main(int argc, char **argv)
{
    int c;
    extern char *optarg;
    extern int optind;

    while ((c = getopt(argc, argv, "i:o:s:f:")) != EOF)
	switch (c)
	    {
	    case 'i':
		InFileName = strdup(optarg);
		break;
	    case 'o':
		OutputBaseName = strdup(optarg);
		break;
	    case 's':
		StartingFile = atoi(optarg);
		break;
	    case 'f':
		FieldsPerFile = atoi(optarg);
		break;
	    case '?':
		usage();
		break;		
	    }    

    if (InFileName == NULL)
	usage();

    if (OutputBaseName == NULL)
	usage();
	    
    ReadAndProcessInFile();

    return(0);       
}