Example #1
0
VIOAPI Status  mni_input_keyword_and_equal_sign(
    FILE         *file,
    const char   keyword[],
    BOOLEAN     print_error_message )
{
    Status     status;
    STRING     str;

    status = mni_input_string( file, &str, (char) '=', (char) 0 );

    if( status == END_OF_FILE )
        return( status );

    if( status != OK || !equal_strings( str, (STRING) keyword ) ||
        mni_skip_expected_character( file, (char) '=' ) != OK )
    {
        if( print_error_message )
            print_error( "Expected \"%s =\"\n", keyword );
        status = ERROR;
    }

    delete_string( str );

    return( status );
}
Example #2
0
VIOAPI  VIO_Status  initialize_tag_file_input(
    FILE      *file,
    int       *n_volumes_ptr )
{
    VIO_STR  line;
    int     n_volumes;

    /* parameter checking */

    if( file == NULL )
    {
        print_error( "initialize_tag_file_input(): passed NULL FILE ptr.\n");
        return( VIO_ERROR );
    }

    /* okay read the header */

    if( mni_input_string( file, &line, (char) 0, (char) 0 ) != VIO_OK ||
        !equal_strings( line, (VIO_STR) TAG_FILE_HEADER ) )
    {
        print_error( "input_tag_points(): invalid header in file.\n");
        delete_string( line );
        return( VIO_ERROR );
    }

    delete_string( line );

    /* now read the number of volumes */

    if( mni_input_keyword_and_equal_sign( file, VOLUMES_STRING, TRUE ) != VIO_OK )
        return( VIO_ERROR );

    if( mni_input_int( file, &n_volumes ) != VIO_OK )
    {
        print_error( "input_tag_points(): expected # volumes after %s.\n",
                     VOLUMES_STRING );
        return( VIO_ERROR );
    }

    if( mni_skip_expected_character( file, (char) ';' ) != VIO_OK )
        return( VIO_ERROR );

    if( n_volumes != 1 && n_volumes != 2 )
    {
        print_error( "input_tag_points(): invalid # volumes: %d \n",
                     n_volumes );
        return( VIO_ERROR );
    }

    /* now read the tag points header */

    if( mni_input_keyword_and_equal_sign( file, TAG_POINTS_STRING, TRUE ) != VIO_OK)
        return( VIO_ERROR );

    if( n_volumes_ptr != NULL )
        *n_volumes_ptr = n_volumes;

    return( VIO_OK );
}
Example #3
0
VIOAPI Status  mni_input_reals(
    FILE    *file,
    int     *n,
    Real    *reals[] )
{
    Real  d;

    *n = 0;

    while( mni_input_real( file, &d ) != ERROR )
    {
        ADD_ELEMENT_TO_ARRAY( *reals, *n, d, DEFAULT_CHUNK_SIZE );
    }

    return( mni_skip_expected_character( file, (char) ';' ) );
}
Example #4
0
/* reads in a Kernel from a file                        */
VIO_Status input_kernel(const char *kernel_file, Kernel * kernel)
{
   int      i, j;

   VIO_STR   line;
   VIO_STR   type_name;
   VIO_STR   str;
   VIO_Real     tmp_real;
   FILE    *file;

   /* parameter checking */
   if(kernel_file == NULL){
      print_error("input_kernel(): passed NULL FILE.\n");
      return (VIO_ERROR);
      }

   file = fopen(kernel_file, "r");
   if(file == NULL){
      print_error("input_kernel(): error opening Kernel file.\n");
      return (VIO_ERROR);
      }

   /* okay read the header */
   if(mni_input_string(file, &line, (char)0, (char)0) != VIO_OK){
      delete_string(line);
      print_error("input_kernel(): could not read header in file.\n");
      return (VIO_ERROR);
      }

   if(!equal_strings(line, KERNEL_FILE_HEADER)){
      delete_string(line);
      print_error("input_kernel(): invalid header in file.\n");
      return (VIO_ERROR);
      }

   /* --- read the type of Kernel */
   if(mni_input_keyword_and_equal_sign(file, KERNEL_TYPE, FALSE) != VIO_OK){
      return (VIO_ERROR);
      }

   if(mni_input_string(file, &type_name, (char)';', (char)0) != VIO_OK){
      print_error("input_kernel(): missing kernel type.\n");
      return (VIO_ERROR);
      }

   if(mni_skip_expected_character(file, (char)';') != VIO_OK){
      return (VIO_ERROR);
      }

   if(!equal_strings(type_name, NORMAL_KERNEL)){
      print_error("input_kernel(): invalid kernel type.\n");
      delete_string(type_name);
      return (VIO_ERROR);
      }
   delete_string(type_name);

   /* --- read the next string */
   if(mni_input_string(file, &str, (char)'=', (char)0) != VIO_OK)
      return (VIO_ERROR);

   if(!equal_strings(str, KERNEL)){
      print_error("Expected %s =\n", KERNEL);
      delete_string(str);
      return (VIO_ERROR);
      }
   delete_string(str);

   if(mni_skip_expected_character(file, (char)'=') != VIO_OK){
      return (VIO_ERROR);
      }

   /* now read the elements (lines) of the kernel */
   if(verbose){
      fprintf(stderr, "Reading [%s]", kernel_file);
      }
   for(i = 0; i < MAX_KERNEL_ELEMS; i++){

      /* allocate a bit of memory */
      SET_ARRAY_SIZE(kernel->K, kernel->nelems, kernel->nelems + 1, 10);
      ALLOC(kernel->K[i], KERNEL_DIMS + 1);

      /* get the 5 dimension vectors and the coefficient */
      for(j = 0; j < 6; j++){
         if(mni_input_real(file, &tmp_real) == VIO_OK){
            kernel->K[i][j] = tmp_real;
            }
         else {
            /* check for end */
            if(mni_skip_expected_character(file, (char)';') == VIO_OK){
               kernel->nelems = i;
               if(verbose){
                  fprintf(stderr, " %dx%d Kernel elements read\n", i, kernel->nelems);
                  }
               return (VIO_OK);
               }
            else {
               print_error("input_kernel(): error reading kernel [%d,%d]\n", i + 1,
                           j + 1);
               return (VIO_ERROR);
               }
            }
         }
      kernel->nelems++;

      if(verbose){
         fprintf(stderr, ".");
         fflush(stderr);
         }
      }

   /* SHOLDN'T BE REACHED */
   print_error("input_kernel(): Glark! Something is amiss in the State of Kansas\n");
   return (VIO_ERROR);
   }
