GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SystemUtils.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <cstdlib>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 
15 #include <dirent.h>
16 #include <ctime>
17 
18 #include <TSystem.h>
19 
20 
24 
25 using std::atoi;
26 
27 //___________________________________________________________________________
28 vector<string>
29  genie::utils::system::GetAllFilesInPath(string path, string extension)
30 {
31  vector<string> files;
32 
33  DIR *dp = NULL;
34  struct dirent *dirp = NULL;
35  if((dp = opendir(path.c_str())) == NULL) {
36  LOG("System", pERROR) << "Can not open directory: " << path;
37  }
38  else {
39  while ((dirp = readdir(dp)) != NULL) {
40  string filename = path + "/" + string(dirp->d_name);
41  bool match = false;
42  if(extension.size()==0) match=true; // get all files
43  else {
44  // match extension
45  vector<string> filenamev = genie::utils::str::Split(filename,".");
46  if(filenamev.size()==0) break;
47  string file_extension = filenamev[filenamev.size()-1];
48  match = (file_extension.find(extension) != string::npos);
49  }
50  if(match) {
51  files.push_back(filename);
52  }
53  }
54  closedir(dp);
55  }
56  return files;
57 }
58 //___________________________________________________________________________
60 {
61  vector<string> vrs = utils::str::Split(tag,".");
62  assert(vrs.size() == 3);
63  return atoi(vrs[0].c_str());
64 }
65 //___________________________________________________________________________
67 {
68  vector<string> vrs = utils::str::Split(tag,".");
69  assert(vrs.size() == 3);
70  return atoi(vrs[1].c_str());
71 }
72 //___________________________________________________________________________
74 {
75  vector<string> vrs = utils::str::Split(tag,".");
76  assert(vrs.size() == 3);
77  return atoi(vrs[2].c_str());
78 }
79 //___________________________________________________________________________
80 bool genie::utils::system::FileExists(string filename)
81 {
82  if(filename.size() == 0) return false;
83 
84  bool is_accessible = ! (gSystem->AccessPathName(filename.c_str()));
85  if (is_accessible) return true;
86 
87  return false;
88 }
89 
90 //___________________________________________________________________________
91 
92 bool genie::utils::system::DirectoryExists( const char * path ) {
93 
94  struct stat info;
95 
96  if( stat( path, &info ) != 0 ) {
97  return false ;
98  } else if(info.st_mode & S_IFDIR) {
99  return true ;
100  } else {
101  return false ;
102  }
103 }
104 
105 
106 //___________________________________________________________________________
108 {
109  time_t now = time(0);
110  tm* local_time = localtime(&now);
111 
112  int yr = local_time->tm_year + 1900;
113  int mon = local_time->tm_mon + 1;
114  int day = local_time->tm_mday;
115  int hr = local_time->tm_hour + 1;
116  int min = local_time->tm_min;
117  int sec = local_time->tm_sec;
118 
119  // daylight saving
120  if(local_time->tm_isdst > 0)
121  {
122  if(hr > 0) {
123  hr--;
124  }
125  else
126  if(hr == 0) {
127  hr = 23;
128  if(day > 1) {
129  day--;
130  }
131  else {
132  mon--;
133  if(mon == 1 || mon == 3 || mon == 5 ||
134  mon == 7 || mon == 8 || mon == 10 || mon == 12)
135  {
136  day = 31;
137  }
138  else
139  if(mon == 2)
140  {
141  day = 28;
142  }
143  else
144  {
145  day = 30;
146  }
147  }
148  }
149  }
150 
151  string local_time_as_string =
152  Form(format.c_str(),yr,mon,day,hr,min,sec);
153 
154  return local_time_as_string;
155 }
156 //___________________________________________________________________________
int GenieMajorVrsNum(string tag)
Definition: SystemUtils.cxx:59
#define pERROR
Definition: Messenger.h:59
bool FileExists(string filename)
Definition: SystemUtils.cxx:80
vector< string > GetAllFilesInPath(string path, string extension="")
Definition: SystemUtils.cxx:29
int GenieMinorVrsNum(string tag)
Definition: SystemUtils.cxx:66
string LocalTimeAsString(string format)
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
bool DirectoryExists(const char *path)
Definition: SystemUtils.cxx:92
int GenieRevisVrsNum(string tag)
Definition: SystemUtils.cxx:73