int mm_read_banner(FILE *f, MM_typecode *matcode) { char line[MM_MAX_LINE_LENGTH]; char banner[MM_MAX_TOKEN_LENGTH]; char mtx[MM_MAX_TOKEN_LENGTH]; char crd[MM_MAX_TOKEN_LENGTH]; char data_type[MM_MAX_TOKEN_LENGTH]; char storage_scheme[MM_MAX_TOKEN_LENGTH]; char *p; mm_clear_typecode(matcode); if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) return MM_PREMATURE_EOF; if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, storage_scheme) != 5) return MM_PREMATURE_EOF; for (p=mtx; *p!='\0'; *p=tolower(*p),p++); /* convert to lower case */ for (p=crd; *p!='\0'; *p=tolower(*p),p++); for (p=data_type; *p!='\0'; *p=tolower(*p),p++); for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++); /* check for banner */ if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0) return MM_NO_HEADER; /* first field should be "mtx" */ if (strcmp(mtx, MM_MTX_STR) != 0) return MM_UNSUPPORTED_TYPE; mm_set_matrix(matcode); /* second field describes whether this is a sparse matrix (in coordinate storgae) or a dense array */ if (strcmp(crd, MM_SPARSE_STR) == 0) mm_set_sparse(matcode); else if (strcmp(crd, MM_DENSE_STR) == 0) mm_set_dense(matcode); else return MM_UNSUPPORTED_TYPE; /* third field */ if (strcmp(data_type, MM_REAL_STR) == 0) mm_set_real(matcode); else if (strcmp(data_type, MM_COMPLEX_STR) == 0) mm_set_complex(matcode); else if (strcmp(data_type, MM_PATTERN_STR) == 0) mm_set_pattern(matcode); else if (strcmp(data_type, MM_INT_STR) == 0) mm_set_integer(matcode); else return MM_UNSUPPORTED_TYPE; /* fourth field */ if (strcmp(storage_scheme, MM_GENERAL_STR) == 0) mm_set_general(matcode); else if (strcmp(storage_scheme, MM_SYMM_STR) == 0) mm_set_symmetric(matcode); else if (strcmp(storage_scheme, MM_HERM_STR) == 0) mm_set_hermitian(matcode); else if (strcmp(storage_scheme, MM_SKEW_STR) == 0) mm_set_skew(matcode); else return MM_UNSUPPORTED_TYPE; return 0; }
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; }
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; } } }
// MANDATORY int mm_read_banner ( FILE *f, MM_typecode *matcode ) /******************************************************************************/ /* Purpose: MM_READ_BANNER reads the header line of an MM file. Modified: 31 October 2008 15 Oct 2012 to replace int with long unsigned int Parameters: Input, FILE *F, a pointer to the input file. Output, MM_typecode *MATCODE, the header information. */ { char line[MM_MAX_LINE_LENGTH]; char banner[MM_MAX_TOKEN_LENGTH]; char mtx[MM_MAX_TOKEN_LENGTH]; char crd[MM_MAX_TOKEN_LENGTH]; char data_type[MM_MAX_TOKEN_LENGTH]; char storage_scheme[MM_MAX_TOKEN_LENGTH]; char *p; mm_clear_typecode(matcode); if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) return MM_PREMATURE_EOF; // banner: MatrixMarket // mtx: matrix or vector // format: coordinate or dense array // field : real or int of value // symmetry: general, symmetric, skew-symmetric ... if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, storage_scheme) != 5) return MM_PREMATURE_EOF; for (p=mtx; *p!='\0'; *p=tolower(*p),p++); /* convert to lower case */ for (p=crd; *p!='\0'; *p=tolower(*p),p++); for (p=data_type; *p!='\0'; *p=tolower(*p),p++); for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++); /* check for banner */ if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0) return MM_NO_HEADER; /* first field should be "mtx" */ if (strcmp(mtx, MM_MTX_STR) != 0) return MM_UNSUPPORTED_TYPE; mm_set_matrix(matcode); /* second field describes whether this is a sparse matrix (in coordinate storgae) or a dense array */ if (strcmp(crd, MM_SPARSE_STR) == 0) mm_set_sparse(matcode); else if (strcmp(crd, MM_DENSE_STR) == 0) mm_set_dense(matcode); else return MM_UNSUPPORTED_TYPE; /* third field */ if (strcmp(data_type, MM_REAL_STR) == 0) mm_set_real(matcode); else if (strcmp(data_type, MM_COMPLEX_STR) == 0) mm_set_complex(matcode); else if (strcmp(data_type, MM_PATTERN_STR) == 0) mm_set_pattern(matcode); else if (strcmp(data_type, MM_INT_STR) == 0) mm_set_integer(matcode); else return MM_UNSUPPORTED_TYPE; /* fourth field */ if (strcmp(storage_scheme, MM_GENERAL_STR) == 0) mm_set_general(matcode); else if (strcmp(storage_scheme, MM_SYMM_STR) == 0) mm_set_symmetric(matcode); else if (strcmp(storage_scheme, MM_HERM_STR) == 0) mm_set_hermitian(matcode); else if (strcmp(storage_scheme, MM_SKEW_STR) == 0) mm_set_skew(matcode); else return MM_UNSUPPORTED_TYPE; return 0; }