示例#1
0
文件: dmio.c 项目: dawuweijun/otpo
int
dspio_readHB_mat_double( const char* filename,
                         int row_i, int row_f,
                         int** p_rowptr,
                         int* p_nnz,
                         int** p_colind,
                         double** p_val )
{
/****************************************************************************/
/*  This function opens and reads the specified file, interpreting its      */
/*  contents as a sparse matrix stored in the Harwell/Boeing standard       */
/*  format and creating compressed column storage scheme vectors to hold    */
/*  the index and nonzero value information.                                */
/*                                                                          */
/*    ----------                                                            */
/*    **CAVEAT**                                                            */
/*    ----------                                                            */
/*  Parsing real formats from Fortran is tricky, and this file reader       */
/*  does not claim to be foolproof.   It has been tested for cases when     */
/*  the real values are printed consistently and evenly spaced on each      */
/*  line, with Fixed (F), and Exponential (E or D) formats.                 */
/*                                                                          */
/*  **  If the input file does not adhere to the H/B format, the  **        */
/*  **             results will be unpredictable.                 **        */
/*                                                                          */
/****************************************************************************/
  FILE *in_file;
  int i,j,ind,col,offset,count,last,Nrhs;
  int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  int Nrow, Ncol, Nnzero;
  int Ptrperline, Ptrwidth, Indperline, Indwidth;
  int Valperline, Valwidth, Valprec;
  int Valflag;           /* Indicates 'E','D', or 'F' float format */
  char* ThisElement;
  char Title[73], Key[9], Type[4], Rhstype[4];
  char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  char line[BUFSIZ];

  int* rowptr;
  int* colind;
  int  len_colind;
  double* val;
  int  nr = row_f - row_i + 1;
  int  temp_elem;
  int  rownum;
  int  running_offset;
  int  new_insert_pos;

  list_t* symm_entries;    /* store list of symmetric entries to add */
  int* all_rowptr;
  int len_extra_symm;   /* # of extra entries due to symmetry */
  fpos_t save_pos;

  dlist_t* symm_vals;
  fpos_t val_save_pos;
  FILE*  val_in_file;
  char val_line[BUFSIZ];
  int val_col;
  int val_ind;
  int val_i;
  int val_skip_lines;
  char* val_ThisElement;
  double val_temp_elem;

  in_file = fopen( filename, "r");
  assert( in_file != NULL );

  readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);

  assert( Nrow == Ncol );
  assert( Type[0] == 'R' || Type[0] == 'r' );
  assert( Type[1] == 'S' || Type[1] == 's' );
  assert( Type[2] == 'A' || Type[2] == 'a' );

  /*  Parse the array input formats from Line 3 of HB file  */
  ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
  ParseIfmt(Indfmt,&Indperline,&Indwidth);
  ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);

  /*  Read row pointer array:   */
  offset = 1;

  rowptr = (int *)malloc( sizeof(int)*(nr + 1) );
  *p_rowptr = rowptr;
  if( rowptr == NULL )
    IOHBTerminate( "Out of memory allocating row pointer" );

  all_rowptr = (int *)malloc( sizeof(int)*(row_f+2) );
  if( all_rowptr == NULL )
    IOHBTerminate( "Out of memory allocating all row pointer" );

  symm_entries = (list_t *)malloc( sizeof(list_t)*(row_f+1) );
  symm_vals = (dlist_t *)malloc( sizeof(dlist_t)*(row_f+1) );
  if( symm_entries == NULL || symm_vals == NULL )
    IOHBTerminate( "Out of memory allocating symmetric entries" );
  for( i = 0; i < (row_f+1); i++ ) {
    lst_init( &(symm_entries[i]) );
    dlst_init( &(symm_vals[i]) );
  }

  ThisElement = (char *) malloc(Ptrwidth+1);
  if ( ThisElement == NULL )
    IOHBTerminate("Insufficient memory for ThisElement (1).");
  *(ThisElement+Ptrwidth) = (char) NULL;
  count = 0;
  for( i = 0; i < Ptrcrd; i++ ) {
    fgets( line, BUFSIZ, in_file );
    if ( sscanf(line, "%*s") < 0 ) 
      IOHBTerminate("--- Null (or blank) line in ptr data region ---\n");
    col = 0;
    for( ind = 0; ind < Ptrperline; ind++ ) {

      if( count > Ncol || count > row_f+1 )
        break;

      /* copy and convert index */
      strncpy( ThisElement, line+col, Ptrwidth );
      temp_elem = atoi(ThisElement) - offset;

      if( count >= row_i ) {
        /* only save elements in the proper range for this processor */
        rowptr[ count - row_i ] = temp_elem;
      }

      all_rowptr[ count ] = temp_elem;

      count++;
      col += Ptrwidth;
    }
  }
  free(ThisElement);

