Graphviz 12.0.1~dev.20240716.0800
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 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 <cgraph/startswith.h>
19#include <cgraph/strcasecmp.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <assert.h>
25
26#include "mmio.h"
27
28int mm_read_banner(FILE * f, MM_typecode * matcode)
29{
30 char line[MM_MAX_LINE_LENGTH];
31 char banner[MM_MAX_TOKEN_LENGTH] = {0};
32 char mtx[MM_MAX_TOKEN_LENGTH] = {0};
33 char crd[MM_MAX_TOKEN_LENGTH] = {0};
34 char data_type[MM_MAX_TOKEN_LENGTH] = {0};
35 char storage_scheme[MM_MAX_TOKEN_LENGTH] = {0};
36
37 mm_clear_typecode(matcode);
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 "mtx" */
52 if (strcasecmp(mtx, MM_MTX_STR) != 0)
54 mm_set_matrix(matcode);
55
56
57 /* second field describes whether this is a sparse matrix (in coordinate
58 storgae) or a dense array */
59
60
61 if (strcasecmp(crd, MM_SPARSE_STR) == 0)
62 mm_set_sparse(matcode);
63 else if (strcasecmp(crd, MM_DENSE_STR) == 0)
64 mm_set_dense(matcode);
65 else
67
68
69 /* third field */
70
71 if (strcasecmp(data_type, MM_REAL_STR) == 0)
72 mm_set_real(matcode);
73 else if (strcasecmp(data_type, MM_COMPLEX_STR) == 0)
74 mm_set_complex(matcode);
75 else if (strcasecmp(data_type, MM_PATTERN_STR) == 0)
76 mm_set_pattern(matcode);
77 else if (strcasecmp(data_type, MM_INT_STR) == 0)
78 mm_set_integer(matcode);
79 else
81
82
83 /* fourth field */
84
85 if (strcasecmp(storage_scheme, MM_GENERAL_STR) == 0)
86 mm_set_general(matcode);
87 else if (strcasecmp(storage_scheme, MM_SYMM_STR) == 0)
88 mm_set_symmetric(matcode);
89 else if (strcasecmp(storage_scheme, MM_HERM_STR) == 0)
90 mm_set_hermitian(matcode);
91 else if (strcasecmp(storage_scheme, MM_SKEW_STR) == 0)
92 mm_set_skew(matcode);
93 else
95
96
97 return 0;
98}
99
100int mm_read_mtx_crd_size(FILE * f, int *M, int *N, int *nz)
101{
102 char line[MM_MAX_LINE_LENGTH];
103 int num_items_read;
104
105 /* set return null parameter values, in case we exit with errors */
106 *M = *N = *nz = 0;
107
108 /* now continue scanning until you reach the end-of-comments */
109 do {
110 if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
111 return MM_PREMATURE_EOF;
112 } while (line[0] == '%');
113
114 /* line[] is either blank or has M,N, nz */
115 if (sscanf(line, "%d %d %d", M, N, nz) == 3)
116 return 0;
117
118 else
119 do {
120 num_items_read = fscanf(f, "%d %d %d", M, N, nz);
121 if (num_items_read == EOF)
122 return MM_PREMATURE_EOF;
123 }
124 while (num_items_read != 3);
125
126 return 0;
127}
#define N(n)
Definition bcomps.c:58
node NULL
Definition grammar.y:149
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
Definition mmio.c:100
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition mmio.c:28
Matrix Market I/O API
#define MM_MTX_STR
Definition mmio.h:106
#define mm_set_complex(typecode)
Definition mmio.h:61
#define mm_set_hermitian(typecode)
Definition mmio.h:70
#define MM_PATTERN_STR
Definition mmio.h:118
#define mm_set_general(typecode)
Definition mmio.h:68
char MM_typecode[4]
Definition mmio.h:29
#define MM_MAX_LINE_LENGTH
Definition mmio.h:25
#define mm_set_skew(typecode)
Definition mmio.h:69
#define MM_SKEW_STR
Definition mmio.h:117
#define MM_PREMATURE_EOF
Definition mmio.h:82
#define mm_set_sparse(typecode)
Definition mmio.h:59
#define mm_clear_typecode(typecode)
Definition mmio.h:72
#define MM_GENERAL_STR
Definition mmio.h:114
#define MM_MAX_TOKEN_LENGTH
Definition mmio.h:27
#define MM_COMPLEX_STR
Definition mmio.h:111
#define mm_set_symmetric(typecode)
Definition mmio.h:67
#define MM_SPARSE_STR
Definition mmio.h:110
#define MM_UNSUPPORTED_TYPE
Definition mmio.h:85
#define mm_set_integer(typecode)
Definition mmio.h:64
#define MM_NO_HEADER
Definition mmio.h:84
#define MM_HERM_STR
Definition mmio.h:116
#define mm_set_matrix(typecode)
Definition mmio.h:55
#define mm_set_dense(typecode)
Definition mmio.h:58
#define MM_SYMM_STR
Definition mmio.h:115
#define mm_set_pattern(typecode)
Definition mmio.h:63
#define MM_DENSE_STR
Definition mmio.h:108
#define MM_REAL_STR
Definition mmio.h:112
#define MM_INT_STR
Definition mmio.h:113
#define mm_set_real(typecode)
Definition mmio.h:62
#define MatrixMarketBanner
Definition mmio.h:26
#define M
Definition randomkit.c:98
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
Definition startswith.h:11
platform abstraction for case-insensitive string functions