Example #5
0
static VIO_Status read_one_tag(
    FILE      *file,
    int       n_volumes,
    VIO_Real      tags_volume1_ptr[],
    VIO_Real      tags_volume2_ptr[],
    VIO_Real      *weight_ptr,
    int       *structure_id_ptr,
    int       *patient_id_ptr,
    VIO_STR    *label_ptr )
{
    VIO_Status  status;
    VIO_STR  line;
    VIO_BOOL last_was_blank, in_quotes;
    int     n_strings, pos, i;
    VIO_Real    x1 = 0.0, y1 = 0.0, z1 = 0.0, x2 = 0.0, y2 = 0.0, z2 = 0.0;
    int     structure_id, patient_id;
    VIO_Real    weight;
    VIO_STR  label;

    /* parameter checking */

    if( file == NULL )
    {
        print_error( "read_one_tag(): passed NULL FILE ptr.\n");
        return( VIO_ERROR );
    }

    status = mni_input_real( file, &x1 );

    if( status == VIO_OK )
    {
        if( mni_input_real( file, &y1 ) != VIO_OK ||
            mni_input_real( file, &z1 ) != VIO_OK ||
            (n_volumes == 2 &&
             (mni_input_real( file, &x2 ) != VIO_OK ||
              mni_input_real( file, &y2 ) != VIO_OK ||
              mni_input_real( file, &z2 ) != VIO_OK)) )
        {
            print_error( "read_one_tag(): error reading tag point\n" );
            return( VIO_ERROR );
        }

        if( tags_volume1_ptr != NULL )
        {
            tags_volume1_ptr[VIO_X] = x1;
            tags_volume1_ptr[VIO_Y] = y1;
            tags_volume1_ptr[VIO_Z] = z1;
        }

        if( n_volumes == 2 && tags_volume2_ptr != NULL )
        {
            tags_volume2_ptr[VIO_X] = x2;
            tags_volume2_ptr[VIO_Y] = y2;
            tags_volume2_ptr[VIO_Z] = z2;
        }

        label = NULL;
        weight = 0.0;
        structure_id = -1;
        patient_id = -1;

        n_strings = 0;
        if( mni_input_line( file, &line ) == VIO_OK )
        {
            i = 0;
            last_was_blank = TRUE;
            in_quotes = FALSE;
            while( line[i] != VIO_END_OF_STRING )
            {
                if( line[i] == ' ' || line[i] == '\t' )
                {
                    last_was_blank = TRUE;
                }
                else
                {
                    if( last_was_blank && !in_quotes )
                        ++n_strings;

                    last_was_blank = FALSE;

                    if( line[i] == '\"' )
                        in_quotes = !in_quotes;
                }
                ++i;
            }

            while( i > 0 &&
                   (line[i] == ' ' || line[i] == '\t' ||
                    line[i] == VIO_END_OF_STRING) )
                --i;

            if( line[i] == ';' )
            {
                (void) unget_character( file, (char) ';' );
                line[i] = VIO_END_OF_STRING;
            }
        }

        if( n_strings != 0 )
        {
            if( n_strings == 1 )
            {
                label = extract_label( line );
            }
            else if( n_strings < 3 || n_strings > 4 ||
                     sscanf( line, "%lf %d %d %n", &weight, &structure_id,
                             &patient_id, &pos ) != 3 )
            {
                print_error( "input_tag_points(): error reading tag point\n" );
                return( VIO_ERROR );
            }
            else if( n_strings == 4 )
            {
                label = extract_label( &line[pos] );
            }
        }

        delete_string( line );

        if( weight_ptr != NULL )
            *weight_ptr = weight;

        if( structure_id_ptr != NULL )
            *structure_id_ptr = structure_id;

        if( patient_id_ptr != NULL )
            *patient_id_ptr = patient_id;

        if( label_ptr != NULL )
            *label_ptr = label;
        else
            delete_string( label );
    }

    if( status == VIO_ERROR )  /* --- found no more tag points, should now find ; */
    {
        if( mni_skip_expected_character( file, (char) ';' ) != VIO_OK )
            status = VIO_ERROR;
        else
            status = VIO_END_OF_FILE;
    }

    return( status );
}