Graphviz
15.1.1~dev.20260630.1303
Loading...
Searching...
No Matches
mmio.c
Go to the documentation of this file.
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 v2.0
5
* which accompanies this distribution, and is available at
6
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.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,
matrix_shape_t
*shape) {
32
char
line[
MM_MAX_LINE_LENGTH
];
33
char
banner[
MM_MAX_TOKEN_LENGTH
] = {0};
34
char
mtx[
MM_MAX_TOKEN_LENGTH
] = {0};
35
char
crd[
MM_MAX_TOKEN_LENGTH
] = {0};
36
char
data_type[
MM_MAX_TOKEN_LENGTH
] = {0};
37
char
storage_scheme[
MM_MAX_TOKEN_LENGTH
] = {0};
38
39
if
(fgets(line,
MM_MAX_LINE_LENGTH
, f) ==
NULL
)
40
return
MM_PREMATURE_EOF
;
41
42
// note: 63 == MM_MAX_TOKEN_LENGTH - 1
43
if
(sscanf(line,
"%63s %63s %63s %63s %63s"
, banner, mtx, crd, data_type,
44
storage_scheme) != 5)
45
return
MM_PREMATURE_EOF
;
46
47
/* check for banner */
48
if
(!
startswith
(banner,
MatrixMarketBanner
))
49
return
MM_NO_HEADER
;
50
51
// first field should be “matrix”
52
if
(strcasecmp(mtx,
MM_MTX_STR
) != 0)
53
return
MM_UNSUPPORTED_TYPE
;
54
55
// second field describes whether this is a sparse matrix (in coordinate
56
// storage) or a dense array
57
58
if
(strcasecmp(crd,
MM_SPARSE_STR
) != 0)
59
return
MM_UNSUPPORTED_TYPE
;
60
61
/* third field */
62
63
if
(strcasecmp(data_type,
MM_REAL_STR
) != 0)
64
return
MM_UNSUPPORTED_TYPE
;
65
66
67
/* fourth field */
68
69
if
(strcasecmp(storage_scheme,
MM_GENERAL_STR
) == 0)
70
*shape =
MS_GENERAL
;
71
else
if
(strcasecmp(storage_scheme,
MM_SYMM_STR
) == 0)
72
*shape =
MS_SYMMETRIC
;
73
else
if
(strcasecmp(storage_scheme,
MM_HERM_STR
) == 0)
74
*shape =
MS_HERMITIAN
;
75
else
if
(strcasecmp(storage_scheme,
MM_SKEW_STR
) == 0)
76
*shape =
MS_SKEW
;
77
else
78
return
MM_UNSUPPORTED_TYPE
;
79
80
81
return
0;
82
}
83
84
int
mm_read_mtx_crd_size
(FILE *f,
size_t
*
M
,
int
*
N
,
size_t
*nz) {
85
char
line[
MM_MAX_LINE_LENGTH
];
86
int
num_items_read;
87
88
/* set return null parameter values, in case we exit with errors */
89
*
M
= 0;
90
*
N
= 0;
91
*nz = 0;
92
93
/* now continue scanning until you reach the end-of-comments */
94
do
{
95
if
(fgets(line,
MM_MAX_LINE_LENGTH
, f) ==
NULL
)
96
return
MM_PREMATURE_EOF
;
97
}
while
(line[0] ==
'%'
);
98
99
/* line[] is either blank or has M,N, nz */
100
if
(sscanf(line,
"%"
PRISIZE_T
" %d %"
PRISIZE_T
,
M
,
N
, nz) == 3)
101
return
0;
102
103
else
104
do
{
105
num_items_read = fscanf(f,
"%"
PRISIZE_T
" %d %"
PRISIZE_T
,
M
,
N
, nz);
106
if
(num_items_read == EOF)
107
return
MM_PREMATURE_EOF
;
108
}
109
while
(num_items_read != 3);
110
111
return
0;
112
}
SparseMatrix.h
N
#define N(n)
Definition
bcomps.c:58
NULL
node NULL
Definition
grammar.y:181
mm_read_mtx_crd_size
int mm_read_mtx_crd_size(FILE *f, size_t *M, int *N, size_t *nz)
Definition
mmio.c:84
mm_read_banner
int mm_read_banner(FILE *f, matrix_shape_t *shape)
Definition
mmio.c:31
mmio.h
Matrix Market I/O API
matrix_shape_t
matrix_shape_t
Definition
mmio.h:32
MS_SYMMETRIC
@ MS_SYMMETRIC
Definition
mmio.h:32
MS_SKEW
@ MS_SKEW
Definition
mmio.h:32
MS_GENERAL
@ MS_GENERAL
Definition
mmio.h:32
MS_HERMITIAN
@ MS_HERMITIAN
Definition
mmio.h:32
MM_MTX_STR
#define MM_MTX_STR
Definition
mmio.h:63
MM_MAX_LINE_LENGTH
#define MM_MAX_LINE_LENGTH
Definition
mmio.h:28
MM_SKEW_STR
#define MM_SKEW_STR
Definition
mmio.h:70
MM_PREMATURE_EOF
#define MM_PREMATURE_EOF
Definition
mmio.h:41
MM_GENERAL_STR
#define MM_GENERAL_STR
Definition
mmio.h:67
MM_MAX_TOKEN_LENGTH
#define MM_MAX_TOKEN_LENGTH
Definition
mmio.h:30
MM_SPARSE_STR
#define MM_SPARSE_STR
Definition
mmio.h:65
MM_UNSUPPORTED_TYPE
#define MM_UNSUPPORTED_TYPE
Definition
mmio.h:44
MM_NO_HEADER
#define MM_NO_HEADER
Definition
mmio.h:43
MM_HERM_STR
#define MM_HERM_STR
Definition
mmio.h:69
MM_SYMM_STR
#define MM_SYMM_STR
Definition
mmio.h:68
MM_REAL_STR
#define MM_REAL_STR
Definition
mmio.h:66
MatrixMarketBanner
#define MatrixMarketBanner
Definition
mmio.h:29
prisize_t.h
PRISIZE_T
#define PRISIZE_T
Definition
prisize_t.h:25
M
#define M
Definition
randomkit.c:92
startswith.h
startswith
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
Definition
startswith.h:11
strcasecmp.h
platform abstraction for case-insensitive string functions
cmd
tools
mmio.c
Generated by
1.9.8