8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Configuration MACRO 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;
59template<
typename TRequires= PathCharType>
60requires std::same_as<TRequires, character>
66template<
typename TRequires= PathCharType>
67requires (!std::same_as<TRequires, character>)
92void createTempFolderInHomeDir(
const PathString& folderName,
Path& resultPath,
95 Path homeTemp( SystemFolders::Home );
96 resultPath.Reset( homeTemp );
100 bool exists= homeTemp.IsDirectory();
102 if( homeTemp.Create() == std::errc(0) ) {
106 std::ofstream file ( fileName );
107 if ( file.is_open() ) {
109 file <<
"This folder was created by \"" << pi.CmdLine
111 <<
"to be used for temporary files." << std::endl;
112 file.write( reasonMsg.Buffer(), reasonMsg.Length() );
119 resultPath.Reset( homeTemp );
129 case SystemFolders::Current:
132 nchar charBuf[FILENAME_MAX];
134 #if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || defined(__APPLE__) || defined(__ANDROID_NDK__)
135 if ( ! getcwd( charBuf,
sizeof(charBuf ) ) )
137 #elif defined ( _WIN32 )
138 if ( !_getcwd( charBuf,
sizeof(charBuf ) ) )
141 #pragma message ("Unknown Platform in file: " __FILE__ )
145 this ->_(
static_cast<const nchar*
>( charBuf ) );
150 case SystemFolders::Home:
152 #if defined (__unix__)
153 if ( !loadEnvVar(
A_CHAR(
"HOME"), *
this ) ) {
154 struct passwd* pwd = getpwuid(getuid());
159 #elif defined(__APPLE__)
160 macos::ALIB_APPLE_OC_NSHomeDirectory( *
this );
162 struct passwd* pwd = getpwuid(getuid());
168 #elif defined(_WIN32)
170 loadEnvVar(
A_CHAR(
"HOMEDRIVE"), *
this );
175 #pragma message ("Unknown Platform in file: " __FILE__ )
181 case SystemFolders::HomeConfig:
183 if( !
Change( SystemFolders::Home ) )
187 #if defined (__unix__)
190 #elif defined(__APPLE__)
193 #elif defined(_WIN32)
197 #pragma message ("Unknown Platform in file: " __FILE__ )
202 case SystemFolders::Module:
208 case SystemFolders::Temp:
214 #if defined (__unix__)
215 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
227 #elif defined(__APPLE__)
228 NString reasonMsg=
"(The default temporary folder \"/tmp\" could not be found.)";
230 macos::ALIB_APPLE_OC_NSTemporaryDirectory( temp );
254 #elif defined(_WIN32)
255 NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
270 #pragma message ("Unknown Platform in file: " __FILE__ )
276 createTempFolderInHomeDir(
A_PATH(
".tmp"), homeTemp, reasonMsg );
291 Change( SystemFolders::Home );
310 case SystemFolders::VarTemp:
316 #if defined (__unix__)
317 NString reasonMsg=
"(The default folder \"/var/tmp\" could not be found.)";
329 #elif defined(__APPLE__)
330 const NString reasonMsg=
"(The default folder \"/private/var/tmp\" could not be found.)";
342 #elif defined(_WIN32)
343 const NString reasonMsg=
"(Environment variables TMP and TEMP either not set or not containing valid paths.)";
358 #pragma message ("Unknown Platform in file: " __FILE__ )
364 createTempFolderInHomeDir(
A_PATH(
".var.tmp"), varTemp, reasonMsg );
379 Change( SystemFolders::Home );
398 default:
ALIB_ERROR(
"SYSTEM",
"Illegal switch state.")
451 if ( lastDirSep < 0 )
453 if ( lastDirSep == 0 )
465 #if defined (__GLIBC__) || defined(__APPLE__) || defined(__ANDROID_NDK__)
467 DIR* dir= opendir( nPath );
468 if ( dir !=
nullptr ) {
474 #elif defined(_WIN32)
476 #if !ALIB_PATH_CHARACTERS_WIDE
477 DWORD dwAttrib = GetFileAttributesA(
Terminate() );
479 DWORD dwAttrib = GetFileAttributesW(
Terminate() );
481 if( dwAttrib == INVALID_FILE_ATTRIBUTES )
483 if ( dwAttrib & FILE_ATTRIBUTE_DIRECTORY )
488 #pragma message ("Unknown Platform in file: " __FILE__ )
494 #if (defined(__GLIBCXX__) && !defined(__MINGW32__)) \
495 || defined(__APPLE__) \
496 || defined(__ANDROID_NDK__)
500 return std::errc(errno);
508 namespace fs = std::filesystem;
510 std::error_code errorCode;
511 fs::path fsRealPath= fs::canonical(fs::path(std::basic_string_view<PathCharType>(
Buffer(),
514 ALIB_DBG(
if(errno==EINVAL && !errorCode) errno= 0;)
515 ALIB_DBG(
if(errno==ENOENT && !errorCode) errno= 0;)
518 return std::errc(errorCode.value());
520 Reset(fsRealPath.c_str());
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 );
537 return std::errc(errCode);
539 #elif defined(_WIN32)
540 #if !ALIB_PATH_CHARACTERS_WIDE
541 BOOL result= CreateDirectoryA(
Terminate(), NULL );
543 BOOL result= CreateDirectoryW(
Terminate(), NULL );
550 return std::errc( GetLastError() );
552 #pragma message ("Unknown Platform in file: " __FILE__ )
#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 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
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< 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.
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.
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
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)