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__ )
37 import ALib.Strings.StdIOStream;
60template<
typename TRequires= PathCharType>
61requires std::same_as<TRequires, character>
67template<
typename TRequires= PathCharType>
68requires (!std::same_as<TRequires, character>)
93void createTempFolderInHomeDir(
const PathString& folderName,
Path& resultPath,
96 Path homeTemp( SystemFolders::Home );
97 resultPath.Reset( homeTemp );
101 bool exists= homeTemp.IsDirectory();
103 if( homeTemp.Create() == SystemErrors::OK ) {
107 std::ofstream file ( fileName );
108 if ( file.is_open() ) {
110 file <<
"This folder was created by \"" << pi.CmdLine
112 <<
"to be used for temporary files." << std::endl;
113 file.write( reasonMsg.Buffer(), reasonMsg.Length() );
120 resultPath.Reset( homeTemp );
130 case SystemFolders::Current:
133 nchar charBuf[FILENAME_MAX];
135 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || defined(__APPLE__) || defined(__ANDROID_NDK__)
136 if ( ! getcwd( charBuf,
sizeof(charBuf ) ) )
138 #elif defined ( _WIN32 )
139 if ( !_getcwd( charBuf,
sizeof(charBuf ) ) )
142 #pragma message ("Unknown Platform in file: " __FILE__ )
146 this ->_(
static_cast<const nchar*
>( charBuf ) );
151 case SystemFolders::Home:
153 #if defined (__unix__)
154 if ( !loadEnvVar(
A_CHAR(
"HOME"), *
this ) ) {
155 struct passwd* pwd = getpwuid(getuid());
160 #elif defined(__APPLE__)
161 macos::ALIB_APPLE_OC_NSHomeDirectory( *
this );
163 struct passwd* pwd = getpwuid(getuid());
169 #elif defined(_WIN32)
171 loadEnvVar(
A_CHAR(
"HOMEDRIVE"), *
this );
176 #pragma message ("Unknown Platform in file: " __FILE__ )
182 case SystemFolders::HomeConfig:
184 if( !
Change( SystemFolders::Home ) )
188 #if defined (__unix__)
191 #elif defined(__APPLE__)
194 #elif defined(_WIN32)
198 #pragma message ("Unknown Platform in file: " __FILE__ )
203 case SystemFolders::Module:
209 case SystemFolders::Temp:
215 #if defined (__unix__)
216 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
228 #elif defined(__APPLE__)
229 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
231 macos::ALIB_APPLE_OC_NSTemporaryDirectory( temp );
255 #elif defined(_WIN32)
256 NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
271 #pragma message ("Unknown Platform in file: " __FILE__ )
277 createTempFolderInHomeDir(
A_PATH(
".tmp"), homeTemp, reasonMsg );
292 Change( SystemFolders::Home );
311 case SystemFolders::VarTemp:
317 #if defined (__unix__)
318 NString reasonMsg=
"(The default folder \"/var/tmp\" could not be found.)";
330 #elif defined(__APPLE__)
331 const NString reasonMsg=
"(The default folder \"/private/var/tmp\" could not be found.)";
343 #elif defined(_WIN32)
344 const NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
359 #pragma message ("Unknown Platform in file: " __FILE__ )
365 createTempFolderInHomeDir(
A_PATH(
".var.tmp"), varTemp, reasonMsg );
380 Change( SystemFolders::Home );
399 default:
ALIB_ERROR(
"SYSTEM",
"Illegal switch state.")
452 if ( lastDirSep < 0 )
454 if ( lastDirSep == 0 )
466 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
468 DIR* dir= opendir( nPath );
469 if ( dir !=
nullptr ) {
475 #elif defined(_WIN32)
477 #if !ALIB_PATH_CHARACTERS_WIDE
478 DWORD dwAttrib = GetFileAttributesA(
Terminate() );
480 DWORD dwAttrib = GetFileAttributesW(
Terminate() );
482 if( dwAttrib == INVALID_FILE_ATTRIBUTES )
484 if ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
489 #pragma message ("Unknown Platform in file: " __FILE__ )
495 #if (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
496 || defined(__APPLE__) \
497 || defined(__ANDROID_NDK__)
505 return SystemErrors::OK;
509 namespace fs = std::filesystem;
511 std::error_code errorCode;
512 fs::path fsRealPath= fs::canonical(fs::path(std::basic_string_view<PathCharType>(
Buffer(),
515 ALIB_DBG(
if(errno==EINVAL && !errorCode) errno= 0;)
516 ALIB_DBG(
if(errno==ENOENT && !errorCode) errno= 0;)
521 Reset(fsRealPath.c_str());
522 return SystemErrors::OK;
532 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
534 int errCode= mkdir( nPath, S_IRWXU | S_IRGRP | S_IROTH
535 | S_IXGRP | S_IXOTH );
539 #elif defined(_WIN32)
540 #if !ALIB_PATH_CHARACTERS_WIDE
541 BOOL result= CreateDirectoryA(
Terminate(), NULL );
543 BOOL result= CreateDirectoryW(
Terminate(), NULL );
548 return SystemErrors::OK;
552 #pragma message ("Unknown Platform in file: " __FILE__ )
constexpr const PathCharType * Terminate() const
TAString & DeleteEnd(integer regionLength)
TAString & ShortenTo(integer newLength)
integer DetectLength(integer offset=0)
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
integer LastIndexOf(PathCharType needle, integer startIndex=MAX_LEN) 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 ChangeToParent()
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)