core/vul/vul_file.h
Go to the documentation of this file.
00001 // This is core/vul/vul_file.h
00002 #ifndef vul_file_h_
00003 #define vul_file_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A collection of miscellaneous filesystem-type utilities
00010 // \author Andrew W. Fitzgibbon, Oxford RRG
00011 // \date   02 Nov 98
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   981102 AWF Initial version.
00016 //   PDA (Manchester) 21/03/2001: Tidied up the documentation
00017 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00018 //   Jun.2003 - Ian Scott      - added support for '\' file separator to dos version
00019 // \endverbatim
00020 
00021 #include <vcl_string.h>
00022 #include <vxl_config.h>
00023 
00024 //: A collection of miscellaneous filesystem-type utilities
00025 //
00026 struct vul_file
00027 {
00028   //: Return current working directory
00029   static vcl_string get_cwd();
00030 
00031   //: change current working directory
00032   static bool change_directory(char const* dirname);
00033   static bool change_directory(vcl_string const& dirname) {
00034     return change_directory(dirname.c_str());
00035   }
00036 
00037   //: Make a writable directory.
00038   // You might imagine mkdir would be a better name,
00039   // and then you might imagine a world w/out ms.
00040   static bool make_directory(char const* filename);
00041   static bool make_directory(vcl_string const& filename) {
00042     return make_directory(filename.c_str());
00043   }
00044 
00045   //: Make a writable directory, including any necessary parents.
00046   // Returns true if successful, or if the directory already exists.
00047   static bool make_directory_path(char const* filename);
00048   static bool make_directory_path(vcl_string const& filename) {
00049     return make_directory_path(filename.c_str());
00050   }
00051 
00052   //: Return true iff filename is a directory.
00053   static bool is_directory(char const* filename);
00054   static bool is_directory(const vcl_string& filename) {
00055     return is_directory(filename.c_str());
00056   }
00057 
00058 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00059   //: Return true iff filename is a drive, e.g., "c:" or "Z:".
00060   static bool is_drive(char const* filename);
00061   static bool is_drive(const vcl_string& filename) {
00062     return is_drive(filename.c_str());
00063   }
00064 #endif
00065 
00066   //: Expand any leading ~ escapes in filename
00067   static vcl_string expand_tilde(char const* filename);
00068   static vcl_string expand_tilde(vcl_string const& filename) {
00069     return expand_tilde(filename.c_str());
00070   }
00071 
00072   //: Return true iff filename exists.  It may be any sort of file.
00073   static bool exists(char const* filename);
00074   static bool exists(vcl_string const& filename) {
00075     return exists(filename.c_str());
00076   }
00077 
00078   //: Return size of vul_file
00079   static unsigned long size(char const* filename);
00080   static unsigned long size(vcl_string filename) { return size(filename.c_str()); }
00081 
00082   //: Return dirname
00083   static vcl_string dirname(char const* filename);
00084   static vcl_string dirname(vcl_string const& filename) {
00085     return dirname(filename.c_str());
00086   }
00087 
00088   //: Return extension (including the '.').
00089   static vcl_string extension(char const* filename);
00090   static vcl_string extension(vcl_string const& filename) {
00091     return extension( filename.c_str() );
00092   }
00093 
00094   //: Return basename
00095   // Only strip specified suffix.
00096   static vcl_string basename(char const* filename, char const* suffix = 0);
00097   static vcl_string basename(vcl_string const& filename, char const* suffix = 0) {
00098     return basename(filename.c_str(), suffix );
00099   }
00100 
00101   //: Strips away directory of the filename
00102   static vcl_string strip_directory(char const* filename);
00103   static vcl_string strip_directory(vcl_string const &filename)
00104   { return strip_directory(filename.c_str()); }
00105 
00106   //: Strips away extension of the filename
00107   static vcl_string strip_extension(char const* filename);
00108   static vcl_string strip_extension(vcl_string const &filename)
00109   { return strip_extension(filename.c_str()); }
00110 
00111   //: Delete 1 or more files using the Local OS preferred globbing.
00112   // E.g. \c delete_file_glob("*"); will delete all the files in the
00113   // current directory on most operating systems.
00114   // \return true if successful.
00115   static bool delete_file_glob(vcl_string const& file_glob);
00116   static bool delete_file_glob(char const* file_glob)
00117   { return delete_file_glob(vcl_string(file_glob)); }
00118 
00119 
00120 #if defined(VCL_WIN32) && VXL_USE_WIN_WCHAR_T
00121 
00122   //: Return current working directory
00123   //  This function is provided as an overloading
00124   static vcl_string get_cwd(char* /*dummy*/)
00125   { return get_cwd(); }
00126 
00127   //: Return current working directory
00128   static std::wstring get_cwd(wchar_t* dummy);
00129 
00130   //: change current working directory
00131   static bool change_directory(wchar_t const* dirname);
00132   static bool change_directory(std::wstring const& dirname) {
00133     return change_directory(dirname.c_str());
00134   }
00135 
00136   //: Make a writable directory.
00137   // You might imagine mkdir would be a better name,
00138   // and then you might imagine a world w/out ms.
00139   static bool make_directory(wchar_t const* filename);
00140   static bool make_directory(std::wstring const& filename) {
00141     return make_directory(filename.c_str());
00142   }
00143 
00144   //: Make a writable directory, including any necessary parents.
00145   // Returns true if successful, or if the directory already exists.
00146   static bool make_directory_path(wchar_t const* filename);
00147   static bool make_directory_path(std::wstring const& filename) {
00148     return make_directory_path(filename.c_str());
00149   }
00150 
00151   //: Return true iff filename is a directory.
00152   static bool is_directory(wchar_t const* filename);
00153   static bool is_directory(const std::wstring& filename) {
00154     return is_directory(filename.c_str());
00155   }
00156 
00157   //: Return true iff filename is a drive, e.g., "c:" or "Z:".
00158   static bool is_drive(wchar_t const* filename);
00159   static bool is_drive(const std::wstring& filename) {
00160     return is_drive(filename.c_str());
00161   }
00162 
00163   ////: Expand any leading ~ escapes in filename
00164   static std::wstring expand_tilde(wchar_t const* filename) {
00165     // ~ meaningless on win32
00166     return std::wstring(filename);
00167   }
00168   static std::wstring expand_tilde(std::wstring const& filename) {
00169     // ~ meaningless on win32
00170     return filename;
00171   }
00172 
00173   //: Return true iff filename exists.  It may be any sort of file.
00174   static bool exists(wchar_t const* filename);
00175   static bool exists(std::wstring const& filename) {
00176     return exists(filename.c_str());
00177   }
00178 
00179   ////: Return size of vul_file
00180   //static unsigned long size(wchar_t const* filename);
00181   //static unsigned long size(std::wstring filename) { return size(filename.c_str()); }
00182 
00183   //: Return dirname
00184   static std::wstring dirname(wchar_t const* filename);
00185   static std::wstring dirname(std::wstring const& filename) {
00186     return dirname(filename.c_str());
00187   }
00188 
00189   //: Return extension (including the '.').
00190   static std::wstring extension(wchar_t const* filename);
00191   static std::wstring extension(std::wstring const& filename) {
00192     return extension( filename.c_str() );
00193   }
00194 
00195   //: Return basename
00196   static std::wstring basename(wchar_t const* filename, wchar_t const* suffix = 0);
00197   static std::wstring basename(std::wstring const& filename, wchar_t const* suffix = 0) {
00198     return basename(filename.c_str(), suffix );
00199   }
00200 
00201   //: Strips away directory of the filename
00202   static std::wstring strip_directory(wchar_t const* filename);
00203   static std::wstring strip_directory(std::wstring const &filename)
00204   { return strip_directory(filename.c_str()); }
00205 
00206   //: Strips away extension of the filename
00207   static std::wstring strip_extension(wchar_t const* filename);
00208   static std::wstring strip_extension(std::wstring const &filename)
00209   { return strip_extension(filename.c_str()); }
00210 
00211 #endif
00212 };
00213 
00214 inline bool vul_file_exists(char const *f) { return vul_file::exists(f); }
00215 inline bool vul_file_exists(vcl_string  f) { return vul_file::exists(f); }
00216 
00217 inline bool vul_file_is_directory(char const *f) { return vul_file::is_directory(f); }
00218 inline bool vul_file_is_directory(vcl_string  f) { return vul_file::is_directory(f); }
00219 
00220 inline unsigned long vul_file_size(char const *f) { return vul_file::size(f); }
00221 inline unsigned long vul_file_size(vcl_string  f) { return vul_file::size(f); }
00222 
00223 inline vcl_string vul_file_extension(char const *f) { return vul_file::extension(f); }
00224 inline vcl_string vul_file_extension(vcl_string  f) { return vul_file_extension(f.c_str()); }
00225 
00226 #endif // vul_file_h_