-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsenumbers.c
149 lines (137 loc) · 3.07 KB
/
parsenumbers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef _PARSENUMBERS_C
#define _PARSENUMBERS_C
#include <string.h>
#include "xmalloc.c"
// utility function: parse floats from a text file
// returns a pointer to a malloc'ed array of the parsed floats
// fills *no with the number of floats
static float *read_ascii_floats(FILE *f, int *no)
{
int r, n = 0, nt = 0;
float *t = NULL;
while(1) {
if (n >= nt)
{
nt = 2 * (nt + 1);
t = xrealloc(t, nt * sizeof * t);
}
r = fscanf(f, "%f", t+n);
if (r != 1)
break;
n += 1;
}
*no = n;
return t;
}
// utility function: parse doubles from a text file
// returns a pointer to a malloc'ed array of the parsed doubles
// fills *no with the number of doubles
static double *read_ascii_doubles(FILE *f, int *no)
{
int r, n = 0, nt = 0;
double *t = NULL;
while(1) {
if (n >= nt)
{
nt = 2 * (nt + 1);
t = xrealloc(t, nt * sizeof * t);
}
r = fscanf(f, "%lf", t+n);
if (r != 1)
break;
n += 1;
}
*no = n;
return t;
}
//// utility function: parse a matrix of doubles from a text file
//// returns a pointer to a malloc'ed array of the parsed doubles
//// fills *nc with the number of cols and *nr with the number of rows
//static double *read_ascii_matrix_doubles(FILE *f, int *nc, int *nl)
//{
// int r, n = 0, nt = 0;
// double *t = NULL;
// while(1) {
// if (n >= nt)
// {
// nt = 2 * (nt + 1);
// t = xrealloc(t, nt * sizeof * t);
// }
// r = fscanf(f, "%lf", t+n);
// if (r != 1)
// break;
// n += 1;
// }
// *no = n;
// return t;
//}
static int parse_doubles(double *t, int nmax, const char *s)
{
int i = 0, w;
while (i < nmax && 1 == sscanf(s, "%lg %n", t + i, &w)) {
i += 1;
s += w;
}
return i;
}
static int parse_floats(float *t, int nmax, const char *s)
{
int i = 0, w;
while (i < nmax && 1 == sscanf(s, "%g %n", t + i, &w)) {
i += 1;
s += w;
}
return i;
}
static float *alloc_parse_floats2(int nmax, const char *s, int *n)
{
if (!nmax) nmax = strlen(s);
float *r = xmalloc(nmax*sizeof*r);
s += strspn(s, "[] \n\t,:;()");
int i = 0, w;
while (i < nmax && 1 == sscanf(s, "%g%*[][ \n\t,:;]%n", r + i, &w))
{
i += 1;
s += w;
}
*n = i;
return r;
}
static float *alloc_parse_floats(int nmax, const char *ss, int *n)
{
// add a space to s, so that gabriele's expression works as intended
int ns = strlen(ss);
char t[2+ns], *s = t;
s[0] = ' ';
for (int i = 0; i <= ns; i++)
s[i+1] = ss[i];
float *r = xmalloc(nmax * sizeof*r);
int i = 0, w;
//while (i < nmax && 1 == sscanf(s, "%g %n", r + i, &w)) {
while (i < nmax && 1 == sscanf(s, "%*[][ \n\t,:;]%g%n", r + i, &w)) {
//fprintf(stderr, "read %g\n", r[i]);
i += 1;
s += w;
}
//fprintf(stderr, "finished, got i=%d\n", i);
*n = i;
return r;
}
static double *alloc_parse_doubles(int nmax, const char *ss, int *n)
{
// add a space to s, so that gabriele's expression works as intended
int ns = strlen(ss);
char t[2+ns], *s = t;
s[0] = ' ';
for (int i = 0; i <= ns; i++)
s[i+1] = ss[i];
double *r = xmalloc(nmax * sizeof*r);
int i = 0, w;
while (i < nmax && 1 == sscanf(s, "%*[][ \n\t,:;]%lf%n", r + i, &w)) {
i += 1;
s += w;
}
*n = i;
return r;
}
#endif//_PARSENUMBERS_C