-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
222 lines (186 loc) · 6.17 KB
/
parse.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Definition of general parsing methods. Uses linked list structure to read
* each row of data in a file. Parses numbers written in scientific notn.
*
* Author: Anthony Atkinson
* Date: 2021-09-30
*/
#include <math.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAXLINE 1 << 16
typedef struct AList {
double mcol;
double temp;
struct AList *tail;
} *LList;
static void substring(char *s, char *buf, int b, int e);
double parsenum(char *exp);
static LList create_list(double mcol, double temp, LList tail);
static LList insert_list(LList head, double mcol, double temp);
static void delete_list(LList list);
static void print_list(LList list);
int parsekurucz(char *f, double **arrayptr);
/**
* Determine the substring of a string from an initial position to final
* position in the target string. The result is stored in 'buf'.
*
* s target string
* buf destination buffer for substring
* b beginning position/byte offset of substring in target string
* e ending position/byte offset of substring in target string
*/
static void substring(char *s, char *buf, int b, int e) {
int i;
char c, *ptr;
for(i = 0; b + i < e; i++) {
c = *(s + b + i * sizeof(char));
ptr = buf + i;
*ptr = c;
}
buf[i] = '\0';
}
/**
* Parse a string in decimal format or scientific notation for its double
* representation. Returns '0.0' for a string that does no match.
*
* In retrospect, I think using scanf() with a %le argument would have sufficed
*
* exp string containing numeric expression
*/
double parsenum(char *exp) {
double val;
char *buf1, *buf2;
regex_t re;
regmatch_t match[4];
regcomp(&re, "^(-?[0-9]+[.]?[0-9]*)(e(-*[0-9]+))?$", REG_EXTENDED);
if (regexec(&re, exp, 4, match, 0) == REG_NOMATCH) {
// No match for the regex exp: return 0.0
printf("value does not match this pattern:\n" \
"^([0-9]+[.]?[0-9]*)(e(-*[0-9]+))?$\n");
regfree(&re);
return 0.;
} else {
// The string matches the regex exp: derive decimal value from subexps
// Obtain coefficient from the scietific notation representation
buf1 = (char *) malloc(match[1].rm_eo - match[1].rm_so);
substring(exp, buf1, match[1].rm_so, match[1].rm_eo);
// Obtain exponent from the scientific notation representation if exists
if (match[2].rm_eo > match[2].rm_so) {
buf2 = (char *) malloc(match[3].rm_eo - match[3].rm_so);
substring(exp, buf2, match[3].rm_so, match[3].rm_eo);
} else {
buf2 = (char *) malloc(2);
strcpy(buf2, "0\0");
}
// Calculate value from coefficient and exponent fields
val = atof(buf1) * pow(10.0, (double) atof(buf2));
free(buf1);
free(buf2);
}
regfree(&re);
return val;
}
static LList create_list(double mcol, double temp, LList tail) {
LList head = malloc(sizeof(struct AList));
head->mcol = mcol;
head->temp = temp;
head->tail = tail;
return head;
}
/**
* Inserts list at the head of the input list. returns the current jead of the list.
*/
static LList insert_list(LList head, double mcol, double temp) {
return create_list(mcol, temp, head);
}
/**
* Deletes the entire list from the provided node onwards. Tail recursion.
*/
static void delete_list(LList list) {
if (list == NULL)
return;
if (list->tail != NULL)
delete_list(list->tail);
free(list);
}
/**
* Print the data of the linked list starting with list as the head.
*/
static void print_list(LList list) {
LList p = list;
int count = 0;
while(p != NULL) {
printf("(%lf, %lf) -> ",p->mcol,p->temp);
p = p->tail;
if(++count%5 == 0) {
printf("\n");
}
}
printf("|\n");
fflush(stdout);
}
/**
* Parses kurucz file for data. Returns a double array of values, the first
* index for the row and the second for the column (0: mass col, 1: temp).
*
* f - string name for file being parsed
*/
int parsekurucz(char *f, double **arrayptr) {
regex_t regp;
regmatch_t regm[3];
int nrows, i;
char line[MAXLINE], *buf1, *buf2;
double mcoli, tempi, *p;
LList head;
FILE *file = fopen(f, "r");
if (file == NULL) {
printf("cannot open %s\n",f);
}
regcomp(®p, "[[:space:]]*([0-9.]+)[[:space:]]+([0-9.]+)", REG_EXTENDED);
nrows = 0;
head = NULL;
// Iterate through each line in the file
while(fgets(line, MAXLINE, file) != NULL) {
// Determine if the line has data in the desired representation
if(!regexec(®p, line, 3, regm, 0)) {
nrows += 1;
// Pare the numeric data substrings from the line string
buf1 = (char *) malloc(regm[1].rm_eo - regm[1].rm_so);
buf2 = (char *) malloc(regm[2].rm_eo - regm[2].rm_so);
substring(line, buf1, regm[1].rm_so, regm[1].rm_eo);
substring(line, buf2, regm[2].rm_so, regm[2].rm_eo);
// Convert the substrings into floating-point numbers
mcoli = parsenum(buf1);
tempi = parsenum(buf2);
// Append the data into a linked list, aggregating the file's data
head = insert_list(head, mcoli, tempi);
}
}
// Check that the end of the file was reached while reading it.
if (!feof(file))
printf("reading (first pass) error in %s\n",f);
// Create array that will store the data
*arrayptr = (double *) malloc(2 * nrows * sizeof(double));
// Convert the linked list into a 2D array (pointer)
p = *arrayptr;
i = nrows - 1;
for(i = nrows - 1; i >= 0; i--) {
// Assign entry's 1st column - mcol
p = *arrayptr + 2 * i;
*p = head->mcol;
// Assign entry's 2nd column - temp
p += 1;
*p = head->temp;
// Move to the next element in the reverse-ordered linked list
head = head->tail;
}
fclose(file);
delete_list(head);
return nrows;
}
// Prevent "unused function" and "unused variable" warnings.
static const void *dummy_ref[] = {print_list, dummy_ref};