12template<
typename TRequires= PathCharType>
13requires std::same_as<TRequires, character>
19template<
typename TRequires= PathCharType>
20requires (!std::same_as<TRequires, character>)
45void createTempFolderInHomeDir(
const PathString& folderName,
Path& resultPath,
48 Path homeTemp( SystemFolders::Home );
49 resultPath.Reset( homeTemp );
53 bool exists= homeTemp.IsDirectory();
55 if( homeTemp.Create() == std::errc(0) ) {
59 std::ofstream file ( fileName );
60 if ( file.is_open() ) {
62 file <<
"This folder was created by \"" << pi.CmdLine
64 <<
"to be used for temporary files." << std::endl;
65 file.write( reasonMsg.Buffer(), reasonMsg.Length() );
72 resultPath.Reset( homeTemp );
82 case SystemFolders::Current:
85 nchar charBuf[FILENAME_MAX];
87 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || defined(__APPLE__) || defined(__ANDROID_NDK__)
88 if ( ! getcwd( charBuf,
sizeof(charBuf ) ) )
90 #elif defined ( _WIN32 )
91 if ( !_getcwd( charBuf,
sizeof(charBuf ) ) )
94 #pragma message ("Unknown Platform in file: " __FILE__ )
98 this ->_(
static_cast<const nchar*
>( charBuf ) );
103 case SystemFolders::Home:
105 #if defined (__unix__)
106 if ( !loadEnvVar(
A_CHAR(
"HOME"), *
this ) ) {
107 struct passwd* pwd = getpwuid(getuid());
112 #elif defined(__APPLE__)
113 macos::ALIB_APPLE_OC_NSHomeDirectory( *
this );
115 struct passwd* pwd = getpwuid(getuid());
121 #elif defined(_WIN32)
123 loadEnvVar(
A_CHAR(
"HOMEDRIVE"), *
this );
128 #pragma message ("Unknown Platform in file: " __FILE__ )
134 case SystemFolders::HomeConfig:
136 if( !
Change( SystemFolders::Home ) )
140 #if defined (__unix__)
143 #elif defined(__APPLE__)
146 #elif defined(_WIN32)
150 #pragma message ("Unknown Platform in file: " __FILE__ )
155 case SystemFolders::Module:
161 case SystemFolders::Temp:
167 #if defined (__unix__)
168 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
180 #elif defined(__APPLE__)
181 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
183 macos::ALIB_APPLE_OC_NSTemporaryDirectory( temp );
207 #elif defined(_WIN32)
208 NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
223 #pragma message ("Unknown Platform in file: " __FILE__ )
229 createTempFolderInHomeDir(
A_PATH(
".tmp"), homeTemp, reasonMsg );
244 Change( SystemFolders::Home );
263 case SystemFolders::VarTemp:
269 #if defined (__unix__)
270 NString reasonMsg=
"(The default folder \"/var/tmp\" could not be found.)";
282 #elif defined(__APPLE__)
283 const NString reasonMsg=
"(The default folder \"/private/var/tmp\" could not be found.)";
295 #elif defined(_WIN32)
296 const NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
311 #pragma message ("Unknown Platform in file: " __FILE__ )
317 createTempFolderInHomeDir(
A_PATH(
".var.tmp"), varTemp, reasonMsg );
332 Change( SystemFolders::Home );
351 default:
ALIB_ERROR(
"SYSTEM",
"Illegal switch state.")
403 if ( lastDirSep < 0 )
405 if ( lastDirSep == 0 )
417 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
419 DIR* dir= opendir( nPath );
420 if ( dir !=
nullptr ) {
426 #elif defined(_WIN32)
428 #if !ALIB_PATH_CHARACTERS_WIDE
429 DWORD dwAttrib = GetFileAttributesA(
Terminate() );
431 DWORD dwAttrib = GetFileAttributesW(
Terminate() );
433 if( dwAttrib == INVALID_FILE_ATTRIBUTES )
435 if ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
440 #pragma message ("Unknown Platform in file: " __FILE__ )
447 #if (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
448 || defined(__APPLE__) \
449 || defined(__ANDROID_NDK__)
453 return std::errc(errno);
460 namespace fs = std::filesystem;
462 std::error_code errorCode;
463 fs::path fsRealPath= fs::canonical(fs::path(std::basic_string_view<PathCharType>(
Buffer(),
466 ALIB_DBG(
if(errno==EINVAL && !errorCode) errno= 0;)
467 ALIB_DBG(
if(errno==ENOENT && !errorCode) errno= 0;)
470 return std::errc(errorCode.value());
472 Reset(fsRealPath.c_str());
484 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
486 int errCode= mkdir( nPath, S_IRWXU | S_IRGRP | S_IROTH
487 | S_IXGRP | S_IXOTH );
489 return std::errc(errCode);
491 #elif defined(_WIN32)
492 #if !ALIB_PATH_CHARACTERS_WIDE
493 BOOL result= CreateDirectoryA(
Terminate(), NULL );
495 BOOL result= CreateDirectoryW(
Terminate(), NULL );
502 return std::errc( GetLastError() );
504 #pragma message ("Unknown Platform in file: " __FILE__ )
513 Path original(*
this);
515 Change(SystemFolders::Current);
519 Change( SystemFolders::Current );
524 DWORD actualLength= GetFullPathNameW( original.
Terminate(), 8192,
525 fullPath.VBuffer(),
nullptr );
526 if( actualLength == 0 )
529 fullPath.SetLength(
integer(actualLength) );
#define ALIB_ALLOW_UNUSED_FUNCTION
#define ALIB_ERROR(domain,...)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_POP_ALLOWANCE
#define ALIB_BOXING_VTABLE_DEFINE(TMapped, Identifier)
constexpr const PathCharType * Terminate() const
TAString & DeleteEnd(integer regionLength)
TAString & ShortenTo(integer newLength)
integer DetectLength(integer offset=0)
TAString & _(const TAppendable &src)
constexpr TLocalString(lang::HeapAllocator &pAllocator)
constexpr integer Length() const
constexpr bool IsEmpty() const
bool EndsWith(const TString &needle) const
constexpr bool IsNotEmpty() const
constexpr const PathCharType * Buffer() const
integer LastIndexOf(PathCharType needle, integer startIndex=MAX_LEN) const
PathCharType CharAtEnd() const
std::errc MakeCanonical()
static PathString varTempDirEvaluatedOnce
void AddModuleName(const PathString &extension)
bool Change(const PathString &path)
static PathString tempDirEvaluatedOnce
This class represents process information.
static const ProcessInfo & Current()
@ Keep
Chooses not no clear existing data.
@ Clear
Chooses to clear existing data.
RecursiveLock GLOBAL_ALLOCATOR_LOCK
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
strings::TString< nchar > NString
Type alias in namespace #"%alib".
strings::TCString< character > CString
Type alias in namespace #"%alib".
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
characters::nchar nchar
Type alias in namespace #"%alib".
strings::TString< PathCharType > PathString
The string-type used with this ALib Module.
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
constexpr PathCharType DIRECTORY_SEPARATOR
The standard path separator character. Defaults to '\' on Windows OS, '/' else.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace #"%alib".
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
static bool Get(const CString &varName, AString &target, lang::CurrentData targetData=lang::CurrentData::Clear)