gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
Gambit::Utils Namespace Reference

Classes

struct  ci_less
 
class  specialised_threadsafe_rng
 Derived thread-safe random number generator class, templated on the RNG engine type. More...
 
class  threadsafe_rng
 Base class for thread-safe random number generators. More...
 
struct  type_equivalency
 Structure providing type equivalency classes to the dep resolver. More...
 

Typedefs

typedef std::chrono::time_point< std::chrono::system_clock > time_point
 

Functions

type_equivalencytypeEquivalencies ()
 Backend info accessor function. More...
 
str fix_type (str)
 Clean out whitespace and strip Gambit and default BOSSed class namespaces. More...
 
time_point get_clock_now ()
 Get clock time. More...
 
double unwrap (double x, void *p)
 Unwrapper for passing std::function to GSL integrator Based on example from https://martin-ueding.de/articles/cpp-lambda-into-gsl/index.html. More...
 
double integrate_cquad (std::function< double(double)> ftor, double a, double b, double abseps, double releps)
 Integrate a std::function using GSL cquad. More...
 
EXPORT_SYMBOLS const strruntime_scratch ()
 Return the path to the run-specific scratch directory Don't call this from a destructor, as the internal static str may have already been destroyed. More...
 
EXPORT_SYMBOLS str construct_runtime_scratch (bool fail_on_mpi_uninitialised=true)
 Construct the path to the run-specific scratch directory This version is safe to call from a destructor. More...
 
EXPORT_SYMBOLS std::vector< strdelimiterSplit (str s, str delim)
 Split a string into a vector of strings, using a delimiter, and removing any whitespace around the delimiter. More...
 
EXPORT_SYMBOLS str strip_leading_namespace (str s, str ns)
 Strips namespace from the start of a string, or after "const". More...
 
EXPORT_SYMBOLS str replace_leading_namespace (str s, str ns, str ns_new)
 Replaces a namespace at the start of a string, or after "const". More...
 
EXPORT_SYMBOLS void strip_whitespace_except_after_const (str &)
 Strip all whitespace except that following "const", in which case the whitespace is replaced by a single space. More...
 
EXPORT_SYMBOLS void strip_parentheses (str &)
 Strips leading and/or trailing parentheses from a string. More...
 
EXPORT_SYMBOLS bool sspairset_contains (const str &, const std::set< sspair > &)
 Test if a set of str,str pairs contains any entry with first element matching a given string. More...
 
EXPORT_SYMBOLS bool sspairset_contains (const str &, const str &, const std::set< sspair > &)
 Tests if a set of str,str pairs contains an entry matching two given strings. More...
 
EXPORT_SYMBOLS bool sspairset_contains (const sspair &, const std::set< sspair > &)
 Tests if a set of str,str pairs contains an entry matching a given pair. More...
 
EXPORT_SYMBOLS str str_fixed_len (str, int)
 Created a str of a specified length. More...
 
EXPORT_SYMBOLS void strcpy2f (char *, int, str)
 Copy a str to a character array, stripping the null termination character. More...
 
EXPORT_SYMBOLS bool endsWith (const std::string &str, const std::string &suffix)
 Checks whether `str' ends with `suffix'. More...
 
EXPORT_SYMBOLS bool startsWith (const std::string &str, const std::string &prefix, bool case_sensitive=true)
 Checks whether `str' begins with `prefix'. More...
 
EXPORT_SYMBOLS bool iequals (const std::string &a, const std::string &b, bool case_sensitive=false)
 Perform a (possibly) case-insensitive string comparison. More...
 
EXPORT_SYMBOLS std::vector< std::string > split (const std::string &input, const std::string &delimiter)
 Split string into vector of strings, using a delimiter string. More...
 
EXPORT_SYMBOLS std::string strtolower (const std::string &a)
 Convert a whole string to lowercase. More...
 
template<typename T , size_t N>
T * beginA (T(&arr)[N])
 Get pointers to beginning and end of array. More...
 
template<typename T , size_t N>
T * endA (T(&arr)[N])
 
template<class Set1 , class Set2 >
bool is_disjoint (const Set1 &set1, const Set2 &set2)
 Test if two sets are disjoint (works on any sorted std container I think) More...
 
EXPORT_SYMBOLS const strensure_path_exists (const str &)
 Ensure that a path exists (and then return the path, for chaining purposes) More...
 
EXPORT_SYMBOLS bool file_exists (const std::string &filename)
 Check if a file exists. More...
 
EXPORT_SYMBOLS std::vector< strls_dir (const str &dir)
 Return a vector of strings listing the contents of a directory (POSIX) More...
 
