Logo Search packages:      
Sourcecode: gandalf version File versions

Gan_SquMatrix_f* gan_squmatf_fscanf_q ( FILE *  fp,
Gan_SquMatrix_f A,
char *  prefix,
int  prefix_len 
)

Read square matrix from ASCII file.

Returns:
Result matrix A. Read square matrix A from file pointer fp. The prefix string for the matrix is read from the file into the prefix string, up to the maximum length prefix_len of the prefix string. Any remaining characters after prefix has been filled are ignored. Pass NULL for prefix and zero for prefix_len to ignore the prefix string. A should either be a pointer to a pre-allocated square matrix or NULL.

Pointer to the read matrix on success, NULL on failure.

See also:
gan_squmatf_fscanf_s().

Definition at line 386 of file matf_square.c.

References Gan_SquMatrix_f::data, GAN_DIAGONAL_MATRIX, gan_err_flush_trace(), gan_err_register, gan_err_test_ptr, GAN_LOWER_TRI_MATRIX, GAN_SCALED_IDENT_MATRIX, gan_squmatf_alloc, gan_squmatf_set_type_size(), GAN_SYMMETRIC_MATRIX, GAN_UPPER_TRI_MATRIX, and Gan_SquMatrix_f::size.

{
   int ch;
   unsigned long rows, cols, i, j;
   char type_string[TYPE_STRING_LENGTH];
   Gan_SquMatrixType type;

   /* read indentation */
   for(;;)
      if ( (ch = getc(fp)) != ' ' || ch == EOF )
         break;

   /* end of file means corrupted file */
   if ( ch == EOF )
   {
      gan_err_flush_trace();
      gan_err_register ( "gan_squmatf_fscanf_q", GAN_ERROR_CORRUPTED_FILE, "" );
      return NULL;
   }

   /* rewind one character to reread the prefix string */
   fseek ( fp, -1, SEEK_CUR );

   /* read prefix string, terminated by end-of-line */
   for ( prefix_len--; prefix_len > 0; prefix_len-- )
   {
      ch = getc(fp);
      if ( ch == EOF || ch == '(' ) break;
      if ( prefix != NULL ) *prefix++ = (char)ch;
   }

   gan_err_test_ptr ( ch == '(', "gan_squmatf_fscanf_q",
                      GAN_ERROR_CORRUPTED_FILE, "" );

   /* terminate string */
   if ( prefix != NULL ) prefix[-1] = '\0';

   /* read rest of string if necessary */
   if ( prefix_len == 0 )
   {
      for(;;)
      {
         ch = getc(fp);
         if ( ch == EOF || ch == '(' ) break;
      }

      gan_err_test_ptr ( ch == '(', "gan_squmatf_fscanf_q",
                         GAN_ERROR_CORRUPTED_FILE, "" );
   }

   /* read dimensions */
   if ( fscanf ( fp, "%ldx%ld ", &rows, &cols ) != 2 || rows != cols )
   {
      gan_err_flush_trace();
      gan_err_register ( "gan_squmatf_fscanf_q", GAN_ERROR_CORRUPTED_FILE, "");
      return NULL;
   }

   /* read matrix type string */
   for ( i = 0; i < TYPE_STRING_LENGTH; i++ )
   {
      type_string[i] = getc(fp);
      if ( type_string[i] == '\n' )
      {
         type_string[i] = '\0';
         break;
      }
   }

   gan_err_test_ptr ( i < TYPE_STRING_LENGTH, "gan_squmatf_fscanf_q",
                      GAN_ERROR_CORRUPTED_FILE, "" );

   /* interpret type string */
   if ( strcmp ( type_string, "symmetric matrix)" ) == 0 )
      type = GAN_SYMMETRIC_MATRIX;
   else if ( strcmp ( type_string, "diagonal matrix)" ) == 0 )
      type = GAN_DIAGONAL_MATRIX;
   else if ( strcmp ( type_string, "scaled identity matrix)" ) == 0 )
      type = GAN_SCALED_IDENT_MATRIX;
   else if ( strcmp ( type_string, "lower triangular matrix)" ) == 0 )
      type = GAN_LOWER_TRI_MATRIX;
   else if ( strcmp ( type_string, "upper triangular matrix)" ) == 0 )
      type = GAN_UPPER_TRI_MATRIX;
   else
   {
      gan_err_flush_trace();
      gan_err_register ( "gan_squmatf_fscanf_q", GAN_ERROR_CORRUPTED_FILE, "");
      return NULL;
   }

   /* create/set matrix */
   if ( A == NULL )
      A = gan_squmatf_alloc ( type, rows );
   else
      A = gan_squmatf_set_type_size ( A, type, rows );

   if ( A == NULL )
   {
      gan_err_register ( "gan_squmatf_fscanf_q", GAN_ERROR_FAILURE, "" );
      return NULL;
   }

   /* read matrix data */
   switch ( type )
   {
      float tmp;

      case GAN_SYMMETRIC_MATRIX:
        for ( i = 0; i < A->size; i++ )
        {
           for ( j = 0; j < i; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 ||
                   tmp != A->data[i*(i+1)/2+j] )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           for ( ; j < A->size; j++ )
              if ( fscanf ( fp, "%f", &A->data[j*(j+1)/2+i] ) != 1 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           /* read end-of-line character */
#ifdef NDEBUG
           getc(fp);
#else
           gan_err_test_ptr ( getc(fp) == '\n', "gan_squmatf_fscanf_q",
                              GAN_ERROR_CORRUPTED_FILE, "" );
#endif
        }
        break;

      case GAN_DIAGONAL_MATRIX:
        for ( i = 0; i < A->size; i++ )
        {
           for ( j = 0; j < i; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           if ( fscanf ( fp, "%f", &A->data[i] ) != 1 )
           {
              gan_err_flush_trace();
              gan_err_register ( "gan_squmatf_fscanf_q",
                                 GAN_ERROR_CORRUPTED_FILE, "" );
              return NULL;
           }

           for ( j = i+1; j < A->size; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           /* read end-of-line character */
#ifdef NDEBUG
           getc(fp);
#else
           gan_err_test_ptr ( getc(fp) == '\n', "gan_squmatf_fscanf_q",
                              GAN_ERROR_CORRUPTED_FILE, "" );
#endif
        }
        break;

      case GAN_SCALED_IDENT_MATRIX:
        for ( i = 0; i < A->size; i++ )
        {
           for ( j = 0; j < i; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           if ( (i == 0 && fscanf ( fp, "%f", &A->data[0] ) != 1) ||
                (i > 0 && (fscanf ( fp, "%f", &tmp ) != 1 ||
                           tmp != A->data[0])) )
           {
              gan_err_flush_trace();
              gan_err_register ( "gan_squmatf_fscanf_q",
                                 GAN_ERROR_CORRUPTED_FILE, "" );
              return NULL;
           }

           for ( j = i+1; j < A->size; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           /* read end-of-line character */
#ifdef NDEBUG
           getc(fp);
#else
           gan_err_test_ptr ( getc(fp) == '\n', "gan_squmatf_fscanf_q",
                              GAN_ERROR_CORRUPTED_FILE, "" );
#endif
        }
        break;

      case GAN_LOWER_TRI_MATRIX:
        for ( i = 0; i < A->size; i++ )
        {
           for ( j = 0; j <= i; j++ )
              if ( fscanf ( fp, "%f", &A->data[i*(i+1)/2+j] ) != 1 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           for ( ; j < A->size; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           /* read end-of-line character */
#ifdef NDEBUG
           getc(fp);
#else
           gan_err_test_ptr ( getc(fp) == '\n', "gan_squmatf_fscanf_q",
                              GAN_ERROR_CORRUPTED_FILE, "" );
#endif
        }
        break;

      case GAN_UPPER_TRI_MATRIX:
        for ( i = 0; i < A->size; i++ )
        {
           for ( j = 0; j <= i; j++ )
              if ( fscanf ( fp, "%f", &tmp ) != 1 || tmp != 0.0 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           for ( ; j < A->size; j++ )
              if ( fscanf ( fp, "%f", &A->data[j*(j+1)/2+i] ) != 1 )
              {
                 gan_err_flush_trace();
                 gan_err_register ( "gan_squmatf_fscanf_q",
                                    GAN_ERROR_CORRUPTED_FILE, "" );
                 return NULL;
              }

           /* read end-of-line character */
#ifdef NDEBUG
           getc(fp);
#else
           gan_err_test_ptr ( getc(fp) == '\n', "gan_squmatf_fscanf_q",
                              GAN_ERROR_CORRUPTED_FILE, "" );
#endif
        }
        break;

      default:
        gan_err_flush_trace();
        gan_err_register ( "gan_squmatf_fscanf_q", GAN_ERROR_ILLEGAL_TYPE, "");
        break;
   }

   /* success */
   return A;
}


Generated by  Doxygen 1.6.0   Back to index