#ifndef NDEBUG_IO
fprintf( stderr, "printing rowptr...\n" );
for( i = 0; i <= nr; i++ )
  fprintf( stderr, "rowptr[%d]==%d\n", i, rowptr[i] );
fprintf( stderr, "printing all_rowptr...\n" );
for( i = 0; i <= row_f+1; i++ )
  fprintf( stderr, "all_rowptr[%d]==%d\n", i, all_rowptr[i] );
#endif


  /* alloc space for column indices and values */

  ThisElement = (char *) malloc(Indwidth+1);
  if ( ThisElement == NULL )
    IOHBTerminate("Insufficient memory for ThisElement (2).");
  *(ThisElement+Indwidth) = (char) NULL;
  val_ThisElement = (char *) malloc(Valwidth+2);
  if ( val_ThisElement == NULL )
    IOHBTerminate("Insufficient memory for val_ThisElement (2).");
  *(val_ThisElement+Valwidth) = (char) NULL;
  *(val_ThisElement+Valwidth+1) = (char) NULL;

  len_extra_symm = 0;
  fgetpos( in_file, &save_pos );
  rownum = 0;

  /* position at analogous place in file for values */
  val_in_file = fopen( filename, "r" );
  assert( val_in_file != NULL );
  val_skip_lines = 4 + (Rhscrd > 0) + Ptrcrd + Indcrd;

#ifndef NDEBUG_IO
fprintf( stderr, "header lines: %d (%d), ptr lines: %d, ind lines: %d\n",
         4 + (Rhscrd>0), Rhscrd, Ptrcrd, Indcrd );
fprintf( stderr, "skipping %d lines to get to vals...\n", val_skip_lines );
#endif

  for( i = 0; i < val_skip_lines; i++ ) {
    fgets( line, BUFSIZ, val_in_file );
  }
  fgetpos( val_in_file, &val_save_pos );

  count = 0;
  /*  for( i = 0; i < Indcrd; i++ ) {*/
  i = 0;       /* line number */
  col = 0;     /* column number on a given line */
  ind = 0;     /* item number on a given line */
  val_col = 0; /* analogous */
  val_ind = 0;
  val_i = 0;
  while( i <= Indcrd ) {

    /* read new lines, if necessary */
    if( (ind % Indperline) == 0 ) {
      fgets( line, BUFSIZ, in_file );
      if ( sscanf(line,"%*s") < 0 )
        IOHBTerminate("--- Null (or blank) line in index data region ---\n");
      i++;

      col = 0;
    }

    if( (val_ind % Valperline) == 0 ) {
      fgets( val_line, BUFSIZ, val_in_file );
      if( sscanf(val_line, "%*s") < 0 )
        IOHBTerminate("--- Null (or blank) line in value region ---\n");
      val_i++;

      val_col = 0;

      if( Valflag == 'D' ) {
        while( strchr(val_line, 'D') ) *strchr(val_line,'D') = 'E';
      }
    }

    if (count == Nnzero) break;

    /* read an entry from this line */
    strncpy( ThisElement, line+col, Indwidth );
    col += Indwidth;
    ind++;

    strncpy( val_ThisElement, val_line + val_col, Valwidth );
    *(val_ThisElement+Valwidth) = (char) NULL;
    val_col += Valwidth;
    val_ind++;
    if ( Valflag != 'F' && strchr(val_ThisElement,'E') == NULL ) {
      /* insert a char prefix for exp */
      last = strlen(val_ThisElement);
      for (j=last+1;j>=0;j--) {
        val_ThisElement[j] = val_ThisElement[j-1];
        if ( val_ThisElement[j] == '+' || val_ThisElement[j] == '-' ) {
          val_ThisElement[j-1] = Valflag;
          break;
        }
      }
    }

    /* done */
    if( count >= rowptr[nr] )
      break;

    temp_elem = atoi( ThisElement ) - offset;
    val_temp_elem = atof( val_ThisElement );

#ifndef NDEBUG_IO
    fprintf( stderr, "found: %d (%f)\n", temp_elem, val_temp_elem );
#endif

    if( temp_elem >= row_i && temp_elem <= row_f ) {
      if( temp_elem != rownum ) { /* ignore diagonal entry */

#ifndef NDEBUG_IO
        fprintf( stderr, "new entry: (%d, %d) == %f\n",
                 temp_elem, rownum, val_temp_elem );
#endif

        len_extra_symm++;
        lst_insert( &(symm_entries[temp_elem]), rownum );
        dlst_insert( &(symm_vals[temp_elem]), val_temp_elem );
      }
    }

    count++;
    if( count >= all_rowptr[rownum+1] )
      rownum++;
  }


