Alamo
FileNameParse.cpp
Go to the documentation of this file.
1 #include "FileNameParse.H"
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <sstream>
6 #include <chrono>
7 #include <regex>
8 
9 #include "Util/Util.H"
10 #include "IO/ParmParse.H"
11 
12 std::time_t t = 0;
13 
14 void IO::FileNameParse(std::string &filename)
15 {
16 
17  //
18  // Variable substitution
19  //
20 
21  IO::ParmParse pp;
22  std::string input = filename;
23  // Define the regex pattern to match {name_of_variable,"formatting string"} or {name_of_variable}
24  std::regex pattern("\\{([a-zA-Z0-9_\\.]+)(\\s*,\\s*\"([^\"]*)\")?\\}");
25 
26  // Iterator to find all matches
27  auto matches_begin = std::sregex_iterator(input.begin(), input.end(), pattern);
28  auto matches_end = std::sregex_iterator();
29 
30  // Iterate over the matches and print them
31  for (std::sregex_iterator i = matches_begin; i != matches_end; ++i)
32  {
33  std::smatch match = *i;
34  std::string variable_name = match[1].str();
35  std::string formatting_string = match[3].str(); // match[3] captures the format string
36 
37  if (!formatting_string.empty()) {
38  Util::Exception(INFO,"Formatting strings are not supported yet");
39  } else {
40  std::vector<std::string> variable_value;
41  pp.queryarr(variable_name.c_str(),variable_value);
42  Util::String::ReplaceAll(filename,"{"+variable_name+"}",Util::String::Join(variable_value,'_'));
43  }
44  }
45 
46 
47  //
48  // Time wildcards
49  //
50 
51  if (!t) t = std::time(0);
52  std::tm * now = std::localtime(&t);
53  int year = now->tm_year+1900;
54  int month = now->tm_mon+1;
55  int day = now->tm_mday;
56  int hour = now->tm_hour;
57  int minute = now->tm_min;
58  int second = now->tm_sec;
59  int microsecond = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000000;
60 
61  std::stringstream ss;
62 
63  // _Y: year
64  ss.str("");
65  ss << year;
66  std::string _Y = ss.str();
68 
69  // _m: month (01..12)
70  ss.str("");
71  ss << std::setfill('0') << std::setw(2) << month;
72  std::string _m = ss.str();
74 
75  // _d: day of month (01..31)
76  ss.str("");
77  ss << std::setfill('0') << std::setw(2) << day;
78  std::string _d = ss.str();
80 
81  // _H: hour (00..23)
82  ss.str("");
83  ss << std::setfill('0') << std::setw(2) << hour;
84  std::string _H = ss.str();
86 
87  // _M: minute (00..59)
88  ss.str("");
89  ss << std::setfill('0') << std::setw(2) << minute;
90  std::string _M = ss.str();
92 
93  // _S: second (00..59)
94  ss.str("");
95  ss << std::setfill('0') << std::setw(2) << second;
96  std::string _S = ss.str();
98 
99  // _f: microsecond (00..59)
100  ss.str("");
101  //ss << std::setfill('0') << std::setw(2) << second;
102  ss << microsecond;
103  std::string _f = ss.str();
105 
106  // _D: spatial dimension (1,2,3)
107  ss.str("");
108  ss << AMREX_SPACEDIM;
109  std::string _D = ss.str();
111 
112 
113 
114 
115 
116 
117 
118 
119 
120  // Ensure consistency in filename across processor name.
121  // (if %f - microseconds is used, then processors will typically have different filenames.)
122  // !! Health warning: nothing is done to ensure that filename is consistent across
123  // MPI tasks. This could cause errors some day !!
124  std::vector<char> cstr(filename.begin(),filename.end());
125  amrex::ParallelDescriptor::Bcast(cstr.data(), cstr.size(), amrex::ParallelDescriptor::IOProcessorNumber(), MPI_COMM_WORLD);
126  filename = std::string(cstr.begin(),cstr.end());
127 }
Util::filename
std::string filename
Definition: Util.cpp:19
FileNameParse.H
Util.H
IO::FileNameParse
void FileNameParse(std::string &filename)
Definition: FileNameParse.cpp:14
Util::String::ReplaceAll
int ReplaceAll(std::string &str, const std::string before, const std::string after)
Definition: Util.cpp:224
t
std::time_t t
Definition: FileNameParse.cpp:12
ParmParse.H
Util::Exception
void Exception(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:194
IO::ParmParse
Definition: ParmParse.H:110
Util::String::Join
std::string Join(std::vector< std::string > &vec, char separator)
Definition: Util.cpp:243
INFO
#define INFO
Definition: Util.H:20
IO::ParmParse::queryarr
int queryarr(std::string name, std::vector< T > &value, std::string, std::string, int)
Definition: ParmParse.H:333