예제 #1
0
int		main(int argc, char* argv[])
{
	char buf[MAX_BUF_SIZE];
	const char* name = NULL;
	const char* opts = NULL;
	bool bCount = false;

	switch( argc )
	{
	case 2:
		if( str_match(argv[1], "-v") )
		{
			printf("Version 1.%d by Roman Dremov 2015\n", GLPVERSION);
			return 0;
		}
		name = argv[1];
		break;
	case 3:
		if( *argv[1] == '-' )
		{
			if( str_match(argv[1] + 1, "c") )
				bCount = true;
			else
				opts = argv[1] + 1;
			name = argv[2];
		}
		break;
	}

	bool bValid = true;

	if( !name )
		bValid = false;

	Options options;

	if( bValid && opts )
	{
		for(int ii=strlen(opts)-1; ii>=0; ii--)
		{
			if( !options.Parse(opts[ii], NULL) )
			{
				bValid = false;
				break;
			}
		}
	}

	if( !bValid )
	{
		char cc;
		char* str = buf;
		char space[] = " ";
		for(cc='a'; cc<='z'; cc++)
		{
			if( options.Parse(cc, space) )
				*str++ = cc;
		}
		*str = 0;
		printf("USE:\n"
			"\tglp -v\t\t\t\tto get version info\n"
			"\tglp -c gitolite.log\t\tto count entries\n"
			"\tglp gitolite.log\t\tto output all entries\n"
			"\tglp [-%s] gitolite.log\tto output entries hierarchically\n", buf);
		printf("OPTIONS (order defines report sorting hierarchy):\n");
		str = buf;
		*str = 0;
		for(cc='a'; cc<='z'; cc++)
		{
			if( options.Parse(cc, space) )
			{
				sprintf(str, "\t%c - ", cc);
				str += strlen(str);
				str += options.Parse(cc, str);
				strcat(str, "\n");
				str += strlen(str);
			}
		}
		*str = 0;
		printf("%s", buf);
		return ERROR_ARG;
	}

	FILE* pf = fopen(name, "r");

	if( !pf )
	{
		printf("File does not exist: %s\n", name);
		return ERROR_FILE;
	}

	Data data;

	do
	{
		char* str = fgets(buf, sizeof(buf), pf);
		if( str )
		{
			Packet packet;
			if( packet.Parse(str) )
				data.Add(packet);
		}
	} while( !feof(pf) );

	fclose(pf);
	data.Out(bCount);
	return ERROR_OK;
}