Graphviz 14.1.3~dev.20260303.0214
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, 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)
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)
60
61 /* third field */
62
63 if (strcasecmp(data_type, MM_REAL_STR) != 0)
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
79
80
81 return 0;
82}
83
84int mm_read_mtx_crd_size(FILE * f, int *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 = *N = 0;
90 *nz = 0;
91
92 /* now continue scanning until you reach the end-of-comments */
93 do {
94 if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
95 return MM_PREMATURE_EOF;
96 } while (line[0] == '%');
97
98 /* line[] is either blank or has M,N, nz */
99 if (sscanf(line, "%d %d %" PRISIZE_T, M, N, nz) == 3)
100 return 0;
101
102 else
103 do {
104 num_items_read = fscanf(f, "%d %d %" PRISIZE_T, M, N, nz);
105 if (num_items_read == EOF)
106 return MM_PREMATURE_EOF;
107 }
108 while (num_items_read != 3);
109
110 return 0;
111}
#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:84
int mm_read_banner(FILE *f, matrix_shape_t *shape)
Definition mmio.c:31
Matrix Market I/O API
matrix_shape_t
Definition mmio.h:32
@ 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:63
#define MM_MAX_LINE_LENGTH
Definition mmio.h:28
#define MM_SKEW_STR
Definition mmio.h:70
#define MM_PREMATURE_EOF
Definition mmio.h:41
#define MM_GENERAL_STR
Definition mmio.h:67
#define MM_MAX_TOKEN_LENGTH
Definition mmio.h:30
#define MM_SPARSE_STR
Definition mmio.h:65
#define MM_UNSUPPORTED_TYPE
Definition mmio.h:44
#define MM_NO_HEADER
Definition mmio.h:43
#define MM_HERM_STR
Definition mmio.h:69
#define MM_SYMM_STR
Definition mmio.h:68
#define MM_REAL_STR
Definition mmio.h:66
#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