/* config.c (C) 1997 Mark A. Livingston */ /* Any non-commercial use of this code is permitted so long as */ /* the above copyright line appears in the file. Please contact */ /* the author regarding permission for commercial use. */ #include #include #include #include "config.h" ConfigRec * ParseConfigFile( char *fname, int verbose, char *outFile ) { char buf[256]; int i; FILE *fp; ConfigRec *cR; /* Allocate the structure to return */ if( ( cR = (ConfigRec *) malloc( sizeof( ConfigRec ) ) ) == NULL ) { fprintf( stderr, "Cannot allocate space for configuration record\n" ); return NULL; } /* Fill it with the default values */ cR->posMaxFOB = cR->posMaxFaro = 0.005; cR->oriMaxFOB = cR->oriMaxFaro = 10.0; cR->minPoints = 2; cR->minWeight = 0.250; cR->cogDistance = 0.025; cR->kernelRadius = 0.10; cR->kernelSigma = 1.0; cR->xMin = 0.0, cR->xMax = 0.0; cR->yMin = 0.0, cR->yMax = 0.0; cR->zMin = 0.0, cR->zMax = 0.0; cR->xSteps = 1, cR->ySteps = 1, cR->zSteps = 1; cR->numDataFiles = 0; cR->verbose = verbose; cR->outfp = stdout; if( fname != NULL ) { if( ( fp = fopen( fname, "r" ) ) == NULL ) { fprintf( stderr, "Cannot open config file '%s'\n", fname ); return cR; } else if( verbose ) printf( "Opened config file %s\n", fname ); rewind( fp ); while( fgets( buf, 255, fp ) ) { if( buf[0] != '#' ) { if( !strncmp( buf, "position_max", 12 ) ) sscanf( buf, "position_max = %lf,%lf", &cR->posMaxFOB, &cR->posMaxFaro ); else if( !strncmp( buf, "orient_max", 10 ) ) sscanf( buf, "orient_max = %lf,%lf", &cR->oriMaxFOB, &cR->oriMaxFaro ); else if( !strncmp( buf, "kernel_radius", 13 ) ) sscanf( buf, "kernel_radius = %lf", &cR->kernelRadius ); else if( !strncmp( buf, "kernel_sigma", 12 ) ) sscanf( buf, "kernel_sigma = %lf", &cR->kernelSigma ); else if( !strncmp( buf, "cog_distance", 12 ) ) sscanf( buf, "cog_distance = %lf", &cR->cogDistance ); else if( !strncmp( buf, "min_points", 10 ) ) sscanf( buf, "min_points = %d", &cR->minPoints ); else if( !strncmp( buf, "min_weight", 10 ) ) sscanf( buf, "min_weight = %lf", &cR->minWeight ); else if( !strncmp( buf, "x", 1 ) ) sscanf( buf, "x = %lf,%lf %d", &cR->xMin, &cR->xMax, &cR->xSteps ); else if( !strncmp( buf, "y", 1 ) ) sscanf( buf, "y = %lf,%lf %d", &cR->yMin, &cR->yMax, &cR->ySteps ); else if( !strncmp( buf, "z", 1 ) ) sscanf( buf, "z = %lf,%lf %d", &cR->zMin, &cR->zMax, &cR->zSteps ); else if( !strncmp( buf, "data", 4 ) ) cR->numDataFiles++; } } /* now that we know how many data files there are, alloc space */ cR->sampFile = ( char ** ) malloc( cR->numDataFiles * sizeof( char * ) ); cR->transFile = ( char ** ) malloc( cR->numDataFiles * sizeof( char * )); cR->nPts = ( int * ) malloc( cR->numDataFiles * sizeof( int ) ); cR->nSampFOB = ( int * ) malloc( cR->numDataFiles * sizeof( int ) ); cR->nSampFARO = ( int * ) malloc( cR->numDataFiles * sizeof( int ) ); rewind( fp ); i = 0; cR->totalPts = 0; while( fgets( buf, 255, fp ) ) { if( !strncmp( buf, "data", 4 ) ) { cR->sampFile[i] = ( char * ) malloc( strlen( buf ) * sizeof( char )); cR->transFile[i] = ( char * ) malloc( strlen( buf ) * sizeof( char)); sscanf( buf, "data p = %d s=%d,%d file = %s trans = %s", &cR->nPts[i], &cR->nSampFOB[i], &cR->nSampFARO[i], cR->sampFile[i], cR->transFile[i] ); cR->totalPts += cR->nPts[i]; i++; } } fclose( fp ); if( verbose ) printf( "Configurations read\n" ); } if( outFile != NULL ) { if( ( cR->outfp = fopen( outFile, "w" ) ) == NULL ) { fprintf( stderr, "Unable to open file '%s' - using stdout\n", outFile); cR->outfp = stdout; } else if( verbose ) { printf( "Opened file '%s'\n", outFile ); } } return cR; }