EXPORT_SYMBOLS str dir_name (const str &path)
 Get directory name from full path+filename (POSIX) More...
 
EXPORT_SYMBOLS str base_name (const str &path)
 Get file name from full path+filename (POSIX) More...
 
EXPORT_SYMBOLS int remove_all_files_in (const str &dirname, bool error_if_absent=true)
 Delete all files in a directory (does not act recursively) More...
 
EXPORT_SYMBOLS str return_time_and_date (const time_point &in)
 Get date and time. More...
 
EXPORT_SYMBOLS bool are_similar (const str &s1, const str &s2)
 Check if two strings are a "close" match Used for "did you mean?" type checking during command line argument processing. More...
 
bool check1 (const str &s1, const str &s2)
 Sub-check for are_similar. More...
 
bool check2 (const str &s1, const str &s2)
 Sub-check for are_similar. More...
 
EXPORT_SYMBOLS double sqr (double a)
 returns square of double - saves tedious repetition More...
 
EXPORT_SYMBOLS bool isInteger (const std::string &)
 Check if a string represents an integer From: http://stackoverflow.com/a/2845275/1447953. More...
 
template<typename T >
int sgn (T val)
 Get the sign of a (hopefully numeric type) More...
 
template<typename... T>
void dummy_function ()
 
template<typename T >
void dummy_function (T one)
 
template<typename T1 , typename... T>
void dummy_function (T1 first, T... args)
 
bool sspairset_contains (const str &el, const std::set< std::pair< str, str >> &set)
 Test if a set of str,str pairs contains any entry with first element matching a given string. More...
 
bool sspairset_contains (const str &el1, const str &el2, const std::set< std::pair< str, str >> &set)
 Tests if a set of str,str pairs contains an entry matching two given strings. More...
 

Variables

const str buildtime_scratch = GAMBIT_DIR "/scratch/build_time/"
 Return the path to the build-time scratch directory. More...
 
const char * whitespaces [] = {" ", "\t", "\n", "\f", "\r"}
 

Typedef Documentation

◆ time_point

typedef std::chrono::time_point< std::chrono::system_clock > Gambit::Utils::time_point

Definition at line 46 of file logging.hpp.

Function Documentation

◆ are_similar()

bool Gambit::Utils::are_similar ( const str s1,
const str s2 
)

Check if two strings are a "close" match Used for "did you mean?" type checking during command line argument processing.

Definition at line 345 of file util_functions.cpp.

References check1(), and check2().

Referenced by is_disjoint(), and Gambit::gambit_core::run_diagnostic().

346  {
347  if(check1(s1,s2) or check1(s2,s1)){ return true; }
348  else if(check2(s1,s2)){ return true; } // symmetric
349  else{ return false; }
350  //TODO: Add more checks? These ones are pretty minimal. Maybe something that computes percentage match between strings...
351  }
bool check2(const str &s1, const str &s2)
Sub-check for are_similar.
bool check1(const str &s1, const str &s2)
Sub-check for are_similar.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ base_name()

std::string Gambit::Utils::base_name ( const str path)

Get file name from full path+filename (POSIX)

Definition at line 286 of file util_functions.cpp.

Referenced by Gambit::Printers::HDF5::find_temporary_files(), is_disjoint(), and main().

287  {
288  char buffer[1000]; // temporary buffer for basename to work with (it is a C function)
289  path.copy(buffer, path.size()); //TODO: error if path.size()>1000
290  buffer[path.size()] = '\0';
291  std::string result = basename(&buffer[0]); // should use the C function...
292  return result;
293  }
Here is the caller graph for this function:

◆ beginA()

template<typename T , size_t N>
T* Gambit::Utils::beginA ( T(&)  arr[N])

Get pointers to beginning and end of array.

Definition at line 156 of file util_functions.hpp.

156 { return &arr[0]; }

◆ check1()

bool Gambit::Utils::check1 ( const str s1,
const str s2 
)

Sub-check for are_similar.

true if s1 can be obtained by deleting one character from s2

Definition at line 354 of file util_functions.cpp.

Referenced by are_similar(), is_disjoint(), and Gambit::DarkBit::RD_oh2_Axions().

355  {
356  if(s2.length() - s1.length() != 1){ return false; }
357  unsigned int i,j;
358  for(i=0,j=0; i<s2.length(); i++,j++)
359  {
360  if(s2[i] == s1[j])
361  {/*do nothing*/}
362  else if(i == j)
363  { j++;}
364  else
365  {return false;}
366  }
367  return true;
368  }
Here is the caller graph for this function:

