| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
/* |
| 11 |
* Matrix Market I/O library for ANSI C |
| 12 |
* |
| 13 |
* See http://math.nist.gov/MatrixMarket for details. |
| 14 |
* |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <ctype.h> |
| 24 |
#include <sparse/SparseMatrix.h> |
| 25 |
#include <util/prisize_t.h> |
| 26 |
#include <util/startswith.h> |
| 27 |
#include <util/strcasecmp.h> |
| 28 |
|
| 29 |
#include "mmio.h" |
| 30 |
|
| 31 |
int mm_read_banner(FILE * f, MM_typecode * matcode) |
| 32 |
{ |
| 33 |
char line[MM_MAX_LINE_LENGTH]; |
| 34 |
char banner[MM_MAX_TOKEN_LENGTH] = {0}; |
| 35 |
char mtx[MM_MAX_TOKEN_LENGTH] = {0}; |
| 36 |
char crd[MM_MAX_TOKEN_LENGTH] = {0}; |
| 37 |
char data_type[MM_MAX_TOKEN_LENGTH] = {0}; |
| 38 |
char storage_scheme[MM_MAX_TOKEN_LENGTH] = {0}; |
| 39 |
|
| 40 |
*matcode = (MM_typecode){0}; |
| 41 |
|
| 42 |
if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) |
| 43 |
return MM_PREMATURE_EOF; |
| 44 |
|
| 45 |
// note: 63 == MM_MAX_TOKEN_LENGTH - 1 |
| 46 |
if (sscanf(line, "%63s %63s %63s %63s %63s", banner, mtx, crd, data_type, |
| 47 |
storage_scheme) != 5) |
| 48 |
return MM_PREMATURE_EOF; |
| 49 |
|
| 50 |
/* check for banner */ |
| 51 |
if (!startswith(banner, MatrixMarketBanner)) |
| 52 |
return MM_NO_HEADER; |
| 53 |
|
| 54 |
/* first field should be "mtx" */ |
| 55 |
if (strcasecmp(mtx, MM_MTX_STR) != 0) |
| 56 |
return MM_UNSUPPORTED_TYPE; |
| 57 |
|
| 58 |
// second field describes whether this is a sparse matrix (in coordinate |
| 59 |
// storage) or a dense array |
| 60 |
|
| 61 |
if (strcasecmp(crd, MM_SPARSE_STR) != 0) |
| 62 |
return MM_UNSUPPORTED_TYPE; |
| 63 |
|
| 64 |
/* third field */ |
| 65 |
|
| 66 |
if (strcasecmp(data_type, MM_REAL_STR) == 0) |
| 67 |
matcode->type = MATRIX_TYPE_REAL; |
| 68 |
else if (strcasecmp(data_type, MM_COMPLEX_STR) == 0) |
| 69 |
matcode->type = MATRIX_TYPE_COMPLEX; |
| 70 |
else if (strcasecmp(data_type, MM_PATTERN_STR) == 0) |
| 71 |
matcode->type = MATRIX_TYPE_PATTERN; |
| 72 |
else if (strcasecmp(data_type, MM_INT_STR) == 0) |
| 73 |
matcode->type = MATRIX_TYPE_INTEGER; |
| 74 |
else |
| 75 |
return MM_UNSUPPORTED_TYPE; |
| 76 |
|
| 77 |
|
| 78 |
/* fourth field */ |
| 79 |
|
| 80 |
if (strcasecmp(storage_scheme, MM_GENERAL_STR) == 0) |
| 81 |
matcode->shape = MS_GENERAL; |
| 82 |
else if (strcasecmp(storage_scheme, MM_SYMM_STR) == 0) |
| 83 |
matcode->shape = MS_SYMMETRIC; |
| 84 |
else if (strcasecmp(storage_scheme, MM_HERM_STR) == 0) |
| 85 |
matcode->shape = MS_HERMITIAN; |
| 86 |
else if (strcasecmp(storage_scheme, MM_SKEW_STR) == 0) |
| 87 |
matcode->shape = MS_SKEW; |
| 88 |
else |
| 89 |
return MM_UNSUPPORTED_TYPE; |
| 90 |
|
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
int mm_read_mtx_crd_size(FILE * f, int *M, int *N, size_t *nz) { |
| 96 |
char line[MM_MAX_LINE_LENGTH]; |
| 97 |
int num_items_read; |
| 98 |
|
| 99 |
/* set return null parameter values, in case we exit with errors */ |
| 100 |
*M = *N = 0; |
| 101 |
*nz = 0; |
| 102 |
|
| 103 |
/* now continue scanning until you reach the end-of-comments */ |
| 104 |
do { |
| 105 |
if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) |
| 106 |
return MM_PREMATURE_EOF; |
| 107 |
} while (line[0] == '%'); |
| 108 |
|
| 109 |
/* line[] is either blank or has M,N, nz */ |
| 110 |
if (sscanf(line, "%d %d %" PRISIZE_T, M, N, nz) == 3) |
| 111 |
return 0; |
| 112 |
|
| 113 |
else |
| 114 |
do { |
| 115 |
num_items_read = fscanf(f, "%d %d %" PRISIZE_T, M, N, nz); |
| 116 |
if (num_items_read == EOF) |
| 117 |
return MM_PREMATURE_EOF; |
| 118 |
} |
| 119 |
while (num_items_read != 3); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |