14# if defined ( _WIN32 )
16# elif defined(__GLIBCXX__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
22# pragma message ("Unknown Platform in file: " __FILE__ )
36template<
typename TIf= PathCharType>
42template<
typename TIf= PathCharType>
67void createTempFolderInHomeDir(
const PathString& folderName,
Path& resultPath,
71 Path homeTemp( SystemFolders::Home );
72 resultPath.Reset( homeTemp );
76 bool exists= homeTemp.IsDirectory();
79 if( homeTemp.Create() == SystemErrors::OK )
84 std::ofstream file ( fileName );
88 file <<
"This folder was created by \"" << pi.CmdLine
90 <<
"to be used for temporary files." << std::endl;
91 file.write( reasonMsg.Buffer(), reasonMsg.Length() );
100 resultPath.Reset( homeTemp );
112 case SystemFolders::Current:
115 nchar charBuf[FILENAME_MAX];
117 #if defined(__GLIBCXX__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
118 if ( ! getcwd( charBuf,
sizeof(charBuf ) ) )
120 #elif defined ( _WIN32 )
121 if ( !_getcwd( charBuf,
sizeof(charBuf ) ) )
124 #pragma message ("Unknown Platform in file: " __FILE__ )
128 this ->_(
static_cast<const nchar*
>( charBuf ) );
133 case SystemFolders::Home:
135 #if defined (__unix__)
136 if ( !loadEnvVar(
A_CHAR(
"HOME"), *
this ) )
138 struct passwd* pwd = getpwuid(getuid());
143 #elif defined(__APPLE__)
144 macos::ALIB_APPLE_OC_NSHomeDirectory( *
this );
147 struct passwd* pwd = getpwuid(getuid());
153 #elif defined(_WIN32)
156 loadEnvVar(
A_CHAR(
"HOMEDRIVE"), *
this );
161 #pragma message ("Unknown Platform in file: " __FILE__ )
167 case SystemFolders::HomeConfig:
169 if( !
Change( SystemFolders::Home ) )
173 #if defined (__unix__)
176 #elif defined(__APPLE__)
179 #elif defined(_WIN32)
183 #pragma message ("Unknown Platform in file: " __FILE__ )
188 case SystemFolders::Module:
194 case SystemFolders::Temp:
199 #if defined (__unix__)
200 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
205 #elif defined(__APPLE__)
206 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
208 macos::ALIB_APPLE_OC_NSTemporaryDirectory( temp );
219 #elif defined(_WIN32)
220 NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
228 #pragma message ("Unknown Platform in file: " __FILE__ )
235 createTempFolderInHomeDir(
A_PATH(
".tmp"), homeTemp, reasonMsg );
244 Change( SystemFolders::Home );
258 case SystemFolders::VarTemp:
263 #if defined (__unix__)
264 NString reasonMsg=
"(The default folder \"/var/tmp\" could not be found.)";
269 #elif defined(__APPLE__)
270 const NString reasonMsg=
"(The default folder \"/private/var/tmp\" could not be found.)";
276 #elif defined(_WIN32)
277 const NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
285 #pragma message ("Unknown Platform in file: " __FILE__ )
292 createTempFolderInHomeDir(
A_PATH(
".var.tmp"), varTemp, reasonMsg );
301 Change( SystemFolders::Home );
362 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
364 DIR* dir= opendir( nPath );
365 if ( dir !=
nullptr )
372 #elif defined(_WIN32)
374 #if !ALIB_PATH_CHARACTERS_WIDE
375 DWORD dwAttrib = GetFileAttributesA(
Terminate() );
377 DWORD dwAttrib = GetFileAttributesW(
Terminate() );
379 if( dwAttrib == INVALID_FILE_ATTRIBUTES )
381 if ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
386 #pragma message ("Unknown Platform in file: " __FILE__ )
392 #if (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
393 || defined(__APPLE__) \
394 || defined(__ANDROID_NDK__)
402 return SystemErrors::OK;
406 namespace fs = std::filesystem;
408 std::error_code errorCode;
409 fs::path fsRealPath= fs::canonical(fs::path(std::basic_string_view<PathCharType>(
Buffer(),
412 ALIB_DBG(
if(errno==EINVAL && !errorCode) errno= 0;)
413 ALIB_DBG(
if(errno==ENOENT && !errorCode) errno= 0;)
418 Reset(fsRealPath.c_str());
419 return SystemErrors::OK;
430 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
432 int errCode= mkdir( nPath, S_IRWXU | S_IRGRP | S_IROTH
433 | S_IXGRP | S_IXOTH );
437 #elif defined(_WIN32)
438 #if !ALIB_PATH_CHARACTERS_WIDE
439 BOOL result= CreateDirectoryA(
Terminate(), NULL );
441 BOOL result= CreateDirectoryW(
Terminate(), NULL );
446 return SystemErrors::OK;
450 #pragma message ("Unknown Platform in file: " __FILE__ )
ALIB_API bool Change(const PathString &path)
static PathString tempDirEvaluatedOnce
ALIB_API SystemErrors Create()
ALIB_API SystemErrors MakeReal()
ALIB_API void AddModuleName(const PathString &extension)
static PathString varTempDirEvaluatedOnce
ALIB_API bool IsDirectory()
static ALIB_API const ProcessInfo & Current()
TAString & ShortenTo(integer newLength)
constexpr const PathCharType * Terminate() const
TAString & DeleteEnd(integer regionLength)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
constexpr bool IsNull() const
constexpr bool IsEmpty() const
void Allocate(TAllocator &allocator, const TString< TChar > ©)
constexpr bool IsNotEmpty() const
constexpr integer Length() const
TChar CharAtStart() const
PathCharType CharAtEnd() const
bool EndsWith(const TString &needle) const
constexpr const PathCharType * Buffer() const
#define ATMP_BOOL_IF(Cond)
#define ALIB_WARNINGS_RESTORE
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ATMP_EQ( T, TEqual)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_WARNINGS_IGNORE_UNUSED_FUNCTION
This is the reference documentation of sub-namespace system of module ALib BaseCamp.
strings::TString< PathCharType > PathString
The string-type used with this ALib Module.
SystemErrors
Denotes result values returned by system functions (glibc, etc).
static constexpr PathCharType DIRECTORY_SEPARATOR
The standard path separator character. Defaults to '\' on Windows OS, '/' else.
platform_specific integer
@ Keep
Chooses not no clear existing data.
@ Clear
Chooses to clear existing data.
ALIB_API MonoAllocator GLOBAL_ALLOCATOR
ALIB_API RecursiveLock GLOBAL_ALLOCATOR_LOCK
lang::system::ProcessInfo ProcessInfo
Type alias in namespace alib.
strings::TString< nchar > NString
Type alias in namespace alib.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace alib.
lang::system::Path Path
Type alias in namespace alib.
strings::TCString< character > CString
Type alias in namespace alib.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
characters::character character
Type alias in namespace alib.
characters::nchar nchar
Type alias in namespace alib.
static ALIB_API bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)