os.h
Go to the documentation of this file.
00001 /** 00002 * @file os.h 00003 * @brief Operating system dependent functions. 00004 * 00005 * Copyright (c) 2011, 2012 University of Pennsylvania. All rights reserved.<br /> 00006 * See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file. 00007 * 00008 * Contact: SBIA Group <sbia-software at uphs.upenn.edu> 00009 */ 00010 00011 #pragma once 00012 #ifndef _BASIS_OS_H 00013 #define _BASIS_OS_H 00014 00015 00016 #include <string> 00017 00018 #include "os/path.h" 00019 00020 00021 /// @addtogroup BasisCxxUtilities 00022 /// @{ 00023 00024 00025 namespace basis { namespace os { 00026 00027 00028 /** 00029 * @brief Get absolute path of the (current) working directory. 00030 * 00031 * @return Absolute path of working directory or empty string on error. 00032 */ 00033 std::string getcwd(); 00034 00035 /** 00036 * @brief Get canonical path of executable file. 00037 * 00038 * @return Canonical path of executable file or empty string on error. 00039 * 00040 * @sa exedir() 00041 * @sa exename() 00042 */ 00043 std::string exepath(); 00044 00045 /** 00046 * @brief Get name of executable. 00047 * 00048 * @note The name of the executable may or may not include the file name 00049 * extension depending on the executable type and operating system. 00050 * Hence, this function is neither an equivalent to 00051 * path::basename(exepath()) nor path::filename(exepath()). 00052 * In particular, on Windows, the .exe and .com extension is not 00053 * included in the returned executable name. 00054 * 00055 * @return Name of the executable derived from the executable's file path 00056 * or empty string on error. 00057 * 00058 * @sa exepath() 00059 * @sa exedir() 00060 */ 00061 std::string exename(); 00062 00063 /** 00064 * @brief Get canonical path of directory containing executable file. 00065 * 00066 * @return Canonical path of directory containing executable file. 00067 * 00068 * @sa exepath() 00069 * @sa exename() 00070 */ 00071 std::string exedir(); 00072 00073 /** 00074 * @brief Read value of symbolic link. 00075 * 00076 * @param [in] path Path of symbolic link. 00077 * 00078 * @returns Value of symbolic link. Can be relative or absolute path. If the 00079 * link could not be read, an empty string is returned. Note that 00080 * on Windows, this function always returns an empty string. 00081 * 00082 * @sa realpath() 00083 */ 00084 std::string readlink(const std::string& path); 00085 00086 /** 00087 * @brief Make directory. 00088 * 00089 * @note The parent directory must exist already. See makedirs() for a function 00090 * that also creates the parent directories if none-existent. 00091 * 00092 * @param path Path of the directory. 00093 * 00094 * @returns Whether the directory was created successfully. 00095 * Note that on Posix, the created directory will have mode 0755. 00096 * On Windows, the default security descriptor is passed on to the 00097 * CreateDirectory() function. 00098 * 00099 * @sa makedirs() 00100 */ 00101 bool mkdir(const std::string& path); 00102 00103 /** 00104 * @brief Make directory including parent directories if required. 00105 * 00106 * @param path Path of the directory. 00107 * 00108 * @returns Whether the directory was created successfully. 00109 * Note that on Posix, the created directories will have mode 0755. 00110 * On Windows, the default security descriptor is passed on to the 00111 * CreateDirectory() function. 00112 */ 00113 bool makedirs(const std::string& path); 00114 00115 /** 00116 * @brief Remove empty directory. 00117 * 00118 * This function removes an empty directory. If the directory is not empty, 00119 * it will fail. See rmtree() for a function that also removes the files and 00120 * recursively the directories within the specified directory. 00121 * 00122 * @param path Path of the directory. 00123 * 00124 * @returns Whether the directory was removed successfully. 00125 * 00126 * @sa rmtree() 00127 */ 00128 bool rmdir(const std::string& path); 00129 00130 /** 00131 * @brief Remove whole directory tree. 00132 * 00133 * @param path Path of the directory. 00134 * 00135 * @returns Whether the directory was removed successfully. 00136 */ 00137 bool rmtree(const std::string& path); 00138 00139 /** 00140 * @brief Remove files and directories from directory. 00141 * 00142 * @param path Path of the directory. 00143 * 00144 * @returns Whether the directory was cleared successfully, i.e., leaving 00145 * the directory @p path empty. 00146 */ 00147 bool emptydir(const std::string& path); 00148 00149 00150 } // namespace os 00151 00152 } // namespace basis 00153 00154 00155 /// @} 00156 // Doxygen group 00157 00158 #endif // _BASIS_OS_H