Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TRAIL_UnixUtils.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 #include <cstdio>
27 #include <sstream>
28 
29 #include "TRAIL_UnixUtils.H"
30 
31 void
32 TRAIL_SafeRemove(const std::string &fname,const std::string &ext)
33 {
34  if(!TRAIL_FILEEXISTS(fname))
35  return;
36  if(TRAIL_ISLINK(fname))
37  unlink(fname.c_str());
38  std::string savename_base(fname+"."+ext);
39  std::string savename(savename_base);
40  unsigned int n = 1;
41  while(TRAIL_FILEEXISTS(savename)){
42  std::ostringstream Ostr;
43  Ostr << savename_base << n++;
44  savename.assign(Ostr.str());
45  }
46  rename(fname.c_str(),savename.c_str());
47 }
48 
49 bool
50 TRAIL_FILEEXISTS(const std::string &fname)
51 {
52  struct stat fstat;
53  if(lstat(fname.c_str(),&fstat))
54  return false;
55  return true;
56 }
57 
58 bool
59 TRAIL_ISDIR(const std::string &fname)
60 {
61  struct stat fstat;
62  if(stat(fname.c_str(),&fstat))
63  return false;
64  if(S_ISDIR(fstat.st_mode))
65  return true;
66  return false;
67 }
68 
69 bool
70 TRAIL_ISLINK(const std::string &fname)
71 {
72  struct stat fstat;
73  if(lstat(fname.c_str(),&fstat))
74  return false;
75  if(S_ISLNK(fstat.st_mode))
76  return true;
77  return(false);
78 }
79 
80 int
81 TRAIL_CreateDirectory(const std::string &fname)
82 {
83  return(mkdir(fname.c_str(),S_IRGRP | S_IXGRP | S_IRWXU));
84 }
85 
86 std::string
87 TRAIL_ResolveLink(const std::string &path)
88 {
89  std::string retVal;
90  char buf[1024];
91  size_t s = readlink(path.c_str(),buf,1024);
92  if(!(s <= 0)){
93  buf[s] = '\0';
94  retVal.assign(buf);
95  }
96  std::string::size_type x = retVal.find_last_of("/");
97  if(x != std::string::npos)
98  retVal.erase(x);
99  return (retVal);
100 }
101 
102 std::string
103 ResolveLink(const std::string &path)
104 {
105  std::string retVal;
106  char buf[1024];
107  size_t s = readlink(path.c_str(),buf,1024);
108  if(!(s <= 0)){
109  buf[s] = '\0';
110  retVal.assign(buf);
111  }
112  std::string::size_type x = retVal.find_last_of("/");
113  if(x != std::string::npos)
114  retVal.erase(x);
115  return (retVal);
116 }
117 
118 Directory::Directory(const std::string &path)
119 {
120  _good = false;
121  _dir = NULL;
122  _path.assign(path);
123  if(open(path))
124  std::cerr << "Directory::Error: Could not open " << path
125  << " as a directory." << std::endl;
126 }
127 
129 {
130  if (_good)
131  closedir(_dir);
132 }
133 
134 Directory::operator void* ()
135 {
136  return(_good ? this : NULL);
137 }
138 
139 bool
141 {
142  return(!_good);
143 }
144 
145 void
147 {
148  if(_good)
149  closedir(_dir);
150 }
151 
152 int
153 Directory::open(const std::string &path)
154 {
155  if(_good){
156  this->close();
157  _path = path;
158  }
159  if(path.empty())
160  return(1);
161  if(!(_dir = opendir(path.c_str())))
162  return(1);
163  _path = path;
164  _good = true;
165  struct dirent *entry;
166  // Skip . and ..
167  entry = readdir(_dir);
168  entry = readdir(_dir);
169  while((entry = readdir(_dir)) != NULL)
170  this->push_back(entry->d_name);
171  return(0);
172 }
173 
174 std::string TRAIL_CWD(void)
175 {
176  char buf[1024];
177  return(std::string(getcwd(buf,1024)));
178 }
179 
180 int
181 TRAIL_CD(const std::string &path,std::ostream *ouf)
182 {
183  if(ouf)
184  *ouf << "TRAIL_CD: Switching directory from "
185  << TRAIL_CWD() << " to " << path << std::endl;
186  int result = chdir(path.c_str());
187  if(ouf)
188  *ouf << "TRAIL_CD: CWD(" << TRAIL_CWD() << ")" << std::endl;
189  return(result);
190 
191 }
192 
193 
194 
195 
196 
197 
bool TRAIL_FILEEXISTS(const std::string &fname)
DIR * _dir
Definition: Directory.H:31
double s
Definition: blastest.C:80
bool operator!()
Definition: Directory.C:56
~Directory()
Definition: Directory.C:45
int TRAIL_CD(const std::string &path, std::ostream *=NULL)
std::string TRAIL_ResolveLink(const std::string &path)
bool TRAIL_ISDIR(const std::string &fname)
int open(const std::string &s="")
Definition: Directory.C:69
void TRAIL_SafeRemove(const std::string &fname, const std::string &ext)
void close()
Definition: Directory.C:62
Directory(const std::string &s="")
Definition: Directory.C:31
std::string TRAIL_CWD(void)
void int int REAL * x
Definition: read.cpp:74
std::string _path
Definition: Directory.H:29
const NT & n
bool _good
Definition: Directory.H:30
int TRAIL_CreateDirectory(const std::string &fname)
bool TRAIL_ISLINK(const std::string &fname)
std::string ResolveLink(const std::string &path)