LCOV - code coverage report
Current view: top level - src/IO - FileNameParse.cpp (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 53 61 86.9 %
Date: 2024-11-18 05:28:54 Functions: 1 1 100.0 %

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

Generated by: LCOV version 1.14