Pol  Revision:cb584c9
fileutil.cpp
Go to the documentation of this file.
1 
7 #include "fileutil.h"
8 
9 #include <limits.h>
10 #include <sys/stat.h>
11 
12 #include "dirlist.h"
13 
14 #ifdef LINUX
15 #include <unistd.h>
16 #else
17 #include <direct.h>
18 #include <io.h>
19 #endif
20 
21 namespace Pol
22 {
23 namespace Clib
24 {
25 std::string normalized_dir_form( const std::string& istr )
26 {
27  std::string str = istr;
28 
29  {
30  std::string::size_type bslashpos;
31  while ( std::string::npos != ( bslashpos = str.find( '\\' ) ) )
32  {
33  str.replace( bslashpos, 1, 1, '/' );
34  }
35  }
36 
37  if ( str.size() == 0 )
38  {
39  return "/";
40  }
41  else if ( str[str.size() - 1] == '/' || str[str.size() - 1] == '\\' )
42  {
43  return str;
44  }
45  else
46  {
47  return str + "/";
48  }
49 }
50 
51 bool IsDirectory( const char* dir )
52 {
53  std::string sdir( dir );
54  if ( sdir[sdir.length() - 1] == '/' )
55  sdir = sdir.erase( sdir.length() - 1, 1 );
56  struct stat st;
57  if ( stat( sdir.c_str(), &st ) )
58  return 0;
59  return ( st.st_mode & S_IFDIR ? true : false );
60 }
61 
62 void MakeDirectory( const char* dir )
63 {
64 #ifdef __unix__
65  mkdir( dir, 0777 );
66 #else
67  mkdir( dir );
68 #endif
69 }
70 
71 int strip_one( std::string& direc )
72 {
73  std::string::size_type pos = direc.find_last_of( "\\/" );
74  if ( pos == std::string::npos )
75  return -1;
76  if ( pos >= 1 && direc[pos - 1] == ':' ) // at "C:\"
77  return -1;
78  direc = direc.substr( 0, pos );
79  return 0;
80 }
81 
82 int make_dir( const char* dir )
83 {
84  if ( access( dir, 0 ) )
85  {
86 #ifdef _WIN32
87  if ( CreateDirectory( dir, NULL ) ) // why is windows too good for POSIX?
88 #else
89  if ( mkdir( dir, 0777 ) == 0 )
90 #endif
91  {
92  return 0; /* made it okay */
93  }
94  else
95  {
96  // if didn't make it,
97  std::string parent_dir = dir;
98  if ( strip_one( parent_dir ) )
99  return -1;
100  if ( make_dir( parent_dir.c_str() ) )
101  return -1;
102 #ifdef _WIN32
103  if ( CreateDirectory( dir, NULL ) )
104  return 0;
105 #else
106  if ( mkdir( dir, 0777 ) == 0 )
107  return 0;
108 #endif
109  // this test is mostly for the case where the path is in normalized form
110  if ( access( dir, 0 ) == 0 )
111  return 0;
112  return -1;
113  }
114  }
115  return 0;
116 }
117 
118 bool FileExists( const char* filename )
119 {
120  return ( access( filename, 0 ) == 0 );
121 }
122 bool FileExists( const std::string& filename )
123 {
124  return ( access( filename.c_str(), 0 ) == 0 );
125 }
126 
127 int filesize( const char* fname )
128 {
129  struct stat st;
130  if ( stat( fname, &st ) )
131  return 0;
132  return st.st_size;
133 }
134 
135 unsigned int GetFileTimestamp( const char* fname )
136 {
137  struct stat st;
138  if ( stat( fname, &st ) )
139  return 0;
140  return (unsigned int)st.st_mtime;
141 }
142 
143 void RemoveFile( const std::string& fname )
144 {
145  unlink( fname.c_str() );
146 }
147 
148 std::string FullPath( const char* filename )
149 {
150 #ifdef __unix__
151  char tmp[PATH_MAX];
152  if ( realpath( filename, tmp ) )
153  return tmp;
154  else
155  return "";
156 #else
157  char p[1025];
158  _fullpath( p, filename, sizeof p );
159  return p;
160 #endif
161 }
162 
163 std::string GetTrueName( const char* filename )
164 {
165  DirList dl;
166  dl.open( filename );
167  if ( !dl.at_end() )
168  {
169  return dl.name();
170  }
171  else
172  return filename;
173 }
174 
175 std::string GetFilePart( const char* filename )
176 {
177  std::string fn( filename );
178  std::string::size_type lastslash = fn.find_last_of( "\\/" );
179  if ( lastslash == std::string::npos )
180  return filename;
181  else
182  return fn.substr( lastslash + 1 );
183 }
184 }
185 }
std::string FullPath(const char *filename)
Definition: fileutil.cpp:148
void MakeDirectory(const char *dir)
Definition: fileutil.cpp:62
int strip_one(std::string &direc)
Definition: fileutil.cpp:71
unsigned int GetFileTimestamp(const char *fname)
Definition: fileutil.cpp:135
std::string name() const
Definition: dirlist.cpp:107
bool at_end() const
Definition: dirlist.cpp:102
std::string GetFilePart(const char *filename)
Definition: fileutil.cpp:175
void open(const char *filespec)
Definition: dirlist.cpp:98
bool IsDirectory(const char *dir)
Definition: fileutil.cpp:51
int filesize(const char *fname)
Definition: fileutil.cpp:127
std::string GetTrueName(const char *filename)
Definition: fileutil.cpp:163
void RemoveFile(const std::string &fname)
Definition: fileutil.cpp:143
std::string normalized_dir_form(const std::string &istr)
Definition: fileutil.cpp:25
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
Definition: berror.cpp:12
int make_dir(const char *dir)
Definition: fileutil.cpp:82