Graphviz 13.0.0~dev.20241220.2304
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/startswith.h>
24#include <util/strcasecmp.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 *matcode = (MM_typecode){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 "mtx" */
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)
64 matcode->type = MATRIX_TYPE_REAL;
65 else if (strcasecmp(data_type, MM_COMPLEX_STR) == 0)
66 matcode->type = MATRIX_TYPE_COMPLEX;
67 else if (strcasecmp(data_type, MM_PATTERN_STR) == 0)
68 matcode->type = MATRIX_TYPE_PATTERN;
69 else if (strcasecmp(data_type, MM_INT_STR) == 0)
70 matcode->type = MATRIX_TYPE_INTEGER;
71 else
73
74
75 /* fourth field */
76
77 if (strcasecmp(storage_scheme, MM_GENERAL_STR) == 0)
78 matcode->shape = MS_GENERAL;
79 else if (strcasecmp(storage_scheme, MM_SYMM_STR) == 0)
80 matcode->shape = MS_SYMMETRIC;
81 else if (strcasecmp(storage_scheme, MM_HERM_STR) == 0)
82 matcode->shape = MS_HERMITIAN;
83 else if (strcasecmp(storage_scheme, MM_SKEW_STR) == 0)
84 matcode->shape = MS_SKEW;
85 else
87
88
89 return 0;
90}
91
92int mm_read_mtx_crd_size(FILE * f, int *M, int *N, int *nz)
93{
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 = *nz = 0;
99
100 /* now continue scanning until you reach the end-of-comments */
101 do {
102 if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
103 return MM_PREMATURE_EOF;
104 } while (line[0] == '%');
105
106 /* line[] is either blank or has M,N, nz */
107 if (sscanf(line, "%d %d %d", M, N, nz) == 3)
108 return 0;
109
110 else
111 do {
112 num_items_read = fscanf(f, "%d %d %d", M, N, nz);
113 if (num_items_read == EOF)
114 return MM_PREMATURE_EOF;
115 }
116 while (num_items_read != 3);
117
118 return 0;
119}
@ MATRIX_TYPE_REAL
@ MATRIX_TYPE_PATTERN
@ MATRIX_TYPE_COMPLEX
@ MATRIX_TYPE_INTEGER
#define N(n)
Definition bcomps.c:58
node NULL
Definition grammar.y:163
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
Definition mmio.c:92
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition mmio.c:28
Matrix Market I/O API
@ MS_SYMMETRIC
Definition mmio.h:29
@ MS_SKEW
Definition mmio.h:29
@ MS_GENERAL
Definition mmio.h:29
@ MS_HERMITIAN
Definition mmio.h:29
#define MM_MTX_STR
Definition mmio.h:65
#define MM_PATTERN_STR
Definition mmio.h:75
#define MM_MAX_LINE_LENGTH
Definition mmio.h:25
#define MM_SKEW_STR
Definition mmio.h:74
#define MM_PREMATURE_EOF
Definition mmio.h:43
#define MM_GENERAL_STR
Definition mmio.h:71
#define MM_MAX_TOKEN_LENGTH
Definition mmio.h:27
#define MM_COMPLEX_STR
Definition mmio.h:68
#define MM_SPARSE_STR
Definition mmio.h:67
#define MM_UNSUPPORTED_TYPE
Definition mmio.h:46
#define MM_NO_HEADER
Definition mmio.h:45
#define MM_HERM_STR
Definition mmio.h:73
#define MM_SYMM_STR
Definition mmio.h:72
#define MM_REAL_STR
Definition mmio.h:69
#define MM_INT_STR
Definition mmio.h:70
#define MatrixMarketBanner
Definition mmio.h:26
#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:33
int type
one of the MATRIX_TYPE_* values from lib/sparse
Definition mmio.h:32