-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_mutils.c
224 lines (192 loc) · 5.47 KB
/
utils_mutils.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
223
224
/*******************************************************************************
* utils_mutils.c
*
*
* Copyright (C) 2013, 2014 Tomasz Korzec
*
* Based on openQCD
* Copyright (C) 2012 Martin Luescher and Stefan Schaefer
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************
*
*
* functions that should go into
* modules/utils/mutils.c
*
*
******************************************************************************/
#define UTILS_MUTILS_C
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
#include "utils.h"
#include "global.h"
static char line[NAME_SIZE+1];
/* this is an exact copy of /modules/utils/mutils.c's version */
static char *get_line(void)
{
char *s,*c;
s=fgets(line,NAME_SIZE+1,stdin);
if (s!=NULL)
{
error_root(strlen(line)==NAME_SIZE,1,"get_line [mutils.c]",
"Input line is longer than NAME_SIZE-1");
c=strchr(line,'#');
if (c!=NULL)
c[0]='\0';
}
return s;
}
/* this is an exact copy of /modules/utils/mutils.c's version */
static void check_tag(char *tag)
{
if (tag[0]=='\0')
return;
error_root((strspn(tag," 0123456789.")!=0L)||
(strcspn(tag," \n")!=strlen(tag)),1,
"check_tag [mutils.c]","Improper tag %s",tag);
}
static long find_tag_opt(char *tag)
{
int ie;
long tofs,lofs,ofs;
char *s,*pl,*pr;
ie=0;
tofs=-1L;
lofs=ftell(stdin);
rewind(stdin);
ofs=ftell(stdin);
s=get_line();
while (s!=NULL)
{
pl=strchr(line,'[');
pr=strchr(line,']');
if ((pl==(line+strspn(line," \t")))&&(pr>pl))
{
if (ofs<lofs)
{
ie=0;
tofs=-1L;
}
else
break;
}
else
{
pl=line+strspn(line," \t");
pr=pl+strcspn(pl," \t\n");
pr[0]='\0';
if (strcmp(pl,tag)==0)
{
if (tofs!=-1L)
ie=1;
tofs=ofs;
}
}
ofs=ftell(stdin);
s=get_line();
}
if (tofs==-1L)
return tofs;
else
{
error_root(ie!=0,1,"find_tag_opt [mutils.c]",
"Tag %s occurs more than once in the current section",tag);
ie=fseek(stdin,tofs,SEEK_SET);
error_root(ie!=0,1,"find_tag_opt [mutils.c]",
"Unable to go to line with tag %s",tag);
}
return tofs;
}
/* long read_line_opt(char *tag,char *defaultline,char *format,...)
*
* On process 0, this program reads a line of text and data from stdin
* in a controlled manner. The tag cannot
* be the empty string "", the program searches
* for the tag in the current section. If the tag is not
* found, default values are assigned to the variables. The program returns
* the offset of the line from the beginning
* of the file and positions the file pointer to the next line. On
* processes other than 0, the program does nothing and returns -1L.
*
*/
long read_line_opt(char *tag,char *defaultline,char *format, ...)
{
int my_rank,is,ic;
long tofs;
char *pl,*p;
va_list args;
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
if (my_rank==0)
{
check_tag(tag);
error_root(tag[0]=='\0',1,"read_line_opt [mutils.c]",
"optional parameters must have a non empty tag");
tofs=find_tag_opt(tag);
if(tofs != -1L)
{
get_line();
pl=line+strspn(line," \t");
pl+=strcspn(pl," \t\n");
}else
{
/* optional parameter gets default value */
error_root(strlen(defaultline)+strlen(tag)+1>NAME_SIZE,1,
"read_line_opt [mutils.c]",
"default line too long");
sprintf(line,"%s %s",tag,defaultline);
pl=line+strspn(line," \t");
pl+=strcspn(pl," \t\n");
}
va_start(args,format);
for (p=format;;)
{
p+=strspn(p," ");
ic=0;
is=2;
if ((p[0]=='\0')||(p[0]=='\n'))
break;
else if (p==strstr(p,"%s"))
ic=sscanf(pl,"%s",va_arg(args,char*));
else if (p==strstr(p,"%d"))
ic=sscanf(pl,"%d",va_arg(args,int*));
else if (p==strstr(p,"%f"))
ic=sscanf(pl,"%f",va_arg(args,float*));
else if (p==strstr(p,"%lf"))
{
is=3;
ic=sscanf(pl,"%lf",va_arg(args,double*));
}
else
error_root(1,1,"read_line_opt [mutils.c]",
"Incorrect format string %s on line with tag %s",
format,tag);
error_root(ic!=1,1,"read_line_opt [mutils.c]",
"Missing data item(s) on line with tag %s",tag);
p+=is;
pl+=strspn(pl," \t");
pl+=strcspn(pl," \t\n");
}
va_end(args);
return tofs;
}
else
return -1L;
}