TF++, Miscellaneous classes and modules in C++:
filestatus.cc
Go to the documentation of this file.
1 
36 #define TF_FILESTATUS_CC_VERSION \
37  "TF_FILESTATUS_CC V1.1"
38 
39 #include<sstream>
40 #include<string>
41 #include<cstdlib>
42 #include <tfxx/filestatus.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <libgen.h>
47 #include <string.h>
48 
49 namespace tfxx {
50 
51  namespace file {
52 
56  bool creatable(const char* path)
57  {
58  char *thepath=strdup(path);
59  std::string dir(dirname(thepath));
60  bool retval=(access(dir.c_str(), W_OK)==0);
61  free(thepath);
62  return(retval);
63  } // bool creatable(const char* path)
64 
65  /*----------------------------------------------------------------------*/
66 
71  bool exists(const char* path)
72  {
73  struct stat status;
74  bool retval=(stat(path, &status)==0);
75  return(retval);
76  } // bool exists(const char* path)
77 
78  /*----------------------------------------------------------------------*/
79 
82  bool writable(const char* path)
83  {
84  bool retval=(access(path, W_OK)==0);
85  return(retval);
86  } // bool writable(const char* path)
87 
88  /*----------------------------------------------------------------------*/
89 
92  bool readable(const char* path)
93  {
94  bool retval=false;
95  if (exists(path)) { retval=(access(path, R_OK)==0); }
96  return(retval);
97  } // bool readable(const char* path)
98 
99  /*----------------------------------------------------------------------*/
100 
103  bool regular(const char* path)
104  {
105  struct stat status;
106  bool retval=false;
107  if (stat(path, &status)==0) { retval=S_ISREG(status.st_mode); }
108  return(retval);
109  } // bool regular(const char* path)
110 
111  /*----------------------------------------------------------------------*/
112 
113  std::string uniquenew(const char* path)
114  {
115  std::string retval(path);
116  int i=0;
117  while (exists(retval.c_str()))
118  {
119  ++i;
120  std::ostringstream oss;
121  oss << path << "_" << i;
122  retval=oss.str();
123  }
124  return(retval);
125  } // std::string unique(const char* path)
126 
127  } // namespace file
128 
129 } // namespace tfxx
130 
131 /* ----- END OF filestatus.cc ----- */
bool writable(const char *path)
Definition: filestatus.cc:82
bool regular(const char *path)
Definition: filestatus.cc:103
bool creatable(const char *path)
Definition: filestatus.cc:56
std::string dirname(const std::string &path)
return path with its last non-slash component removed
Definition: dirname.cc:45
std::string uniquenew(const char *path)
Definition: filestatus.cc:113
bool readable(const char *path)
Definition: filestatus.cc:92
bool exists(const char *path)
Definition: filestatus.cc:71
Namespace containing all code of library libtfxx.