os.h
Go to the documentation of this file.
1 // ===========================================================================
2 // Copyright (c) 2011-2012 University of Pennsylvania
3 // Copyright (c) 2013-2016 Andreas Schuh
4 // All rights reserved.
5 //
6 // See COPYING file for license information or visit
7 // https://cmake-basis.github.io/download.html#license
8 // ===========================================================================
9 
10 /**
11  * @file os.h
12  * @brief Operating system dependent functions.
13  */
14 
15 #pragma once
16 #ifndef _BASIS_OS_H
17 #define _BASIS_OS_H
18 
19 
20 #include <string>
21 
22 #include "os/path.h"
23 
24 
25 /// @addtogroup BasisCxxUtilities
26 /// @{
27 
28 
29 namespace basis { namespace os {
30 
31 
32 /**
33  * @brief Get absolute path of the (current) working directory.
34  *
35  * @return Absolute path of working directory or empty string on error.
36  */
37 std::string getcwd();
38 
39 /**
40  * @brief Get canonical path of executable file.
41  *
42  * @return Canonical path of executable file or empty string on error.
43  *
44  * @sa exedir()
45  * @sa exename()
46  */
47 std::string exepath();
48 
49 /**
50  * @brief Get name of executable.
51  *
52  * @note The name of the executable may or may not include the file name
53  * extension depending on the executable type and operating system.
54  * Hence, this function is neither an equivalent to
55  * path::basename(exepath()) nor path::filename(exepath()).
56  * In particular, on Windows, the .exe and .com extension is not
57  * included in the returned executable name.
58  *
59  * @return Name of the executable derived from the executable's file path
60  * or empty string on error.
61  *
62  * @sa exepath()
63  * @sa exedir()
64  */
65 std::string exename();
66 
67 /**
68  * @brief Get canonical path to directory containing executable file.
69  *
70  * @return Canonical path to directory containing executable file.
71  *
72  * @sa exepath()
73  * @sa exename()
74  */
75 std::string exedir();
76 
77 /**
78  * @brief Read value of symbolic link.
79  *
80  * @param [in] path Path of symbolic link.
81  *
82  * @returns Value of symbolic link. Can be relative or absolute path. If the
83  * link could not be read, an empty string is returned. Note that
84  * on Windows, this function always returns an empty string.
85  *
86  * @sa realpath()
87  */
88 std::string readlink(const std::string& path);
89 
90 /**
91  * @brief Make directory.
92  *
93  * @note The parent directory must exist already. See makedirs() for a function
94  * that also creates the parent directories if none-existent.
95  *
96  * @param path Path of the directory.
97  *
98  * @returns Whether the directory was created successfully.
99  * Note that on Posix, the created directory will have mode 0755.
100  * On Windows, the default security descriptor is passed on to the
101  * CreateDirectory() function.
102  *
103  * @sa makedirs()
104  */
105 bool mkdir(const std::string& path);
106 
107 /**
108  * @brief Make directory including parent directories if required.
109  *
110  * @param path Path of the directory.
111  *
112  * @returns Whether the directory was created successfully.
113  * Note that on Posix, the created directories will have mode 0755.
114  * On Windows, the default security descriptor is passed on to the
115  * CreateDirectory() function.
116  */
117 bool makedirs(const std::string& path);
118 
119 /**
120  * @brief Remove empty directory.
121  *
122  * This function removes an empty directory. If the directory is not empty,
123  * it will fail. See rmtree() for a function that also removes the files and
124  * recursively the directories within the specified directory.
125  *
126  * @param path Path of the directory.
127  *
128  * @returns Whether the directory was removed successfully.
129  *
130  * @sa rmtree()
131  */
132 bool rmdir(const std::string& path);
133 
134 /**
135  * @brief Remove whole directory tree.
136  *
137  * @param path Path of the directory.
138  *
139  * @returns Whether the directory was removed successfully.
140  */
141 bool rmtree(const std::string& path);
142 
143 /**
144  * @brief Remove files and directories from directory.
145  *
146  * @param path Path of the directory.
147  *
148  * @returns Whether the directory was cleared successfully, i.e., leaving
149  * the directory @p path empty.
150  */
151 bool emptydir(const std::string& path);
152 
153 
154 } // namespace os
155 
156 } // namespace basis
157 
158 
159 /// @}
160 // Doxygen group
161 
162 #endif // _BASIS_OS_H
bool mkdir(const std::string &path)
Make directory.
Definition: os.cxx:199
std::string getcwd()
Get absolute path of the (current) working directory.
Definition: os.cxx:47
bool rmdir(const std::string &path)
Remove empty directory.
Definition: os.cxx:226
bool emptydir(const std::string &path)
Remove files and directories from directory.
Definition: os.cxx:238
Definition: basis.h:34
std::string exedir()
Get canonical path to directory containing executable file.
Definition: os.cxx:140
std::string exename()
Get name of executable.
Definition: os.cxx:127
bool makedirs(const std::string &path)
Make directory including parent directories if required.
Definition: os.cxx:205
bool rmtree(const std::string &path)
Remove whole directory tree.
Definition: os.cxx:232
File/directory path related functions.
std::string readlink(const std::string &path)
Read value of symbolic link.
Definition: os.cxx:147
std::string exepath()
Get canonical path of executable file.
Definition: os.cxx:63