ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
directory.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of sub-namespace # alib::lang::system of module \alib_basecamp of
4 * the \aliblong.
5 *
6 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7 * Published under \ref mainpage_license "Boost Software License".
8 **************************************************************************************************/
9#ifndef HPP_ALIB_CAMP_DIRECTORY
10#define HPP_ALIB_CAMP_DIRECTORY 1
11
12#if !defined(HPP_ALIB_CAMP_ERRORS)
14#endif
15
16#if !defined(HPP_ALIB_STRINGS_STRINGNZT)
18#endif
19
20
21namespace alib { namespace lang::system {
22
23// #############################################################################################
24// MacOS System call wrappers (implemented in Objective-C)
25// #############################################################################################
26#if defined(__APPLE__)
27namespace macos
28{
29 void ALIB_APPLE_OC_NSTemporaryDirectory( AString& target);
30 void ALIB_APPLE_OC_NSHomeDirectory ( AString& target);
31}
32#endif
33
34
35/** ************************************************************************************************
36 * Represents a directory (folder) in a file system.
37 *
38 * \note
39 * This class is not recommended to be used. It was created once to have something for
40 * class \alib{config;IniFile}, but never came to a state of true usability.
41 **************************************************************************************************/
43{
44 // #############################################################################################
45 // Types
46 // #############################################################################################
47 public:
48 /** ****************************************************************************************
49 * Denotes upper and lower case character conversions
50 ******************************************************************************************/
51 enum class SpecialFolder
52 {
53 /** The root directory . */
54 Root,
55
56 /** The current directory of the process. */
57 Current,
58
59 /** The user's home directory */
60 Home,
61
62 /**
63 * The user's configuration directory. This is evaluated as follows:
64 * - Using environment variable \e HOME (under WindowsOS a combination of \e HOMEDRIVE
65 * and \e HOMEPATH), the home directory is determined.
66 * - If within this a directory \e ".config" exists, it is used, otherwise
67 * - if within this the directories \e "AppData/Roaming" exist, this is used.
68 */
70
71 /** The directory of the executable of the process. */
72 Module,
73
74 /**
75 * A directory to be used for creation of temporary files.
76 *
77 * - On GNU/Linux OS this defaults to <c>/tmp</c>.
78 * - On Windows OS, environment variables TMP and TEMP are evaluated.<br>
79 *
80 * If the directory does not exist, then (on all OS), a new directory named \c ".tmp"
81 * is created in the user's home directory and returned (if not existent already).
82 * If this fails, the home directory itself is returned.
83 *
84 * \note With the potential creation of the directory \c ".tmp" in the user's home
85 * directory, a small \c readme.txt file is created containing the name of
86 * the running application and the reason of the creation.
87 *
88 * To overrule this behavior, public static variable #evaluatedTempDir
89 * may be set arbitrarily prior to using this enum value.
90 */
91 Temp,
92
93 /**
94 * A directory to be used for creation of temporary files that survives reboots of the
95 * host machine.
96 *
97 * - On GNU/Linux OS this defaults to <c>/var/tmp</c>.
98 * - On Windows OS, environment variables TMP and TEMP are evaluated (same as
99 * with \b %SpecialFolder::Temp).<br>
100 *
101 * If the directory does not exist, then (on all OS), a new directory named \c ".var.tmp"
102 * is created in the user's home directory and returned (if not existent already).
103 * If this fails, the home directory itself is returned.
104 *
105 * \note With the potential creation of the directory \c ".var.tmp" in the user's home
106 * directory, a small \c readme.txt file is created, containing the name of
107 * the running application and the reason of the creation.
108 *
109 * To overrule this behavior, public static variable #evaluatedVarTempDir
110 * may be set arbitrarily prior to using this enum value.
111 */
112 VarTemp,
113 };
114
115 // #############################################################################################
116 // Fields
117 // #############################################################################################
118 public:
119 /**
120 * Singleton containing the path for the use of enum value
121 * \ref SpecialFolder "SpecialFolder::Temp".
122 * This is evaluated once with the first use of \b %SpecialFolder::Temp.
123 * To change the default behavior of evaluation, this variable may be filled with a proper
124 * path prior to using enum \b %SpecialFolder::Temp.
125 */
127
128 /**
129 * Singleton containing the path for the use of enum value
130 * \ref SpecialFolder "SpecialFolder::VarTemp".
131 * This is evaluated once with the first use of \b %SpecialFolder::VarTemp.
132 * To change the default behavior of evaluation, this variable may be filled with a proper
133 * path prior to using enum \b %SpecialFolder::VarTemp.
134 */
136
137 /** The path of the directory represented by this instance */
139
140 // #############################################################################################
141 // Constructors
142 // #############################################################################################
143 public:
144 /** ****************************************************************************************
145 * Constructs an object representing no directory.
146 * Field #Path remains empty.
147 ******************************************************************************************/
149 {}
150
151 /** ****************************************************************************************
152 * Constructs an object representing one of the known special directories.
153 * @param special The special directory to initialize this instance to.
154 ******************************************************************************************/
156 {
157 Change( special );
158 }
159
160 /** ****************************************************************************************
161 * Constructs an object representing the given path.
162 * @param path The initial path. If this is a relative path, it relates to the processes
163 * current directory.
164 ******************************************************************************************/
165 Directory( const String& path )
166 {
167 Path._( '/' );
168 Change( path );
169 }
170
171
172 // #############################################################################################
173 // Interface
174 // #############################################################################################
175 public:
176 /** ****************************************************************************************
177 * Changes the directory. If the given path is relative (e.g. a name of a sub directory
178 * or ".."), such path is added to the current #Path. Otherwise, field #Path is replaced.
179 * If the resulting destination directory is not valid, \c false is returned and the
180 * field #Path stays intact.
181 * @param path The relative or absolute path to change to.
182 * @returns \c true if the change was successful, \c false otherwise.
183 ******************************************************************************************/
184 ALIB_API bool Change( const CString& path );
185
186 /** ****************************************************************************************
187 * Overloaded version of #Change(const CString&) accepting a non-zero-terminated string.
188 * @param path The relative or absolute path to change to.
189 * @returns \c true if the change was successful, \c false otherwise.
190 ******************************************************************************************/
191 ALIB_API bool Change( const StringNZT& path );
192
193 /** ****************************************************************************************
194 * Changes the directory to one of the known special directories.
195 * @param special The special directory to change this instance to.
196 ******************************************************************************************/
197 ALIB_API void Change( SpecialFolder special );
198
199
200 // #############################################################################################
201 // static interface
202 // #############################################################################################
203 public:
204 /** ****************************************************************************************
205 * Returns the actual directory in the file system associated with the current process.
206 * @param target The target AString to store the directory. Will be cleared in any case
207 * @return \c true on success, \c false on failure.
208 ******************************************************************************************/
210 static bool CurrentDirectory( AString& target );
211
212 /** ****************************************************************************************
213 * Tests if the given (absolute or relative) path represents a directory in the file system.
214 * @param path Zero-terminated string defining the path to test.
215 * @return \c true if a directory was found.
216 ******************************************************************************************/
218 static bool Exists( const CString& path );
219
220 /** ****************************************************************************************
221 * Overloaded version of #Change(const CString&) accepting a non-zero-terminated string.
222 * @param path The relative or absolute path to change to.
223 * @returns \c true if the change was successful, \c false otherwise.
224 ******************************************************************************************/
225 static bool Exists( const StringNZT& path )
226 {
227 return Exists( String256(path) );
228 }
229
230 /** ****************************************************************************************
231 * Creates the directory of the given (absolute or relative) path.
232 * @param path Zero-terminated string defining the path to test.
233 * @return A value of enum type \alib{lang::system;SystemErrors}.
234 ******************************************************************************************/
236 static SystemErrors Create( const CString& path );
237}; //class Directory
238
239} // namespace alib[:: lang::system]
240
241/// Type alias in namespace \b alib.
243
244/**
245 * The standard path separator character. Defaults to '\\' on Windows OS, '/' else.
246 * Note: Available with including "alib/lang/system/directory.hpp"
247 */
249#if !defined(ALIB_DOX)
250 #if defined( _WIN32 )
251 = '\\';
252 #else
253 = '/';
254 #endif
255#else
256 ;
257#endif
258} // namespace [alib]
259
261#endif // HPP_ALIB_CAMP_DIRECTORY
static ALIB_API bool CurrentDirectory(AString &target)
Directory(const String &path)
static AString evaluatedVarTempDir
static bool Exists(const StringNZT &path)
ALIB_API bool Change(const CString &path)
static ALIB_API bool Exists(const CString &path)
Directory(SpecialFolder special)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
Definition records.hpp:752
#define ALIB_API
Definition alib.hpp:538
@ Create
Denotes to create data.
Definition alib.cpp:57
constexpr nchar DirectorySeparator
LocalString< 256 > String256
Type alias name for TLocalString<character,256> .
characters::nchar nchar
Type alias in namespace alib.