#ifndef NDEBUG_IO
fprintf( stderr, "extra entries due to symmetry: %d\n", len_extra_symm );
#endif

  len_colind = rowptr[nr] - rowptr[0] + len_extra_symm;
  *p_nnz = len_colind;
  colind = (int *)malloc( sizeof(int)*len_colind );
  *p_colind = colind;

#ifndef NDEBUG_IO
fprintf( stderr, "nnz == %d\n", len_colind );
#endif

  val = (double *)malloc( sizeof(double)*len_colind );
  *p_val = val;

  assert( colind != NULL && val != NULL );

  /*  Read column index array:  */
  fsetpos( in_file, &save_pos );
  i = 0;
  col = 0;
  ind = 0;

  fsetpos( val_in_file, &val_save_pos );
  val_i = 0;
  val_col = 0;
  val_ind = 0;

  count = 0;
  rownum = 0;
  running_offset = 0;

  while( i <= Indcrd ) {

    /* read new lines, if necessary */
    if( (ind % Indperline) == 0 ) {
      fgets( line, BUFSIZ, in_file );
      if ( sscanf(line,"%*s") < 0 )
        IOHBTerminate("--- Null (or blank) line in index data region ---\n");
      i++;

      col = 0;
    }

    if( (val_ind % Valperline) == 0 ) {
      fgets( val_line, BUFSIZ, val_in_file );
      if( sscanf(val_line, "%*s") < 0 )
        IOHBTerminate("--- Null (or blank) line in value region ---\n");
      val_i++;

      val_col = 0;

      if( Valflag == 'D' ) {
        while( strchr(val_line, 'D') ) *strchr(val_line,'D') = 'E';
      }
    }


    if (count == Nnzero) break;


    /* read an entry from this line */
    strncpy( ThisElement, line+col, Indwidth );
    col += Indwidth;
    ind++;

    strncpy( val_ThisElement, val_line + val_col, Valwidth );
    *(val_ThisElement+Valwidth) = (char) NULL;
    val_col += Valwidth;
    val_ind++;
    if ( Valflag != 'F' && strchr(val_ThisElement,'E') == NULL ) {
      /* insert a char prefix for exp */
      last = strlen(val_ThisElement);
      for (j=last+1;j>=0;j--) {
        val_ThisElement[j] = val_ThisElement[j-1];
        if ( val_ThisElement[j] == '+' || val_ThisElement[j] == '-' ) {
          val_ThisElement[j-1] = Valflag;
          break;
        }
      }
    }

    /* done */
    if( count >= all_rowptr[nr + row_i] )
      break;

    temp_elem = atoi( ThisElement ) - offset;
    val_temp_elem = atof( val_ThisElement );

#ifndef NDEBUG_IO
    fprintf( stderr, "entry: (%d, %d) == %f\n", rownum, temp_elem, val_temp_elem );
#endif

    if( count >= all_rowptr[ row_i ] ) {
      new_insert_pos = count - all_rowptr[row_i] + running_offset;

#ifndef NDEBUG_IO
      fprintf( stderr, "inserting into colind[%d]: %d\n",
               new_insert_pos, temp_elem );
#endif
      colind[ new_insert_pos ] = temp_elem;
      val[ new_insert_pos ] = val_temp_elem;
    }

    count++;
    if( count >= all_rowptr[rownum+1] ) {
      rownum++;

      if( rownum >= row_i && rownum <= row_f+1 ) {
#ifndef NDEBUG_IO
        fprintf( stderr, "rowptr[%d] (== %d) += %d\n", rownum - row_i, rowptr[ rownum - row_i ], running_offset );
#endif
        rowptr[ rownum - row_i ] += running_offset;
      }

      if( rownum >= row_i && rownum <= row_f ) {
        /* insert symmetric entries, if any */
        int new_additions = 0;
        list_node_t* trace = symm_entries[rownum].head;
        dlist_node_t* dtrace = symm_vals[rownum].head;

#ifndef NDEBUG_IO
        fprintf( stderr, "symm_entry[%d].length == %d\n", rownum, symm_entries[rownum].length );
#endif

        new_insert_pos = count - all_rowptr[row_i] + running_offset;
        while( trace != NULL ) {
          assert( dtrace != NULL );

#ifndef NDEBUG_IO
          fprintf( stderr, "inserting symmetric entry: colind[%d] = %d\n",
                   new_insert_pos + new_additions, trace->value );
          fprintf( stderr, "inserting symmetric value: val[%d] = %f\n",
                   new_insert_pos + new_additions, dtrace->value );
#endif
          colind[ new_insert_pos + new_additions ] = trace->value;
          val[ new_insert_pos + new_additions ] = dtrace->value;
          trace = trace->next;
          dtrace = dtrace->next;
          new_additions++;
        } /* while */
        assert( new_additions == symm_entries[rownum].length );
        assert( new_additions == symm_vals[rownum].length );
        running_offset += new_additions;
      } /* rownum >= row_i ... */
    } /* count >= ... */
  }

  free(ThisElement);
  free(val_ThisElement);


  for( i = 0; i < row_f+1; i++ ) {
    lst_destroy( &(symm_entries[i]) );
  }
  for( i = 0; i < row_f+1; i++ ) {
    dlst_destroy( &(symm_vals[i]) );
  }
  free( symm_entries );
  free( symm_vals );
  free( all_rowptr );

  fclose( in_file );
  fclose( val_in_file );
  return 1;
}
示例#2
0
FILE * readHB_ind(const char* filename, int colptr[], int rowind[])
{
/****************************************************************************/
/*  This function opens and reads the specified file, interpreting its      */
/*  contents as a sparse matrix stored in the Harwell/Boeing standard       */
/*  format and creating compressed column storage scheme vectors to hold    */
/*  the index information.                                                  */
/*                                                                          */
/*    ----------                                                            */
/*    **CAVEAT**                                                            */
/*    ----------                                                            */
/*  Parsing real formats from Fortran is tricky, and this file reader       */
/*  does not claim to be foolproof.   It has been tested for cases when     */
/*  the real values are printed consistently and evenly spaced on each      */
/*  line, with Fixed (F), and Exponential (E or D) formats.                 */
/*                                                                          */
/*  **  If the input file does not adhere to the H/B format, the  **        */
/*  **             results will be unpredictable.                 **        */
/*                                                                          */
/****************************************************************************/
    FILE *in_file;
    int i,ind,col,offset,count;
    int Nrow,Ncol,Nnzero,Nrhs;
    int Ptrcrd, Indcrd, Valcrd, Rhscrd;
    int Ptrperline, Ptrwidth, Indperline, Indwidth;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    char* ThisElement;
    char line[BUFSIZ];
    char Title[73], Key[8], Type[4], Rhstype[4];
    char Ptrfmt[17], Indfmt[17], Rhsfmt[21], Valfmt[21];

    if ( (in_file = fopen( filename, "r")) == NULL ) {
       fprintf(stderr,"Error: Cannot open file: %s\n",filename);
       return 0;
    }

    readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                  Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                  &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);

