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