SystemUtils.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5  or see $GENIE/LICENSE
6 
7  Author: Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
8  University of Liverpool & STFC Rutherford Appleton Lab
9 
10  For documentation see the corresponding header file.
11 
12  Important revisions after version 2.0.0 :
13  @ Oct 08, 2009 - CA
14  That file was added in 2.5.1
15  @ Apr 20, 2012 - CA
16  Added LocalTimeAsString(string format) to tag validation program outputs.
17 
18 */
19 //____________________________________________________________________________
20 
21 #include <cstdlib>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 #include <dirent.h>
26 #include <ctime>
27 
28 #include <TSystem.h>
29 
30 
34 
35 using std::atoi;
36 
37 //___________________________________________________________________________
38 vector<string>
40 {
41  vector<string> files;
42 
43  DIR *dp = NULL;
44  struct dirent *dirp = NULL;
45  if((dp = opendir(path.c_str())) == NULL) {
46  LOG("System", pERROR) << "Can not open directory: " << path;
47  }
48  else {
49  while ((dirp = readdir(dp)) != NULL) {
50  string filename = path + "/" + string(dirp->d_name);
51  bool match = false;
52  if(extension.size()==0) match=true; // get all files
53  else {
54  // match extension
55  vector<string> filenamev = genie::utils::str::Split(filename,".");
56  if(filenamev.size()==0) break;
57  string file_extension = filenamev[filenamev.size()-1];
58  match = (file_extension.find(extension) != string::npos);
59  }
60  if(match) {
61  files.push_back(filename);
62  }
63  }
64  closedir(dp);
65  }
66  return files;
67 }
68 //___________________________________________________________________________
70 {
71  vector<string> vrs = utils::str::Split(tag,".");
72  assert(vrs.size() == 3);
73  return atoi(vrs[0].c_str());
74 }
75 //___________________________________________________________________________
77 {
78  vector<string> vrs = utils::str::Split(tag,".");
79  assert(vrs.size() == 3);
80  return atoi(vrs[1].c_str());
81 }
82 //___________________________________________________________________________
84 {
85  vector<string> vrs = utils::str::Split(tag,".");
86  assert(vrs.size() == 3);
87  return atoi(vrs[2].c_str());
88 }
89 //___________________________________________________________________________
91 {
92  if(filename.size() == 0) return false;
93 
94  bool is_accessible = ! (gSystem->AccessPathName(filename.c_str()));
95  if (is_accessible) return true;
96 
97  return false;
98 }
99 
100 //___________________________________________________________________________
101 
103 
104  struct stat info;
105 
106  if( stat( path, &info ) != 0 ) {
107  return false ;
108  } else if(info.st_mode & S_IFDIR) {
109  return true ;
110  } else {
111  return false ;
112  }
113 }
114 
115 
116 //___________________________________________________________________________
118 {
119  time_t now = time(0);
120  tm* local_time = localtime(&now);
121 
122  int yr = local_time->tm_year + 1900;
123  int mon = local_time->tm_mon + 1;
124  int day = local_time->tm_mday;
125  int hr = local_time->tm_hour + 1;
126  int min = local_time->tm_min;
127  int sec = local_time->tm_sec;
128 
129  // daylight saving
130  if(local_time->tm_isdst > 0)
131  {
132  if(hr > 0) {
133  hr--;
134  }
135  else
136  if(hr == 0) {
137  hr = 23;
138  if(day > 1) {
139  day--;
140  }
141  else {
142  mon--;
143  if(mon == 1 || mon == 3 || mon == 5 ||
144  mon == 7 || mon == 8 || mon == 10 || mon == 12)
145  {
146  day = 31;
147  }
148  else
149  if(mon == 2)
150  {
151  day = 28;
152  }
153  else
154  {
155  day = 30;
156  }
157  }
158  }
159  }
160 
161  string local_time_as_string =
162  Form(format.c_str(),yr,mon,day,hr,min,sec);
163 
164  return local_time_as_string;
165 }
166 //___________________________________________________________________________
int GenieMajorVrsNum(string tag)
Definition: SystemUtils.cxx:69
#define pERROR
Definition: Messenger.h:60
bool FileExists(string filename)
Definition: SystemUtils.cxx:90
vector< string > GetAllFilesInPath(string path, string extension="")
Definition: SystemUtils.cxx:39
string filename
Definition: shutoffs.py:106
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
int GenieMinorVrsNum(string tag)
Definition: SystemUtils.cxx:76
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:97
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
const std::string path
Definition: plot_BEN.C:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:42
bool DirectoryExists(const char *path)
int GenieRevisVrsNum(string tag)
Definition: SystemUtils.cxx:83
assert(nhit_max >=nhit_nbins)