Line data Source code
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 :
9 : std::time_t t = 0;
10 :
11 : /// Internal function to do processing of the file name
12 76 : void IO::FileNameParse(std::string &filename)
13 : {
14 :
15 : //
16 : // Variable substitution
17 : //
18 :
19 76 : IO::ParmParse pp;
20 76 : std::string input = filename;
21 : // Define the regex pattern to match {name_of_variable,"formatting string"} or {name_of_variable}
22 76 : std::regex pattern("\\{([a-zA-Z0-9_\\.]+)(\\s*,\\s*\"([^\"]*)\")?\\}");
23 :
24 : // Iterator to find all matches
25 76 : auto matches_begin = std::sregex_iterator(input.begin(), input.end(), pattern);
26 76 : auto matches_end = std::sregex_iterator();
27 :
28 : // Iterate over the matches and print them
29 76 : for (std::sregex_iterator i = matches_begin; i != matches_end; ++i)
30 : {
31 0 : std::smatch match = *i;
32 0 : std::string variable_name = match[1].str();
33 0 : std::string formatting_string = match[3].str(); // match[3] captures the format string
34 :
35 0 : if (!formatting_string.empty()) {
36 0 : Util::Exception(INFO,"Formatting strings are not supported yet");
37 : } else {
38 0 : std::vector<std::string> variable_value;
39 0 : pp.queryarr(variable_name.c_str(),variable_value);
40 0 : Util::String::ReplaceAll(filename,"{"+variable_name+"}",Util::String::Join(variable_value,'_'));
41 0 : }
42 76 : }
43 :
44 :
45 : //
46 : // Time wildcards
47 : //
48 :
49 76 : if (!t) t = std::time(0);
50 76 : std::tm * now = std::localtime(&t);
51 76 : int year = now->tm_year+1900;
52 76 : int month = now->tm_mon+1;
53 76 : int day = now->tm_mday;
54 76 : int hour = now->tm_hour;
55 76 : int minute = now->tm_min;
56 76 : int second = now->tm_sec;
57 76 : int microsecond = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000000;
58 :
59 76 : std::stringstream ss;
60 :
61 : // _Y: year
62 76 : ss.str("");
63 76 : ss << year;
64 76 : std::string _Y = ss.str();
65 228 : Util::String::ReplaceAll(filename,"%Y",_Y);
66 :
67 : // _m: month (01..12)
68 76 : ss.str("");
69 76 : ss << std::setfill('0') << std::setw(2) << month;
70 76 : std::string _m = ss.str();
71 228 : Util::String::ReplaceAll(filename,"%m",_m);
72 :
73 : // _d: day of month (01..31)
74 76 : ss.str("");
75 76 : ss << std::setfill('0') << std::setw(2) << day;
76 76 : std::string _d = ss.str();
77 228 : Util::String::ReplaceAll(filename,"%d",_d);
78 :
79 : // _H: hour (00..23)
80 76 : ss.str("");
81 76 : ss << std::setfill('0') << std::setw(2) << hour;
82 76 : std::string _H = ss.str();
83 228 : Util::String::ReplaceAll(filename,"%H",_H);
84 :
85 : // _M: minute (00..59)
86 76 : ss.str("");
87 76 : ss << std::setfill('0') << std::setw(2) << minute;
88 76 : std::string _M = ss.str();
89 228 : Util::String::ReplaceAll(filename,"%M",_M);
90 :
91 : // _S: second (00..59)
92 76 : ss.str("");
93 76 : ss << std::setfill('0') << std::setw(2) << second;
94 76 : std::string _S = ss.str();
95 228 : Util::String::ReplaceAll(filename,"%S",_S);
96 :
97 : // _f: microsecond (00..59)
98 76 : ss.str("");
99 : //ss << std::setfill('0') << std::setw(2) << second;
100 76 : ss << microsecond;
101 76 : std::string _f = ss.str();
102 228 : Util::String::ReplaceAll(filename,"%f",_f);
103 :
104 : // _D: spatial dimension (1,2,3)
105 76 : ss.str("");
106 76 : ss << AMREX_SPACEDIM;
107 76 : std::string _D = ss.str();
108 228 : 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 76 : std::vector<char> cstr(filename.begin(),filename.end());
123 76 : amrex::ParallelDescriptor::Bcast(cstr.data(), cstr.size(), amrex::ParallelDescriptor::IOProcessorNumber(), MPI_COMM_WORLD);
124 76 : filename = std::string(cstr.begin(),cstr.end());
125 76 : }
|