int mm_read_mtx_array_size(ZOLTAN_FILE* f, int *M, int *N) { char line[MM_MAX_LINE_LENGTH]; int num_items_read; /* set return null parameter values, in case we exit with errors */ *M = *N = 0; /* now continue scanning until you reach the end-of-comments */ do { if (ZOLTAN_FILE_gets(line,MM_MAX_LINE_LENGTH,f) == NULL) return MM_PREMATURE_EOF; }while (line[0] == '%'); /* line[] is either blank or has M,N, nz */ if (sscanf(line, "%d %d", M, N) == 2) return 0; else /* we have a blank line */ do { num_items_read = ZOLTAN_FILE_scanf (f, "%d %d", M, N); if (num_items_read == EOF) return MM_PREMATURE_EOF; } while (num_items_read != 2); return 0; }
int mm_read_mtx_crd_size(ZOLTAN_FILE* f, int *M, int *N, int *nz ) { char line[MM_MAX_LINE_LENGTH], *c; int num_items_read; /* set return null parameter values, in case we exit with errors */ *M = *N = *nz = 0; /* now continue scanning until you reach the end-of-comments */ do { if (ZOLTAN_FILE_gets(line,MM_MAX_LINE_LENGTH,f) == NULL) return MM_PREMATURE_EOF; c = line; while (*c && (*c != '\n') && isspace(*c)) c++; }while (*c == '%'); /* line[] is either blank or has M,N, nz */ if (sscanf(line, "%d %d %d", M, N, nz) == 3) return 0; else do { num_items_read = ZOLTAN_FILE_scanf(f, "%d %d %d", M, N, nz); if (num_items_read == EOF) return MM_PREMATURE_EOF; } while (num_items_read != 3); return 0; }
/*** Implemented as a macro as "vfscanf" or "vsscanf" are C99 only ***/ int ZOLTAN_FILE_scanf (ZOLTAN_FILE* stream, const char * format, ... ) { int retval = 0; va_list argp; va_start(argp, format); if (stream->type == STANDARD) { retval = (vfscanf(stream->strm.fileunc, format, argp)); } else { char buff[1024]; if (ZOLTAN_FILE_gets(buff, 1024, (stream)) != NULL) retval = vsscanf(buff, format, argp); } va_end(argp); return (retval); }
double read_val( ZOLTAN_FILE* infile, /* file to read value from */ int *end_flag /* 0 => OK, 1 => EOL, -1 => EOF */ ) { double val; /* return value */ char *ptr; /* ptr to next string to read */ char *ptr2; /* ptr to next string to read */ int length; /* length of line to read */ int length_left; /* length of line still around */ int white_seen; /* have I detected white space yet? */ int done; /* checking for end of scan */ int i; /* loop counter */ *end_flag = 0; if (offset == 0 || offset >= break_pnt) { if (offset >= break_pnt) { /* Copy rest of line back to beginning. */ length_left = LINE_LENGTH - save_pnt - 1; ptr2 = line; ptr = &line[save_pnt]; for (i=length_left; i; i--) *ptr2++ = *ptr++; length = save_pnt + 1; } else { length = LINE_LENGTH; length_left = 0; } /* Now read next line, or next segment of current one. */ ptr2 = ZOLTAN_FILE_gets(&line[length_left], length, infile); if (ptr2 == (char *) NULL) { /* We've hit end of file. */ *end_flag = -1; return((double) 0.0); } if ((line[LINE_LENGTH - 2] != '\n') && (line[LINE_LENGTH - 2] != '\f') && (strlen(line) == LINE_LENGTH - 1)){ /* Line too long. Find last safe place in line. */ break_pnt = LINE_LENGTH - 1; save_pnt = break_pnt; white_seen = FALSE; done = FALSE; while (!done) { --break_pnt; if (line[break_pnt] != '\0') { if (isspace((int)(line[break_pnt]))) { if (!white_seen) { save_pnt = break_pnt + 1; white_seen = TRUE; } } else if (white_seen) { done= TRUE; } } } } else { break_pnt = LINE_LENGTH; } offset = 0; } while (isspace((int)(line[offset])) && offset < LINE_LENGTH) offset++; if (line[offset] == '%' || line[offset] == '#') { *end_flag = 1; if (break_pnt < LINE_LENGTH) { flush_line(infile); } return((double) 0.0); } ptr = &(line[offset]); val = strtod(ptr, &ptr2); if (ptr2 == ptr) { /* End of input line. */ offset = 0; *end_flag = 1; return((double) 0.0); } else { offset = (int) (ptr2 - line) / sizeof(char); } return(val); }
int mm_read_banner(ZOLTAN_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 (ZOLTAN_FILE_gets(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; /* convert to lower case */ for (p=mtx; *p!='\0'; *p= (char) tolower(*p),p++); for (p=crd; *p!='\0'; *p= (char) tolower(*p),p++); for (p=data_type; *p!='\0'; *p= (char) tolower(*p),p++); for (p=storage_scheme; *p!='\0'; *p= (char) tolower(*p),p++); /* check for banner */ if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0) return MM_NO_HEADER; /* first field should be "matrix" */ 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; }