◆ check2()

bool Gambit::Utils::check2 ( const str s1,
const str s2 
)

Sub-check for are_similar.

true if s1 can be obtained from s2 by changing no more than X characters (X=2 for now)

Definition at line 371 of file util_functions.cpp.

Referenced by are_similar(), is_disjoint(), and Gambit::DarkBit::RD_oh2_Axions().

372  {
373  unsigned int error_limit = 2;
374  unsigned int number_of_errors = 0;
375 
376  if(s2.length() != s1.length()){ return false; }
377  unsigned int i,j;
378  for(i=0,j=0; i<s2.length(); i++,j++)
379  {
380  if(s2[i] == s1[j])
381  {/*do nothing*/}
382  else if(number_of_errors <= error_limit)
383  { number_of_errors++;}
384  else
385  {return false;}
386  }
387  return true;
388  }
Here is the caller graph for this function:

◆ construct_runtime_scratch()

str Gambit::Utils::construct_runtime_scratch ( bool  fail_on_mpi_uninitialised = true)

Construct the path to the run-specific scratch directory This version is safe to call from a destructor.

Definition at line 67 of file util_functions.cpp.

References ensure_path_exists(), LOCAL_INFO, and Gambit::utils_error().

Referenced by Gambit::Logging::LogMaster::emit_backlog(), and runtime_scratch().

72  {
73  str master_procID;
74  #ifdef WITH_MPI
75  if (GMPI::Is_initialized() and not GMPI::Is_finalized())
76  {
77  master_procID = "/master_process_" + std::to_string(GMPI::Comm().MasterPID());
78  }
79  else
80  {
81  if (fail_on_mpi_uninitialised)
82  utils_error().raise(LOCAL_INFO, "Tried to call construct_runtime_scratch without MPI initialised!");
83  master_procID = "/unattached_MPI_process_" + std::to_string(getpid());
84  }
85  #else
86  master_procID = "/master_process_" + std::to_string(getpid());
87  #endif
88  return ensure_path_exists(GAMBIT_DIR "/scratch/run_time/machine_" + std::to_string(gethostid()) + master_procID + "/");
89  }
EXPORT_SYMBOLS error & utils_error()
Utility errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
EXPORT_SYMBOLS const str & ensure_path_exists(const str &)
Ensure that a path exists (and then return the path, for chaining purposes)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ delimiterSplit()

std::vector< str > Gambit::Utils::delimiterSplit ( str  s,
str  delim 
)

Split a string into a vector of strings, using a delimiter, and removing any whitespace around the delimiter.

Split a string into a vector of strings using a delimiter, and remove any whitespace around the delimiters.

Definition at line 93 of file util_functions.cpp.

References split(), and daFunk::vec().

Referenced by Gambit::PostProcessor::get_done_points(), Gambit::Logging::LogMaster::initialise(), Gambit::module_functor_common::makeBackendMatchingRule(), Gambit::module_functor_common::makeBackendOptionRule(), Gambit::module_functor_common::makeBackendRuleForModel(), Gambit::Printers::parse_label_for_ModelParameters(), Gambit::Printers::parse_label_for_spectrum_entry(), Gambit::PostProcessor::PPDriver::PPDriver(), Gambit::functor::setAllowedModelGroupCombo(), Gambit::module_functor_common::setBackendConditionalDependency(), Gambit::module_functor_common::setBackendReq(), Gambit::module_functor_common::setModelConditionalBackendReq(), Gambit::module_functor_common::setModelConditionalDependency(), and Gambit::functor::setModelGroup().

94  {
95  std::vector<str> vec;
96  // Get rid of any whitespace around the delimiters
97  std::regex rgx1("\\s+"+delim), rgx2(delim+"\\s+");
98  s = std::regex_replace(s, rgx1, delim);
99  s = std::regex_replace(s, rgx2, delim);
100  if (s == "") return vec;
101  // Split up the list of versions by the delimiters
102  boost::split(vec, s, boost::is_any_of(delim), boost::token_compress_on);
103  return vec;
104  }
EXPORT_SYMBOLS std::vector< std::string > split(const std::string &input, const std::string &delimiter)
Split string into vector of strings, using a delimiter string.
std::vector< T > vec(std::vector< T > vector)
Definition: daFunk.hpp:142
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dir_name()

std::string Gambit::Utils::dir_name ( const str path)

Get directory name from full path+filename (POSIX)

Definition at line 276 of file util_functions.cpp.

