8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
19# if defined ( _WIN32 )
21# elif defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || defined(__APPLE__) || defined(__ANDROID_NDK__)
27# pragma message ("Unknown Platform in file: " __FILE__ )
60template<
typename TRequires= PathCharType>
61requires std::same_as<TRequires, character>
66template<
typename TRequires= PathCharType>
67requires (!std::same_as<TRequires, character>)
91void createTempFolderInHomeDir(
const PathString& folderName,
Path& resultPath,
95 Path homeTemp( SystemFolders::Home );
96 resultPath.Reset( homeTemp );
100 bool exists= homeTemp.IsDirectory();
103 if( homeTemp.Create() == SystemErrors::OK )
108 std::ofstream file ( fileName );
109 if ( file.is_open() )
112 file <<
"This folder was created by \"" << pi.CmdLine
114 <<
"to be used for temporary files." << std::endl;
115 file.write( reasonMsg.Buffer(), reasonMsg.Length() );
124 resultPath.Reset( homeTemp );
136 case SystemFolders::Current:
139 nchar charBuf[FILENAME_MAX];
141 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || defined(__APPLE__) || defined(__ANDROID_NDK__)
142 if ( ! getcwd( charBuf,
sizeof(charBuf ) ) )
144 #elif defined ( _WIN32 )
145 if ( !_getcwd( charBuf,
sizeof(charBuf ) ) )
148 #pragma message ("Unknown Platform in file: " __FILE__ )
152 this ->_(
static_cast<const nchar*
>( charBuf ) );
157 case SystemFolders::Home:
159 #if defined (__unix__)
160 if ( !loadEnvVar(
A_CHAR(
"HOME"), *
this ) )
162 struct passwd* pwd = getpwuid(getuid());
167 #elif defined(__APPLE__)
168 macos::ALIB_APPLE_OC_NSHomeDirectory( *
this );
171 struct passwd* pwd = getpwuid(getuid());
177 #elif defined(_WIN32)
180 loadEnvVar(
A_CHAR(
"HOMEDRIVE"), *
this );
185 #pragma message ("Unknown Platform in file: " __FILE__ )
191 case SystemFolders::HomeConfig:
193 if( !
Change( SystemFolders::Home ) )
197 #if defined (__unix__)
200 #elif defined(__APPLE__)
203 #elif defined(_WIN32)
207 #pragma message ("Unknown Platform in file: " __FILE__ )
212 case SystemFolders::Module:
218 case SystemFolders::Temp:
225 #if defined (__unix__)
226 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
238 #elif defined(__APPLE__)
239 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
241 macos::ALIB_APPLE_OC_NSTemporaryDirectory( temp );
266 #elif defined(_WIN32)
267 NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
282 #pragma message ("Unknown Platform in file: " __FILE__ )
289 createTempFolderInHomeDir(
A_PATH(
".tmp"), homeTemp, reasonMsg );
307 Change( SystemFolders::Home );
329 case SystemFolders::VarTemp:
336 #if defined (__unix__)
337 NString reasonMsg=
"(The default folder \"/var/tmp\" could not be found.)";
349 #elif defined(__APPLE__)
350 const NString reasonMsg=
"(The default folder \"/private/var/tmp\" could not be found.)";
362 #elif defined(_WIN32)
363 const NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
378 #pragma message ("Unknown Platform in file: " __FILE__ )
385 createTempFolderInHomeDir(
A_PATH(
".var.tmp"), varTemp, reasonMsg );
403 Change( SystemFolders::Home );
424 default:
ALIB_ERROR(
"SYSTEM",
"Illegal switch state.")
472 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
474 DIR* dir= opendir( nPath );
475 if ( dir !=
nullptr )
482 #elif defined(_WIN32)
484 #if !ALIB_PATH_CHARACTERS_WIDE
485 DWORD dwAttrib = GetFileAttributesA(
Terminate() );
487 DWORD dwAttrib = GetFileAttributesW(
Terminate() );
489 if( dwAttrib == INVALID_FILE_ATTRIBUTES )
491 if ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
496 #pragma message ("Unknown Platform in file: " __FILE__ )
502 #if (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
503 || defined(__APPLE__) \
504 || defined(__ANDROID_NDK__)
512 return SystemErrors::OK;
516 namespace fs = std::filesystem;
518 std::error_code errorCode;
519 fs::path fsRealPath= fs::canonical(fs::path(std::basic_string_view<PathCharType>(
Buffer(),
522 ALIB_DBG(
if(errno==EINVAL && !errorCode) errno= 0;)
523 ALIB_DBG(
if(errno==ENOENT && !errorCode) errno= 0;)
528 Reset(fsRealPath.c_str());
529 return SystemErrors::OK;
540 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
542 int errCode= mkdir( nPath, S_IRWXU | S_IRGRP | S_IROTH
543 | S_IXGRP | S_IXOTH );
547 #elif defined(_WIN32)
548 #if !ALIB_PATH_CHARACTERS_WIDE
549 BOOL result= CreateDirectoryA(
Terminate(), NULL );
551 BOOL result= CreateDirectoryW(
Terminate(), NULL );
556 return SystemErrors::OK;
560 #pragma message ("Unknown Platform in file: " __FILE__ )
constexpr const PathCharType * Terminate() const
TAString & DeleteEnd(integer regionLength)
TAString & ShortenTo(integer newLength)
TAString & _(const TAppendable &src)
constexpr integer Length() const
constexpr bool IsEmpty() const
bool EndsWith(const TString &needle) const
TChar CharAtStart() const
constexpr bool IsNotEmpty() const
constexpr const PathCharType * Buffer() const
PathCharType CharAtEnd() const
ALIB_DLL SystemErrors MakeReal()
static PathString varTempDirEvaluatedOnce
ALIB_DLL SystemErrors Create()
ALIB_DLL void AddModuleName(const PathString &extension)
ALIB_DLL bool IsDirectory()
ALIB_DLL bool Change(const PathString &path)
static PathString tempDirEvaluatedOnce
This class represents process information.
static ALIB_DLL const ProcessInfo & Current()
#define ALIB_BOXING_VTABLE_DEFINE(TMapped, Identifier)
#define ALIB_WARNINGS_RESTORE
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_ERROR(domain,...)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_WARNINGS_IGNORE_UNUSED_FUNCTION
@ Keep
Chooses not no clear existing data.
@ Clear
Chooses to clear existing data.
ALIB_DLL TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
ALIB_DLL RecursiveLock GLOBAL_ALLOCATOR_LOCK
strings::TString< PathCharType > PathString
The string-type used with this ALib Module.
constexpr PathCharType DIRECTORY_SEPARATOR
The standard path separator character. Defaults to '\' on Windows OS, '/' else.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
strings::TCString< character > CString
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
strings::TString< nchar > NString
Type alias in namespace alib.
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace alib.
characters::nchar nchar
Type alias in namespace alib.
static ALIB_DLL bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)