Codemesh Runtime v3 C++ API Reference  3.9.205
xmog_util_file.h
1 //
2 // Copyright (c) 1999-2020 by Codemesh, Inc.. ALL RIGHTS RESERVED.
3 //
4 
5 #ifndef XMOG_UTIL_FILE_INC
6 #define XMOG_UTIL_FILE_INC
7 
8 
9 #include <sys/stat.h>
10 #include <string>
11 #include <vector>
12 #include "xmog.h"
13 
14 
15 #if (XMOG_WINDOWS==1)
16 # define XMOG_CASE_SENSITIVITY_DEFAULT false
17 #else
18 # define XMOG_CASE_SENSITIVITY_DEFAULT true
19 #endif
20 
21 
22 namespace xmog
23 {
24  namespace util
25  {
28  enum file_spec
29  {
31  onlyFile,
33  onlyDir,
35  fileAndDir
36  };
37 
39  class XMOG_DECLSPEC filename_matcher
40  {
41  public:
42 
43  filename_matcher( const char * pattern );
44 
45  bool matches( const char * name, bool bCaseSensitive = XMOG_CASE_SENSITIVITY_DEFAULT ) const;
46 
47  private:
48 
49  const char * pattern;
50  const char * patLast;
51  };
52 
54  class XMOG_DECLSPEC file
55  {
56  public:
57 
58  static const char * noTerminators[];
59 
63  file( const char * name_ );
64 
68  file( const std::string & name_ );
69 
74  file( const file & parent_, const char * const name_ );
75 
76  ~file();
77 
79  inline operator const std::string & () const
80  {
81  return mName;
82  }
83 
86  inline bool operator == ( const std::string & name ) const
87  {
88  return mName == name;
89  }
90 
92  inline const char * name() const
93  {
94  return mName.c_str();
95  }
96 
98  inline const char * key_name() const
99  {
100  return mKeyName.c_str();
101  }
102 
104  inline file nested( const char * sub ) const
105  {
106  if( sub[ 0 ] == '\\' || sub[ 0 ] == '/' )
107  return file( (mName + sub).c_str() );
108  else
109  return file( (mName + "/" + sub).c_str() );
110  }
111 
112 
114  inline bool exists() const
115  {
116  return status == 0;
117  }
118 
120  inline off_t size() const
121  {
122  return stats.st_size;
123  }
124 
125 
127  inline bool is_dir() const
128  {
129 # if(XMOG_WINDOWS==1)
130  return exists() && ( stats.st_mode & _S_IFDIR ) != 0;
131 # else
132  return exists() && ( stats.st_mode & S_IFDIR ) != 0;
133 # endif
134  }
135 
137  inline bool is_file() const
138  {
139 # if(XMOG_WINDOWS==1)
140  return exists() && ( stats.st_mode & _S_IFREG ) != 0;
141 # else
142  return exists() && ( stats.st_mode & S_IFREG ) != 0;
143 # endif
144  };
145 
153  size_t list( ::std::vector<file> & container, const char * filter = NULL, file_spec fs = fileAndDir, bool bRecurse = false, const char * terms[] = noTerminators ) const;
154 
160  std::string path( const char * filter, file_spec fs = onlyFile ) const;
161 
167  int read( char ** pdata, jint * length );
168 
172  int write( const char * text );
173 
176  int mkdir( int mode = 0777 );
177 
179  void del();
180 
181  private:
182 
183  void init( const char * name_ );
184 
185 # if (XMOG_IS_MSVC==1)
186 # pragma warning( push )
187 # pragma warning( disable: 4251 )
188 # endif
189  ::std::string mName;
190  ::std::string mKeyName;
191 # if (XMOG_IS_MSVC==1)
192 # pragma warning( pop )
193 # endif
194  struct stat stats;
195  int status;
196 
197  };
198  }
199 }
200 
201 
202 #endif // XMOG_UTIL_FILE_INC￿
xmog::util::file::nested
file nested(const char *sub) const
Return the file object representing a nested file, relative to this file.
Definition: xmog_util_file.h:104
xmog::util::filename_matcher
Utility type for locating files based on a pattern.
Definition: xmog_util_file.h:39
xmog::util::file::exists
bool exists() const
Return true if this instance represents an existing file.
Definition: xmog_util_file.h:114
xmog::util::file::is_file
bool is_file() const
Return true if this instance represents a regular file.
Definition: xmog_util_file.h:137
xmog::util::file::size
off_t size() const
Return the number of bytes in the file.
Definition: xmog_util_file.h:120
xmog::util::file::key_name
const char * key_name() const
Return the file's name.
Definition: xmog_util_file.h:98
xmog::util::file::name
const char * name() const
Return the file's name.
Definition: xmog_util_file.h:92
xmog::util::file
Utility wrapper for file objects.
Definition: xmog_util_file.h:54
xmog::util::file::is_dir
bool is_dir() const
Return true if this instance represents a directory.
Definition: xmog_util_file.h:127

Copyright (c) 1999-2020 by Codemesh, Inc., ALL RIGHTS RESERVED.