Referenced by Gambit::Printers::HDF5::find_temporary_files(), Gambit::IniParser::importRound(), and is_disjoint().

277  {
278  char buffer[1000]; // temporary buffer for dirname to work with (it is a C function)
279  path.copy(buffer, path.size()); //TODO: error if path.size()>1000
280  buffer[path.size()] = '\0';
281  std::string result = dirname(&buffer[0]); // should use the C function...
282  return result;
283  }
Here is the caller graph for this function:

◆ dummy_function() [1/3]

template<typename... T>
void Gambit::Utils::dummy_function ( )

Definition at line 242 of file util_functions.hpp.

Referenced by dummy_function().

242 {}
Here is the caller graph for this function:

◆ dummy_function() [2/3]

template<typename T >
void Gambit::Utils::dummy_function ( one)

Definition at line 243 of file util_functions.hpp.

References void.

244  {
245  (void)one;
246  }
hb_ModelParameters void
Funk one(Args... argss)
Definition: daFunk.hpp:602

◆ dummy_function() [3/3]

template<typename T1 , typename... T>
void Gambit::Utils::dummy_function ( T1  first,
T...  args 
)

Definition at line 248 of file util_functions.hpp.

References find_all_gambit_bits::args, dummy_function(), and void.

249  {
250  (void)first;
251  dummy_function(args...);
252  }
hb_ModelParameters void
void dummy_function(T1 first, T... args)
Here is the call graph for this function:

◆ endA()

template<typename T , size_t N>
T* Gambit::Utils::endA ( T(&)  arr[N])

◆ endsWith()

bool Gambit::Utils::endsWith ( const std::string &  str,
const std::string &  suffix 
)

Checks whether `str' ends with `suffix'.

Definition at line 398 of file util_functions.cpp.

References daFunk::delta().

Referenced by Gambit::Printers::HDF5Printer2::check_consistency(), Gambit::Printers::HDF5MasterBuffer::get_all_dset_names_on_disk(), Gambit::Printers::lsGroup_process(), and main().

399  {
400  if (&suffix == &str) return true; // str and suffix are the same string
401  if (suffix.length() > str.length()) return false;
402  size_t delta = str.length() - suffix.length();
403  for (size_t i = 0; i < suffix.length(); ++i) {
404  if (suffix[i] != str[delta + i]) return false;
405  }
406  return true;
407  }
Funk delta(std::string arg, double pos, double width)
Definition: daFunk.hpp:902
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensure_path_exists()

const std::string & Gambit::Utils::ensure_path_exists ( const str path)

Ensure that a path exists (and then return the path, for chaining purposes)

Definition at line 227 of file util_functions.cpp.

Referenced by Gambit::IniParser::Parser::basicParse(), Gambit::Printers::asciiPrinter::common_constructor(), Gambit::Printers::HDF5Printer::common_constructor(), construct_runtime_scratch(), Gambit::Scanner::Plugins::pluginInfo::dump(), Gambit::initialise_standalone_logs(), is_disjoint(), main(), Gambit::SpecBit::make_vpp_inputs(), Gambit::Scanner::Plugins::pluginInfo::resume(), scanner_plugin(), and Gambit::Scanner::Plugins::pluginInfo::temp_file_path().

228  {
229  // Split off potential filename
230  // If only path is provided, it must end in a slash!!!
231  size_t found = path.find_last_of("/\\");
232  if (found != std::string::npos)
233  {
234  std::string prefix = path.substr(0,found);
235  recursive_mkdir( prefix.c_str() );
236  }
237  return path;
238  }
Here is the caller graph for this function:

◆ file_exists()

bool Gambit::Utils::file_exists ( const std::string &  filename)

◆ fix_type()

str Gambit::Utils::fix_type ( str  s)

Clean out whitespace and strip Gambit and default BOSSed class namespaces.

Definition at line 34 of file type_equivalency.cpp.

References replace_leading_namespace(), strip_leading_namespace(), and strip_whitespace_except_after_const().

Referenced by Gambit::Utils::type_equivalency::add(), Gambit::IniParser::IniFile::getRules(), Gambit::DRes::matchesRules(), Gambit::module_functor_common::setBackendReq(), Gambit::module_functor_common::setDependency(), and Gambit::module_functor_common::setModelConditionalBackendReqSingular().