/*  Parse the array input formats from Line 3 of HB file  */
    ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
    if (Valflag == 'D') {
          *strchr(Valfmt,'D') = 'E';
    }

/*  Read column pointer array:   */

    offset = 1-_SP_base;  /* if base 0 storage is declared (via macro definition), */
                          /* then storage entries are offset by 1                  */

    ThisElement = (char *) malloc(Ptrwidth+1);
    if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.\nhb2mtxstrm.c: Line 216 approx.");
    count=0;
    for (i=0;i<Ptrcrd;i++)
    {
       fgets(line, BUFSIZ, in_file);
       if ( sscanf(line,"%*s") < 0 ) 
         IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n");
       col =  0;
       for (ind = 0;ind<Ptrperline;ind++)
       {
          if (count > Ncol) break;
          strncpy(ThisElement,line+col,Ptrwidth);
          *(ThisElement+Ptrwidth) = (char) NULL;
/*          ThisElement = substr(line,col,Ptrwidth); */
          colptr[count] = atoi(ThisElement)-offset;
          count++; col += Ptrwidth;
       }
    }
    free(ThisElement);

/*  Read row index array:  */

    ThisElement = (char *) malloc(Ptrwidth+1);
    if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.\nhb2mtxstrm.c: Line 238 approx.");
    count = 0;
    for (i=0;i<Indcrd;i++)
    {
       fgets(line, BUFSIZ, in_file);
       if ( sscanf(line,"%*s") < 0 ) 
         IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n");
       col =  0;
       for (ind = 0;ind<Indperline;ind++)
       {
          if (count == Nnzero) break;
          strncpy(ThisElement,line+col,Indwidth);
          *(ThisElement+Indwidth) = (char) NULL;
          /* ThisElement = substr(line,col,Indwidth);*/
          rowind[count] = atoi(ThisElement)-offset;
          count++; col += Indwidth;
       }
    }
    free(ThisElement);
    return in_file;
}
示例#3
0
void main(int argc, char *argv[])
{
    FILE *in_file;
    char Title[73], Key[9], Rhstype[4];
    char Type[4];
    char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
    int Ptrcrd, Indcrd, Valcrd, Rhscrd;
    int Indperline, Indwidth;
    int Valperline, Valwidth, Valprec;
    int Valflag;           /* Indicates 'E','D', or 'F' float format */
    int Nrow, Ncol, Nnzero;
    int Nrhs;
    char* ThisElement;
    char format[30];
    char rformat[30];
    int *colptr, *rowind;
    int *colcount;
    int i,j, repeat, count, col, ind, items, last;
    char line[BUFSIZ];
    MM_typecode matcode;

    if (argc != 2) {
      printf("Usage: %s HBfile\n\n", argv[0]);
      printf("   Sends Matrix Market formatted output to stdout\n");
      exit(-1);
    } 

    in_file = fopen( argv[1], "r");
    if (in_file == NULL)
    {
       fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
       exit(1);
    }

    readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
                  Ptrfmt, Indfmt, Valfmt, Rhsfmt,
                  &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
    fclose(in_file);

    if (Type[0] == 'P' ) {
      fprintf(stderr,"This is a streaming translator for LARGE files with ");
      fprintf(stderr,"REAL or COMPLEX data.  Use 'hbmat2mtx' for PATTERN matrices.\n");
      exit(1);
    }
    in_file = readHB_newind(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind);
    ParseIfmt(Indfmt,&Indperline,&Indwidth);
    sprintf(format,"%%%dd %%%dd ",Indwidth,Indwidth);
    ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
    sprintf(rformat,"%%%ds ",Valwidth);
    ThisElement = (char *) malloc(Valwidth+1);


/*  Skip to values in hb file: */

   mm_set_matrix(&matcode);
   mm_set_coordinate(&matcode);
   if ( Type[0] == 'R' )
    mm_set_real(&matcode);
   else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
   else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
   }
   if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
   else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
   else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
   else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
   else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
   }
   if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
   }
    mm_write_banner(stdout, matcode);
    fprintf(stdout,"%% RBTitle: %s\n",Title);
    fprintf(stdout,"%% RBKey:   %s\n",Key);
    mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

    if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.\nhb2mtxstrm.c: Line 117 approx.");
    repeat=0;
    if ( Type[0] == 'C') repeat=1;
    count = 0;
    colcount = &colptr[1];
    items = 0; 
    for (i=0;i<Valcrd;i++)
    {
       fgets(line, BUFSIZ, in_file);
       if ( sscanf(line,"%*s") < 0 ) {
         fprintf(stderr,"iohb.c: Null (or blank) line in value data region of HB file.\n");
         exit(1);
       }
       if (Valflag == 'D') {
          while( strchr(line,'D') ) *strchr(line,'D') = 'E';
       }
       col =  0;
       for (ind = 0;ind<Valperline;ind++)
       {
          if (count == Nnzero) break;
          if (count == *colcount-1) colcount++;
          strncpy(ThisElement,line+col,Valwidth);
          *(ThisElement+Valwidth) = (char) NULL;
          if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
             /* insert a char prefix for exp */
             last = strlen(ThisElement);
             for (j=last+1;j>=0;j--) {
                ThisElement[j] = ThisElement[j-1];
                if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
                   ThisElement[j-1] = Valflag;
                   break;
                }
             }
          }
         items++;
         if (items==1) fprintf(stdout,format,rowind[count],colcount-colptr);
         if (items > repeat) { 
            fprintf(stdout,rformat,ThisElement);
            fprintf(stdout,"\n");
            count++; 
            items=0;
         } else {
            fprintf(stdout,rformat,ThisElement);
         }
         col += Valwidth;
       }
    }
       
}
示例#4
0
文件: hbmat2mtx.c 项目: nschloe/hbio
int main(int argc, char *argv[])
{
  FILE *in_file;
  char Title[73], Key[9], Rhstype[4];
  char Type[4];
  char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  int Ptrcrd, Indcrd, Valcrd;
  int Rhscrd = 0;
  int Indperline, Indwidth;
  int Valperline, Valwidth, Valprec;
  int Valflag;           /* Indicates 'E','D', or 'F' float format */
  int Nrow, Ncol, Nnzero;
  int Nrhs;
  char *valc = NULL;
  char *Valfmtc;
  char *tmp1;
  char *tmp2;
  char format[30];
  char rformat[30];
  char cformat[30];
  int *colptr, *rowind;
  int i,j, indxval, rowp1, colp1;
  MM_typecode matcode;

  if (argc != 2) {
    printf("Usage: %s HBfile \n", argv[0]);
    exit(-1);
  }

  in_file = fopen( argv[1], "r");
  if (in_file == NULL)
  {
    fprintf(stderr,"Error: Cannot open file: %s\n",argv[1]);
    exit(1);
  }

  readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
      Ptrfmt, Indfmt, Valfmt, Rhsfmt,
      &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  fclose(in_file);

  readHB_newmat_char(argv[1], &Nrow, &Ncol, &Nnzero, &colptr, &rowind, &valc,
      &Valfmtc);
  ParseIfmt(Indfmt,&Indperline,&Indwidth);
  ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  sprintf(format,"%%%dd %%%dd \n",Indwidth,Indwidth);
  sprintf(rformat,"%%%dd %%%dd %%%ds\n",Indwidth,Indwidth,Valwidth);
  sprintf(cformat,"%%%dd %%%dd %%%ds %%%ds\n",Indwidth,Indwidth,Valwidth,Valwidth);


  mm_set_matrix(&matcode);
  mm_set_coordinate(&matcode);
  if ( Type[0] == 'R' )
    mm_set_real(&matcode);
  else if ( Type[0] == 'C' )
    mm_set_complex(&matcode);
  else if ( Type[0] == 'P' )
    mm_set_pattern(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",Type);
    exit(1);
  }
  if ( Type[1] == 'U' || Type[1] == 'R' )
    mm_set_general(&matcode);
  else if ( Type[1] == 'S' )
    mm_set_symmetric(&matcode);
  else if ( Type[1] == 'Z' )
    mm_set_skew(&matcode);
  else if ( Type[1] == 'H' )
    mm_set_hermitian(&matcode);
  else {
    fprintf(stderr,"Unrecognized field in HB Type: %1s",&Type[1]);
    exit(1);
  }
  if ( Type[2] != 'A' ){
    fprintf(stderr,"Unrecognized format in HB Type: %1s",&Type[2]);
    exit(1);
  }
  mm_write_banner(stdout, matcode);
  fprintf(stdout,"%% RBTitle: %s\n",Title);
  fprintf(stdout,"%% RBKey:   %s\n",Key);
  mm_write_mtx_crd_size(stdout, Nrow, Ncol, Nnzero);

  if ( Type[0] == 'C' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        indxval = 2*(i-1);
        rowp1 = rowind[i-1]+1-1;
        colp1 = j + 1;
        tmp1 = substr(valc,indxval*Valwidth,Valwidth);
        tmp2 = substr(valc,(indxval+1)*Valwidth,Valwidth);
        fprintf(stdout,cformat,rowp1,colp1,tmp1,tmp2);
      }
  } else if ( Type[0] == 'R' ) {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        tmp1 = substr(valc,(i-1)*Valwidth,Valwidth);
        fprintf(stdout,rformat,rowp1,colp1,tmp1);
      }
  } else {
    /*  Loop through columns */
    for (j = 0; j < Ncol ; j++)
      for (i=colptr[j];i<colptr[j+1];i++)
      {
        rowp1 = rowind[i-1];
        colp1 = j + 1;
        fprintf(stdout,format,rowp1,colp1);
      }
  }
  return 0;
}