示例#1
0
void parsecommandline( int argc, char *argv[] )
{
	/* valid option characters; last char MUST be 0 ! */
	char optionchar[] =   { 'h', 'g', 'x', 'v', 'n', 'a', 'w', 'd', 'A',
							'T', 'f', 'W', 'c',  'r', 'p', 0 };
	int musthaveparam[] = {  0 ,  1,   1,   1,   2,   1,   0,   2,   1,
							 0,   1,   0,   0,   1, 0, 0 };
	int nopts;
	int mhp[256];
	int isopt[256];
	int optchar;


	nopts = strlen(optionchar);
	if ( nopts > 50 )
	{
		fprintf(stderr, "\n\nparsecommandline: the option-chars string "
				"seems to be\nVERY long (%d bytes) !\n\n", nopts );
		exit(-1);
	}

	fill_n( isopt, 256, 0 );
	fill_n( mhp, 256, 0 );
	for ( int i = 0; i < nopts; i ++ )
	{
		if ( isopt[static_cast<int>(optionchar[i])] )
		{
			fprintf(stderr, "\n\nparsecommandline: Bug: option character '%c'"
					" is specified twice in the\n"
				   "option character array !\n\n", optionchar[i] );
			exit(-1);
		}
		isopt[ static_cast<int>(optionchar[i]) ] = 1;
		mhp[ static_cast<int>(optionchar[i])] = musthaveparam[i];
	}

	++argv; --argc;
	while ( argc > 0 )
	{
		if ( argv[0][0] == '-' )
		{
			optchar = argv[0][1];

			if ( ! isopt[optchar] )
			{
				fprintf(stderr, "\nIs not a valid command line option\n");
				commandlineerror( argv[0], NULL );
			}
			for ( int i = 0; i < mhp[optchar]; i ++ )
				if ( ! argv[1+i] || argv[1+i][0] == '-' )
				{
					fprintf(stderr, "\nCommand line option -%c must "
							"have %d parameter(s)\n",
							argv[0][1], mhp[optchar] );
					commandlineerror( argv[0], NULL );
					argv += 1 + i;
					argc -= 1 + i;
					continue;
				}

			switch ( optchar )
			{
				case 'h': commandlineerror( NULL, NULL);  break;

				case 'w': With_window = true; break;
				case 'T': No_timing = true; break;
				case 'W': White_background = true; break;
				case 'c': All_collisions = true; break;

				case 'x': Complexity = atoi( argv[1] ); break;
				case 'a': Rot_vel = atof( argv[1] ); break;

				case 'n': Ndistances = atoi( argv[1] );
						  Nrotations = atoi( argv[2] );
						  break;

				case 'd': Dist_1 = atof( argv[1] );
						  Dist_2 = atof( argv[2] );
						  break;


				case 'v': for ( unsigned int i = 0; i < strlen(argv[1]); i ++ )
							  switch ( argv[1][i] )
							  {
								  case 'v': col_verbose = true;
											break;
								  case 'b': verbose |= VERBOSE_PRINT;
											break;
								  case 'h': col_verboseShowHulls = true;
											break;
								  default : commandlineerror(argv[0],argv[1]);
							  }
						  break;

				case 'g': if ( ! strcmp(argv[1],"pl") )
							  geom_type = OBJ_PLANES;
						  else
						  if ( ! strcmp(argv[1],"sh") )
							  geom_type = OBJ_SPHERES;
						  else
						  if ( ! strcmp(argv[1],"to") )
							  geom_type = OBJ_TORUS;
						  else
						  if ( ! strcmp(argv[1],"file") )
							  geom_type = OBJ_FILE;			// just a dummy
						  else
						  {
							  fputs("unrecognized obj type\n",stderr);
							  commandlineerror(argv[0],argv[1]);
						  }
						  break;

				case 'A': if ( ! strcmp(argv[1],"do") )
							  Algorithm = col::ALGO_DOPTREE;
						  else
						  if ( ! strcmp(argv[1],"bx") )
							  Algorithm = col::ALGO_BOXTREE;

						   else
						   {
							  fputs("unrecognized algorithm\n",stderr);
							  commandlineerror(argv[0],argv[1]);
						   }
						   break;

				case 'f': loadGeom( argv[1], &fixed_node );
						  moving_node = fixed_node->clone();
						  fixed_geom = col::getGeom( fixed_node );
						  moving_geom = col::getGeom( moving_node );
						  geom_type = OBJ_FILE;
						  // post: moving_geom == fixed_geom
						  break;
				case 'r':
						configFile = argv[1];
						break;
				case 'p':
						print_pairs = true;
						break;
				default: fprintf(stderr, "\nBug in parsecommandline !\n");
						 commandlineerror( argv[0], NULL );
			}

			argv += 1 + mhp[optchar];
			argc -= 1 + mhp[optchar];
		}
		else
		{
			/* command line arg doesn't start with '-' */
			fprintf(stderr, "\nThis is not a valid command line option\n");
			commandlineerror( argv[0], NULL );
			/* or, load file instead .. */
		}
	}

	// check sanity of options
	if ( Complexity < 1 ||
		 (geom_type != OBJ_PLANES && Complexity < 3) ||
		 Complexity > 1000 )
	{
		fprintf(stderr,"complexity (%u) out of range!\n", Complexity );
		exit(-1);
	}

	if ( Ndistances > MaxNumDistances )
	{
		fprintf(stderr,"too many (%u) distances!\n", Ndistances );
		Ndistances = MaxNumDistances;
	}
	if ( Ndistances < 1 )
		Ndistances = 1;

	if ( Rot_vel < col::NearZero )
		Rot_vel = 360.0f / Nrotations;
	Rot_vel *= 2.0 * 3.1415926535 / 360.0;

	if ( Ndistances > 1 )
		// Dist_vel will be used like this:
		// d = Dist_1 + dist_step * Dist_vel
		Dist_vel = (Dist_2 - Dist_1) / (Ndistances - 1);
	else
		Dist_vel = 0.0;

	if ( geom_type == OBJ_FILE &&
		 (moving_node == osg::NullFC || fixed_node == osg::NullFC) )
	{
		fputs("bench: '-g file' or '-f' option given,\n"
			  "       but moving node or fixed node is still NULL!", stderr );
		exit(-1);
	}
}