35  {
37  s = strip_leading_namespace(s, "Gambit");
38  // Loop over all the default versions of BOSSed backends and replace any corresponding leading namespace.
39  for (auto it = Backends::backendInfo().default_safe_versions.begin(); it != Backends::backendInfo().default_safe_versions.end(); ++it)
40  {
41  s = Utils::replace_leading_namespace(s, it->first+"_default", it->first+"_"+it->second);
42  }
43  return s;
44  }
EXPORT_SYMBOLS str replace_leading_namespace(str s, str ns, str ns_new)
Replaces a namespace at the start of a string, or after "const".
EXPORT_SYMBOLS void strip_whitespace_except_after_const(str &)
Strip all whitespace except that following "const", in which case the whitespace is replaced by a sin...
EXPORT_SYMBOLS str strip_leading_namespace(str s, str ns)
Strips namespace from the start of a string, or after "const".
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_clock_now()

time_point Gambit::Utils::get_clock_now ( )

Get clock time.

Get current system clock time.

Definition at line 328 of file util_functions.cpp.

Referenced by Gambit::SpecBit::helper_run_vevacious(), and is_disjoint().

329  {
330  return std::chrono::system_clock::now();
331  }
Here is the caller graph for this function:

◆ iequals()

bool Gambit::Utils::iequals ( const std::string &  a,
const std::string &  b,
bool  case_sensitive = false 
)

Perform a (possibly) case-insensitive string comparison.

Perform a simple case-insensitive string comparison From: https://stackoverflow.com/a/4119881/1447953.

Definition at line 195 of file util_functions.cpp.

References b.

Referenced by Gambit::Printers::SQLitePrinter::ensure_column_exists(), Gambit::Printers::SQLitePrinter::insert_data(), Gambit::Printers::parse_label_for_ModelParameters(), and Gambit::Printers::parse_label_for_spectrum_entry().

196  {
197  if(case_sensitive)
198  return a==b;
199  unsigned int sz = a.size();
200  if (b.size() != sz)
201  return false;
202  for (unsigned int i = 0; i < sz; ++i)
203  if (tolower(a[i]) != tolower(b[i]))
204  return false;
205  return true;
206  }
START_MODEL b
Definition: demo.hpp:270
Here is the caller graph for this function:

◆ integrate_cquad()

double Gambit::Utils::integrate_cquad ( std::function< double(double)>  ftor,
double  a,
double  b,
double  abseps,
double  releps 
)

Integrate a std::function using GSL cquad.

Definition at line 40 of file integration.cpp.

References Gambit::invalid_point(), Gambit::invalid_point_exception::raise(), and unwrap().

Referenced by Gambit::DecayBit::chargino_plus_1_decays_smallsplit(), and Gambit::DecayBit::stau_1_decays_smallsplit().

41  {
42 
43  double result = 0.0;
44  gsl_integration_cquad_workspace * gsl_ws = gsl_integration_cquad_workspace_alloc(100);
45 
46  gsl_function F;
47  F.function = unwrap;
48  F.params = &ftor;
49 
50  gsl_integration_cquad(&F, a, b, abseps, releps, gsl_ws, &result, NULL, NULL);
51  gsl_integration_cquad_workspace_free(gsl_ws);
52 
53  // Check result
54  if (Utils::isnan(result))
55  {
56  invalid_point().raise("Integration returned NaN.");
57  }
58 
59  return result;
60  }
START_MODEL b
Definition: demo.hpp:270
double unwrap(double x, void *p)
Unwrapper for passing std::function to GSL integrator Based on example from https://martin-ueding.de/articles/cpp-lambda-into-gsl/index.html.
Definition: integration.cpp:33
virtual void raise(const std::string &)
Raise the exception, i.e. throw it. Exact override of base method.
Definition: exceptions.cpp:422
invalid_point_exception & invalid_point()
Invalid point exceptions.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_disjoint()

template<class Set1 , class Set2 >
bool Gambit::Utils::is_disjoint ( const Set1 &  set1,
const Set2 &  set2 
)

Test if two sets are disjoint (works on any sorted std container I think)

Definition at line 163 of file util_functions.hpp.

References are_similar(), base_name(), check1(), check2(), dir_name(), ensure_path_exists(), EXPORT_SYMBOLS, file_exists(), get_clock_now(), isInteger(), ls_dir(), remove_all_files_in(), return_time_and_date(), and sqr().

Referenced by Gambit::Logging::LogMaster::finalsend(), Gambit::module_functor_common::makeBackendOptionRule(), and Gambit::module_functor_common::makeBackendRuleForModel().

