11#define COMMENT_CHAR '#'
20static std::string empty_key(
"");
22bool input::open(
const std::string &file_name,
bool use_cmdline,
bool exit_on_error) {
23 extern const char *input_file;
27 if (use_cmdline && input_file !=
nullptr) {
35 bool got_error =
false;
39 hila::out0 <<
"Error: file '" << fname <<
"' cannot be opened because '" << filename
40 <<
"' is open in this input variable\n";
48 print_dashed_line(
"Reading from standard input");
51 inputfile.open(fname);
52 if (inputfile.is_open()) {
53 is_initialized =
true;
56 print_dashed_line(
"Reading file " + filename);
63 hila::out0 <<
"Input file '" << fname <<
"' could not be opened\n";
72 if (got_error && exit_on_error) {
80 if (is_initialized && !use_cin) {
82 is_initialized =
false;
91bool input::get_line() {
102 if (!std::getline(*inptr, linebuffer)) {
107 }
while (linebuffer.at(0) == COMMENT_CHAR);
108 size_t i = linebuffer.find(COMMENT_CHAR);
109 if (i != std::string::npos)
110 linebuffer.resize(i);
113 is_line_printed =
false;
119void input::print_linebuf(
int end_of_key) {
124 is_line_printed =
true;
127 while (i < linebuffer.size() && std::isspace(linebuffer[i]))
129 for (; i < linebuffer.size() && i < end_of_key; i++) {
133 if (end_of_key > 0) {
134 for (
int j = i; j < 20; j++)
138 while (i < linebuffer.size() && std::isspace(linebuffer[i]))
140 if (i < linebuffer.size()) {
148bool input::remove_whitespace() {
150 while (lb_start < linebuffer.size() && std::isspace(linebuffer[lb_start]))
152 if (lb_start == linebuffer.size())
162bool input::contains_word_list(
const std::string &list,
int &end_of_key) {
163 const char *p = linebuffer.c_str() + lb_start;
164 const char *q = list.c_str();
165 while (std::isspace(*p))
167 while (std::isspace(*q))
170 bool last_space =
false;
174 while (*p && *q && *p == *q && !std::isspace(*q)) {
180 if (std::isspace(*q) && std::isspace(*p)) {
182 while (std::isspace(*p))
184 while (std::isspace(*q))
194 while (std::isspace(*q))
199 if (*q != 0 || !(last_space || *p == 0 || std::isspace(*p) || last_char ==
',' || *p ==
','))
202 end_of_key = p - linebuffer.c_str();
204 while (std::isspace(*p))
206 lb_start = p - linebuffer.c_str();
214bool input::peek_token(std::string &tok) {
215 if (!remove_whitespace())
218 bool in_quotes =
false;
219 for (i = lb_start; i < linebuffer.size() &&
220 ((!std::isspace(linebuffer[i]) && linebuffer[i] !=
',') || in_quotes);
222 if (linebuffer[i] ==
'"') {
223 in_quotes = !in_quotes;
226 if (i == lb_start && linebuffer[i] ==
',')
233 tok = linebuffer.substr(lb_start, i - lb_start);
239bool input::get_token(std::string &tok) {
240 if (peek_token(tok)) {
241 lb_start += tok.size();
249bool input::match_token(
const std::string &tok) {
251 if (peek_token(s) && s == tok) {
252 lb_start += tok.size();
260bool input::handle_key(
const std::string &key) {
266 if (key.size() > 0 && !contains_word_list(key, end_of_key)) {
268 hila::out0 <<
"Error: expecting key '" << key <<
"', found instead:\n";
269 hila::out0 <<
"\"" << linebuffer <<
"\"\n----\n";
274 print_linebuf(end_of_key);
283bool input::is_value(
const std::string &str, std::string &val) {
284 val = remove_quotes(str);
288std::string input::remove_quotes(
const std::string &val) {
292 for (j = i = 0; i < val.size(); i++)
302int input::get_item(
const std::string &label,
const std::vector<std::string> &items,
bool bcast) {
304 bool no_error = handle_key(label);
310 if (no_error && peek_token(s)) {
312 for (
int i = 0; i < items.size() && item < 0; i++) {
318 if ((items[i] ==
"%s") ||
319 (items[i] ==
"%f" && is_value(s,dval)) ||
320 (items[i] ==
"%i" && is_value(s,lval)) ||
321 contains_word_list(items[i],end_of_key)) {
333 hila::out0 <<
"Input '" << label <<
"' must be one of: ";
334 for (
int i = 0; i < items.size(); i++) {
335 if (items[i] ==
"%s")
337 else if (items[i] ==
"%f")
339 else if (items[i] ==
"%i")
This file defines all includes for HILA.
Implement hila::swap for gauge fields.
int myrank()
rank of this node
std::ostream out0
This writes output only from main process (node 0)
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
void finishrun()
Normal, controlled exit - all nodes must call this. Prints timing information and information about c...
void broadcast2(T &t, U &u, int rank=0)
and broadcast with two values