Graphviz 14.1.2~dev.20251213.2040
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 <stdio.h>
19#include <string.h>
20#include <stdlib.h>
21#include <ctype.h>
22#include <sparse/SparseMatrix.h>
23#include <util/prisize_t.h>
24#include <util/startswith.h>
25#include <util/strcasecmp.h>
26
27#include "mmio.h"
28
29int mm_read_banner(FILE * f, MM_typecode * matcode)
30{
31 char line[MM_MAX_LINE_LENGTH];
32 char banner[MM_MAX_TOKEN_LENGTH] = {0};
33 char mtx[MM_MAX_TOKEN_LENGTH] = {0};
34 char crd[MM_MAX_TOKEN_LENGTH] = {0};
35 char data_type[MM_MAX_TOKEN_LENGTH] = {0};
36 char storage_scheme[MM_MAX_TOKEN_LENGTH] = {0};
37
38 *matcode = (MM_typecode){0};
39
40 if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
41 return MM_PREMATURE_EOF;
42
43 // note: 63 == MM_MAX_TOKEN_LENGTH - 1
44 if (sscanf(line, "%63s %63s %63s %63s %63s", banner, mtx, crd, data_type,
45 storage_scheme) != 5)
46 return MM_PREMATURE_EOF;
47
48 /* check for banner */
49 if (!startswith(banner, MatrixMarketBanner))
50 return MM_NO_HEADER;
51
52 /* first field should be "mtx" */
53 if (strcasecmp(mtx, MM_MTX_STR) != 0)
55
56 // second field describes whether this is a sparse matrix (in coordinate
57 // storage) or a dense array
58
59 if (strcasecmp(crd, MM_SPARSE_STR) != 0)
61
62 /* third field */
63
64 if (strcasecmp(data_type, MM_REAL_STR) == 0)
65 matcode->type = MATRIX_TYPE_REAL;
66 else if (strcasecmp(data_type, MM_COMPLEX_STR) == 0)
67 matcode->type = MATRIX_TYPE_COMPLEX;
68 else if (strcasecmp(data_type, MM_PATTERN_STR) == 0)
69 matcode->type = MATRIX_TYPE_PATTERN;
70 else if (strcasecmp(data_type, MM_INT_STR) == 0)
71 matcode->type = MATRIX_TYPE_INTEGER;
72 else
74
75
76 /* fourth field */
77
78 if (strcasecmp(storage_scheme, MM_GENERAL_STR) == 0)
79 matcode->shape = MS_GENERAL;
80 else if (strcasecmp(storage_scheme, MM_SYMM_STR) == 0)
81 matcode->shape = MS_SYMMETRIC;
82 else if (strcasecmp(storage_scheme, MM_HERM_STR) == 0)
83 matcode->shape = MS_HERMITIAN;
84 else if (strcasecmp(storage_scheme, MM_SKEW_STR) == 0)
85 matcode->shape = MS_SKEW;
86 else
88
89
90 return 0;
91}
92
93int mm_read_mtx_crd_size(FILE * f, int *M, int *N, size_t *nz) {
94 char line[MM_MAX_LINE_LENGTH];
95 int num_items_read;
96
97 /* set return null parameter values, in case we exit with errors */
98 *M = *N = 0;
99 *nz = 0;
100
101 /* now continue scanning until you reach the end-of-comments */
102 do {
103 if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
104 return MM_PREMATURE_EOF;
105 } while (line[0] == '%');
106
107 /* line[] is either blank or has M,N, nz */
108 if (sscanf(line, "%d %d %" PRISIZE_T, M, N, nz) == 3)
109 return 0;
110
111 else
112 do {
113 num_items_read = fscanf(f, "%d %d %" PRISIZE_T, M, N, nz);
114 if (num_items_read == EOF)
115 return MM_PREMATURE_EOF;
116 }
117 while (num_items_read != 3);
118
119 return 0;
120}
@ MATRIX_TYPE_REAL
@ MATRIX_TYPE_PATTERN
@ MATRIX_TYPE_COMPLEX
@ MATRIX_TYPE_INTEGER
#define N(n)
Definition bcomps.c:58
node NULL
Definition grammar.y:181
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, size_t *nz)
Definition mmio.c:93
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition mmio.c:29
Matrix Market I/O API
@ MS_SYMMETRIC
Definition mmio.h:32
@ MS_SKEW
Definition mmio.h:32
@ MS_GENERAL
Definition mmio.h:32
@ MS_HERMITIAN
Definition mmio.h:32
#define MM_MTX_STR
Definition mmio.h:68
#define MM_PATTERN_STR
Definition mmio.h:78
#define MM_MAX_LINE_LENGTH
Definition mmio.h:28
#define MM_SKEW_STR
Definition mmio.h:77
#define MM_PREMATURE_EOF
Definition mmio.h:46
#define MM_GENERAL_STR
Definition mmio.h:74
#define MM_MAX_TOKEN_LENGTH
Definition mmio.h:30
#define MM_COMPLEX_STR
Definition mmio.h:71
#define MM_SPARSE_STR
Definition mmio.h:70
#define MM_UNSUPPORTED_TYPE
Definition mmio.h:49
#define MM_NO_HEADER
Definition mmio.h:48
#define MM_HERM_STR
Definition mmio.h:76
#define MM_SYMM_STR
Definition mmio.h:75
#define MM_REAL_STR
Definition mmio.h:72
#define MM_INT_STR
Definition mmio.h:73
#define MatrixMarketBanner
Definition mmio.h:29
#define PRISIZE_T
Definition prisize_t.h:25
#define M
Definition randomkit.c:90
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
matrix_shape_t shape
Definition mmio.h:36
int type
one of the MATRIX_TYPE_* values from lib/sparse
Definition mmio.h:35