164  {
165  if(set1.empty() || set2.empty()) return true;
166 
167  typename Set1::const_iterator
168  it1 = set1.begin(),
169  it1End = set1.end();
170  typename Set2::const_iterator
171  it2 = set2.begin(),
172  it2End = set2.end();
173 
174  if(*it1 > *set2.rbegin() || *it2 > *set1.rbegin()) return true;
175 
176  while(it1 != it1End && it2 != it2End)
177  {
178  if(*it1 == *it2) return false;
179  if(*it1 < *it2) { it1++; }
180  else { it2++; }
181  }
182 
183  return true;
184  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isInteger()

bool Gambit::Utils::isInteger ( const std::string &  s)

Check if a string represents an integer From: http://stackoverflow.com/a/2845275/1447953.

Definition at line 175 of file util_functions.cpp.

Referenced by Gambit::Printers::HDF5::find_temporary_files(), and is_disjoint().

176  {
177  if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;
178 
179  char * p ;
180  strtol(s.c_str(), &p, 10) ;
181 
182  return (*p == 0) ;
183  }
Here is the caller graph for this function:

◆ ls_dir()

std::vector< std::string > Gambit::Utils::ls_dir ( const str dir)

Return a vector of strings listing the contents of a directory (POSIX)

Return a vector of strings listing the contents of a directory (POSIX) Based on http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html.

Definition at line 251 of file util_functions.cpp.

References void.

Referenced by Gambit::Printers::HDF5::find_temporary_files(), and is_disjoint().

252  {
253  std::vector<std::string> dir_contents;
254  DIR *dp;
255  struct dirent *ep;
256  dp = opendir(dir.c_str());
257 
258  if( dp != NULL )
259  {
260  while( (ep = readdir(dp)) )
261  {
262  dir_contents.push_back(ep->d_name);
263  }
264  (void) closedir(dp);
265  }
266  else
267  {
268  std::string msg = "Utils::ls_dir function failed to open the directory '"+dir+"'!";
269  std::cerr << msg << std::endl;
270  abort();
271  }
272  return dir_contents;
273  }
hb_ModelParameters void
Here is the caller graph for this function:

◆ remove_all_files_in()

int Gambit::Utils::remove_all_files_in ( const str dirname,
bool  error_if_absent = true 
)

Delete all files in a directory (does not act recursively)

Definition at line 296 of file util_functions.cpp.

References LOCAL_INFO, and Gambit::utils_error().

Referenced by is_disjoint(), and scanner_plugin().

297  {
298  struct dirent *pDirent;
299  DIR *pDir;
300  pDir = opendir(dirname.c_str());
301  if (pDir == NULL)
302  {
303  if (error_if_absent)
304  {
305  utils_error().raise(LOCAL_INFO, "Directory "+dirname+" not found.");
306  }
307  else
308  {
309  return 1;
310  }
311  }
312  while ( (pDirent = readdir(pDir)) != NULL )
313  {
314  // Delete the contents
315  if ( strcmp(pDirent->d_name, ".") and strcmp(pDirent->d_name, "..") )
316  {
317  std::ostringstream ss;
318  ss << dirname << pDirent->d_name;
319  cout << "Deleting " << ss.str() << endl;
320  remove(ss.str().c_str());
321  }
322  }
323  closedir (pDir);
324  return 0;
325  }
EXPORT_SYMBOLS error & utils_error()
Utility errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ replace_leading_namespace()

str Gambit::Utils::replace_leading_namespace ( str  s,
str  ns,
str  ns_new 
)

Replaces a namespace at the start of a string, or after "const".

Definition at line 115 of file util_functions.cpp.

Referenced by fix_type(), and Gambit::DRes::typeComp().

116  {
117  std::regex expression("(^|[\\s\\*\\&\\(\\,\\[])"+ns+"::");
118  s = std::regex_replace(s, expression, str("$1")+ns_new+"::");
119  return s;
120  }
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the caller graph for this function:

◆ return_time_and_date()

std::string Gambit::Utils::return_time_and_date ( const time_point in)

Get date and time.

Return (locally defined) date and time corresponding to time_point.

Definition at line 334 of file util_functions.cpp.

Referenced by is_disjoint(), and Gambit::Logging::StdLogger::write().

335  {
336  std::time_t t = std::chrono::system_clock::to_time_t(in);
337 
338  std::string ts = std::ctime(&t); // for example : Tue Sep 27 14:21:13 2011\n
339  ts.resize(ts.size()-1); // Remove the annoying trailing newline
340  return ts;
341  }
Here is the caller graph for this function:

◆ runtime_scratch()

const str & Gambit::Utils::runtime_scratch ( )

Return the path to the run-specific scratch directory Don't call this from a destructor, as the internal static str may have already been destroyed.

Definition at line 59 of file util_functions.cpp.

References construct_runtime_scratch().

Referenced by Gambit::gambit_core::check_databases(), and Gambit::gambit_core::model_diagnostic().

60  {
61  static const str path = construct_runtime_scratch();
62  return path;
63  }
EXPORT_SYMBOLS str construct_runtime_scratch(bool fail_on_mpi_uninitialised=true)
Construct the path to the run-specific scratch directory This version is safe to call from a destruct...
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sgn()

template<typename T >
int Gambit::Utils::sgn ( val)

Get the sign of a (hopefully numeric type)

Definition at line 239 of file util_functions.hpp.

239 { return (T(0) < val) - (val < T(0)); }

◆ split()

std::vector< std::string > Gambit::Utils::split ( const std::string &  input,
const std::string &  delimiter 
)

Split string into vector of strings, using a delimiter string.

Definition at line 209 of file util_functions.cpp.

Referenced by delimiterSplit(), scanner+_harvester::main(), and Gambit::Printers::HDF5Printer2::resume_reader_options().

210  {
211  std::vector<std::string> result;
212  boost::iter_split(result, input, boost::algorithm::first_finder(delimiter));
213  return result;
214  }
Here is the caller graph for this function:

◆ sqr()

◆ sspairset_contains() [1/5]

EXPORT_SYMBOLS bool Gambit::Utils::sspairset_contains ( const str ,
const std::set< sspair > &   
)

Test if a set of str,str pairs contains any entry with first element matching a given string.

Referenced by sspairset_contains().

Here is the caller graph for this function:

◆ sspairset_contains() [2/5]

EXPORT_SYMBOLS bool Gambit::Utils::sspairset_contains ( const str ,
const str ,
const std::set< sspair > &   
)

Tests if a set of str,str pairs contains an entry matching two given strings.

◆ sspairset_contains() [3/5]

bool Gambit::Utils::sspairset_contains ( const sspair quantity,
const std::set< sspair > &  set 
)

Tests if a set of str,str pairs contains an entry matching a given pair.

Definition at line 153 of file util_functions.cpp.

154  {
155  return std::find(set.begin(), set.end(), quantity) != set.end();
156  }

◆ sspairset_contains() [4/5]

bool Gambit::Utils::sspairset_contains ( const str el,
const std::set< std::pair< str, str >> &  set 
)

Test if a set of str,str pairs contains any entry with first element matching a given string.

Definition at line 140 of file util_functions.cpp.

141  {
142  for (std::pair<str,str> x : set) { if (x.first == el) return true; }
143  return false;
144  }

◆ sspairset_contains() [5/5]

bool Gambit::Utils::sspairset_contains ( const str el1,
const str el2,
const std::set< std::pair< str, str >> &  set 
)

Tests if a set of str,str pairs contains an entry matching two given strings.

Definition at line 147 of file util_functions.cpp.

References sspairset_contains().

148  {
149  return sspairset_contains(std::pair<str,str>(el1, el2), set);
150  }
bool sspairset_contains(const str &el1, const str &el2, const std::set< std::pair< str, str >> &set)
Tests if a set of str,str pairs contains an entry matching two given strings.
Here is the call graph for this function:

◆ startsWith()

bool Gambit::Utils::startsWith ( const std::string &  str,
const std::string &  prefix,
bool  case_sensitive = true 
)

Checks whether `str' begins with `prefix'.

Definition at line 410 of file util_functions.cpp.

Referenced by Gambit::PostProcessor::PPDriver::check_settings(), Gambit::PostProcessor::get_done_points(), and Gambit::Printers::parse_label_for_ModelParameters().

411  {
412  if (&prefix == &str) return true; // str and prefix are the same string
413  if (prefix.length() > str.length()) return false;
414  for (size_t i = 0; i < prefix.length(); ++i) {
415  if(case_sensitive)
416  {
417  if (prefix[i] != str[i]) return false;
418  }
419  else
420  {
421  if (tolower(prefix[i]) != tolower(str[i])) return false;
422  }
423  }
424  return true;
425  }
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the caller graph for this function:

◆ str_fixed_len()

str Gambit::Utils::str_fixed_len ( str  s,
int  len 
)

Created a str of a specified length.

Created a std::string of a specified length.

Definition at line 159 of file util_functions.cpp.

Referenced by strcpy2f().

160  {
161  int oldlen = s.length();
162  if (oldlen > len)
163  {
164  return s.substr(0,len-1);
165  }
166  else if (oldlen < len)
167  {
168  s.append(len-oldlen,' ');
169  }
170  return s;
171  }
Here is the caller graph for this function:

◆ strcpy2f()

void Gambit::Utils::strcpy2f ( char *  arr,
int  len,
str  s 
)

Copy a str to a character array, stripping the null termination character.

Copy a std::string to a character array, stripping the null termination character. Good for sending to Fortran.

Definition at line 186 of file util_functions.cpp.

References str_fixed_len().

Referenced by scanner_plugin().

187  {
188  s = str_fixed_len(s, len-1);
189  strcpy(arr, s.c_str());
190  arr[len-1] = ' ';
191  }
EXPORT_SYMBOLS str str_fixed_len(str, int)
Created a str of a specified length.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ strip_leading_namespace()

str Gambit::Utils::strip_leading_namespace ( str  s,
str  ns 
)

Strips namespace from the start of a string, or after "const".

Definition at line 107 of file util_functions.cpp.

Referenced by fix_type().

108  {
109  std::regex expression("(^|[\\s\\*\\&\\(\\,\\[])"+ns+"::");
110  s = std::regex_replace(s, expression, str("$1"));
111  return s;
112  }
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the caller graph for this function:

◆ strip_parentheses()

void Gambit::Utils::strip_parentheses ( str s)

Strips leading and/or trailing parentheses from a string.

Definition at line 133 of file util_functions.cpp.

Referenced by Gambit::module_functor_common::makeBackendMatchingRule(), Gambit::module_functor_common::makeBackendOptionRule(), Gambit::module_functor_common::makeBackendRuleForModel(), Gambit::functor::setAllowedModelGroupCombo(), Gambit::module_functor_common::setBackendReq(), and Gambit::functor::setModelGroup().

134  {
135  if (s.at(0) == '(') s = s.substr(1, s.size());
136  if (*s.rbegin() == ')') s = s.substr(0, s.size()-1);
137  }
Here is the caller graph for this function:

◆ strip_whitespace_except_after_const()

void Gambit::Utils::strip_whitespace_except_after_const ( str s)

Strip all whitespace except that following "const", in which case the whitespace is replaced by a single space.

Strips all whitespaces from a string, but re-inserts a single regular space after "const".

Definition at line 123 of file util_functions.cpp.

Referenced by fix_type(), and Gambit::functor::setAllowedModelGroupCombo().

124  {
125  str tempstr("__TEMP__"), empty(""), constdec2("const ");
126  std::regex constdec1("const\\s+"), temp(tempstr), whitespace("\\s+");
127  s = std::regex_replace(s, constdec1, tempstr);
128  s = std::regex_replace(s, whitespace, empty);
129  s = std::regex_replace(s, temp, constdec2);
130  }
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
Here is the caller graph for this function:

◆ strtolower()

std::string Gambit::Utils::strtolower ( const std::string &  a)

Convert a whole string to lowercase.

Definition at line 217 of file util_functions.cpp.

References b.

218  {
219  unsigned int sz = a.size();
220  std::string b = a;
221  for (unsigned int i = 0; i < sz; ++i)
222  { b[i] = tolower(a[i]); }
223  return b;
224  }
START_MODEL b
Definition: demo.hpp:270

◆ typeEquivalencies()

type_equivalency & Gambit::Utils::typeEquivalencies ( )

Backend info accessor function.

Type equivalency accessor function.

Definition at line 25 of file equivalency_singleton.cpp.

Referenced by Gambit::add_equivrelation(), and main().

26  {
27  static type_equivalency local;
28  return local;
29  }
Here is the caller graph for this function:

◆ unwrap()

double Gambit::Utils::unwrap ( double  x,
void p 
)

Unwrapper for passing std::function to GSL integrator Based on example from https://martin-ueding.de/articles/cpp-lambda-into-gsl/index.html.

Definition at line 33 of file integration.cpp.

Referenced by integrate_cquad().

34  {
35  auto fp = static_cast<std::function<double(double)> *>(p);
36  return (*fp)(x);
37  }
Here is the caller graph for this function:

Variable Documentation

◆ buildtime_scratch

const str Gambit::Utils::buildtime_scratch = GAMBIT_DIR "/scratch/build_time/"

Return the path to the build-time scratch directory.

Definition at line 70 of file util_functions.hpp.

Referenced by Gambit::Scanner::Plugins::Plugin_Loader::Plugin_Loader().

◆ whitespaces

const char* Gambit::Utils::whitespaces[] = {" ", "\t", "\n", "\f", "\r"}

Definition at line 55 of file util_functions.cpp.