Graphviz 14.1.2~dev.20260123.1158
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 "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
31int 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)
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)
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
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
90
91
92 return 0;
93}
94
95int 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}
@ 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:95
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition mmio.c:31
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:92
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