// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/classes/system/system.cpp
// Function: smooth::System::System::GetPersonalFilesDirectory(smooth::System::PersonalFilesType)
S::String S::System::System::GetPersonalFilesDirectory(PersonalFilesType type)
{
	static String	 personalDir[NumPersonalFilesTypes];

	if (personalDir[type] != NIL) return personalDir[type];

	String::InputFormat	 inputFormat("UTF-8");

#ifdef __WIN32__
	ITEMIDLIST	*idlist;
	Buffer<wchar_t>	 buffer(32768 + 1);

	switch (type)
	{
		default:
		case PersonalFilesGeneric:
			SHGetSpecialFolderLocation(NIL, CSIDL_PROFILE, &idlist);
			break;
		case PersonalFilesDocuments:
			SHGetSpecialFolderLocation(NIL, CSIDL_PERSONAL, &idlist);
			break;
		case PersonalFilesPictures:
			SHGetSpecialFolderLocation(NIL, CSIDL_MYPICTURES, &idlist);
			break;
		case PersonalFilesMusic:
			SHGetSpecialFolderLocation(NIL, CSIDL_MYMUSIC, &idlist);
			break;
		case PersonalFilesMovies:
			if (Backends::BackendWin32::IsWindowsVersionAtLeast(VER_PLATFORM_WIN32_NT, 5, 1)) SHGetSpecialFolderLocation(NIL, CSIDL_MYVIDEO, &idlist);
			else										  SHGetSpecialFolderLocation(NIL, CSIDL_PERSONAL, &idlist);
			break;
		case PersonalFilesDownloads:
			SHGetSpecialFolderLocation(NIL, CSIDL_PERSONAL, &idlist);
			break;
	}

	SHGetPathFromIDList(idlist, buffer);

	personalDir[type] = buffer;

	CoTaskMemFree(idlist);

	if (personalDir[type] == NIL) personalDir[type] = "C:";
#else
	passwd	*pw = getpwuid(getuid());

	if (pw != NIL)	personalDir[type] = pw->pw_dir;
	else		personalDir[type] = "~";

#ifdef __APPLE__
	OSErr	 error = -1;
	FSRef	 entry;

	switch (type)
	{
		default:
		case PersonalFilesGeneric:
			break;
		case PersonalFilesDocuments:
			error = FSFindFolder(kUserDomain, kDocumentsFolderType, kDontCreateFolder, &entry);
			break;
		case PersonalFilesPictures:
			error = FSFindFolder(kUserDomain, kPictureDocumentsFolderType, kDontCreateFolder, &entry);
			break;
		case PersonalFilesMusic:
			error = FSFindFolder(kUserDomain, kMusicDocumentsFolderType, kDontCreateFolder, &entry);
			break;
		case PersonalFilesMovies:
			error = FSFindFolder(kUserDomain, kMovieDocumentsFolderType, kDontCreateFolder, &entry);
			break;
		case PersonalFilesDownloads:
			break;
	}

	if (error == noErr)
	{
		CFURLRef	 url = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &entry);

		if (url != NIL)
		{
			CFStringRef	 path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);

			if (path != NIL)
			{
				Buffer<char>	 buffer(CFStringGetLength(path) * 4 + 1);

				CFStringGetCString(path, buffer, buffer.Size(), kCFStringEncodingUTF8);

				personalDir[type] = buffer;

				CFRelease(path);
			}

			CFRelease(url);
		}
	}
#else
	String	 configHome = getenv("XDG_CONFIG_HOME");

	if (configHome == NIL) configHome = String(personalDir[type]).Append("/.config");

	if (File(String(configHome).Append("/user-dirs.dirs")).Exists())
	{
		IO::InStream	 in(IO::STREAM_FILE, String(configHome).Append("/user-dirs.dirs"), IO::IS_READ);

		while (in.GetPos() < in.Size())
		{
			String	 entry = in.InputLine();

			if ((type == PersonalFilesDocuments && entry.StartsWith("XDG_DOCUMENTS_DIR")) ||
			    (type == PersonalFilesPictures  && entry.StartsWith("XDG_PICTURES_DIR" )) ||
			    (type == PersonalFilesMusic	    && entry.StartsWith("XDG_MUSIC_DIR"	   )) ||
			    (type == PersonalFilesMovies    && entry.StartsWith("XDG_VIDEOS_DIR"   )) ||
			    (type == PersonalFilesDownloads && entry.StartsWith("XDG_DOWNLOAD_DIR" )))
			{
				personalDir[type] = entry.SubString(entry.Find("\"") + 1, entry.FindLast("\"") - entry.Find("\"") - 1).Replace("$HOME", personalDir[type]);

				break;
			}
		}
	}
#endif
#endif

	if (!personalDir[type].EndsWith(Directory::GetDirectoryDelimiter())) personalDir[type].Append(Directory::GetDirectoryDelimiter());
	if ( personalDir[type] == Directory::GetDirectoryDelimiter())	     personalDir[type] = NIL;

	return personalDir[type];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movl %esi, %r15d
movq %rdi, 0x38(%rsp)
movb 0xbf6af(%rip), %al      # 0x16c2f0
testb %al, %al
je 0xad10b
movl %r15d, %eax
imulq $0x18, %rax, %rax
leaq 0xbf609(%rip), %r14     # 0x16c260
addq %rax, %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x57d40
testb %al, %al
je 0xacc80
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x58280
movq %rbx, %rax
jmp 0xad0f9
leaq 0x5553f(%rip), %rsi     # 0x1021c6
leaq 0xb8(%rsp), %rdi
callq 0x5a450
callq 0x57170
movl %eax, %edi
callq 0x56a10
testq %rax, %rax
je 0xaccb3
movq 0x20(%rax), %rsi
movq %r14, %rdi
callq 0x57010
jmp 0xaccc2
leaq 0x36579(%rip), %rsi     # 0xe3233
movq %r14, %rdi
callq 0x57010
leaq 0x55a85(%rip), %rdi     # 0x10274e
callq 0x58950
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x58c40
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0x56280
testb %al, %al
je 0xacd42
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
callq 0x58280
leaq 0x55a5c(%rip), %rsi     # 0x10275e
leaq 0x8(%rsp), %rdi
callq 0x58c40
leaq 0xd0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x573e0
leaq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x56630
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0xd0(%rsp), %rdi
callq 0x5a588
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x58280
leaq 0x55a26(%rip), %rsi     # 0x10277e
leaq 0x20(%rsp), %rdi
callq 0x58c40
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x573e0
movq %rax, %r12
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x5a180
leaq 0xd0(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %r12, %rsi
callq 0x57940
leaq 0xd0(%rsp), %rdi
callq 0x580a0
movl %eax, %ebp
leaq 0xd0(%rsp), %rdi
callq 0x5a500
leaq 0x40(%rsp), %rdi
callq 0x5a588
leaq 0x20(%rsp), %rdi
callq 0x5a588
leaq 0x8(%rsp), %rdi
callq 0x5a588
testb %bpl, %bpl
je 0xad047
leaq 0x8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x58280
leaq 0x55990(%rip), %rsi     # 0x10277e
leaq 0x20(%rsp), %rdi
callq 0x58c40
leaq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x573e0
leaq 0xd0(%rsp), %rdi
pushq $0x3
popq %rsi
pushq $0x2
popq %rcx
movq %rax, %rdx
callq 0x59a20
leaq 0x20(%rsp), %r12
movq %r12, %rdi
callq 0x5a588
leaq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x5a588
decl %r15d
leaq 0x55653(%rip), %rbp     # 0x102494
cmpw $0x0, 0xd8(%rsp)
je 0xacf2f
movq 0x118(%rsp), %rax
cmpq 0xe0(%rsp), %rax
jge 0xad03a
movq %r13, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x58500
cmpl $0x4, %r15d
ja 0xacf22
movslq (%rbp,%r15,4), %rax
addq %rbp, %rax
jmpq *%rax
movq %r12, %rdi
leaq 0x558fa(%rip), %rsi     # 0x10278e
callq 0x58c40
movq %r13, %rdi
movq %r12, %rsi
callq 0x58a10
jmp 0xacf14
movq %r12, %rdi
leaq 0x5591e(%rip), %rsi     # 0x1027ce
callq 0x58c40
movq %r13, %rdi
movq %r12, %rsi
callq 0x58a10
jmp 0xacf14
movq %r12, %rdi
leaq 0x558e5(%rip), %rsi     # 0x1027b1
callq 0x58c40
movq %r13, %rdi
movq %r12, %rsi
callq 0x58a10
jmp 0xacf14
movq %r12, %rdi
leaq 0x558d7(%rip), %rsi     # 0x1027bf
callq 0x58c40
movq %r13, %rdi
movq %r12, %rsi
callq 0x58a10
jmp 0xacf14
movq %r12, %rdi
leaq 0x5589c(%rip), %rsi     # 0x1027a0
callq 0x58c40
movq %r13, %rdi
movq %r12, %rsi
callq 0x58a10
movl %eax, %ebx
movq %r12, %rdi
callq 0x5a588
testb %bl, %bl
jne 0xacf3e
movq %r13, %rdi
callq 0x5a588
jmp 0xace41
movw $0x5, 0x1c8(%rsp)
jmp 0xad03a
leaq 0x55807(%rip), %rsi     # 0x10274c
leaq 0x40(%rsp), %rdi
callq 0x58c40
leaq 0x8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x58830
movl %eax, %ebp
leaq 0x557e5(%rip), %rsi     # 0x10274c
leaq 0xa0(%rsp), %rdi
callq 0x58c40
leaq 0x8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x59d50
movl %eax, %r15d
leaq 0x557bc(%rip), %rsi     # 0x10274c
leaq 0x88(%rsp), %rdi
callq 0x58c40
leaq 0x8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x58830
incl %ebp
notl %eax
addl %eax, %r15d
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x5a2c0
leaq 0x5580e(%rip), %rsi     # 0x1027df
leaq 0x70(%rsp), %rdi
callq 0x58c40
leaq 0x20(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r14, %rdx
callq 0x58800
movq %r14, %rdi
movq %rax, %rsi
callq 0x56630
leaq 0x70(%rsp), %rdi
callq 0x5a588
leaq 0x20(%rsp), %rdi
callq 0x5a588
leaq 0x88(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x5a588
leaq 0x40(%rsp), %rdi
callq 0x5a588
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0xd0(%rsp), %rdi
callq 0x565f0
callq 0x563a0
leaq 0xd0(%rsp), %rdi
movq %rax, %rsi
callq 0x58c40
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x59e50
movl %eax, %ebp
leaq 0xd0(%rsp), %rdi
callq 0x5a588
testb %bpl, %bpl
jne 0xad0b2
callq 0x563a0
leaq 0xd0(%rsp), %rdi
movq %rax, %rsi
callq 0x58c40
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x573e0
leaq 0xd0(%rsp), %rdi
callq 0x5a588
callq 0x563a0
movq %r14, %rdi
movq %rax, %rsi
callq 0x59180
testb %al, %al
je 0xad0d0
movq %r14, %rdi
xorl %esi, %esi
callq 0x58c30
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x58280
leaq 0x58(%rsp), %rdi
callq 0x5a588
leaq 0xb8(%rsp), %rdi
callq 0x57b90
movq 0x38(%rsp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbf1de(%rip), %rdi     # 0x16c2f0
callq 0x57210
testl %eax, %eax
je 0xacc49
xorl %r14d, %r14d
leaq 0xbf137(%rip), %r12     # 0x16c260
leaq (%r12,%r14), %rdi
xorl %esi, %esi
callq 0x5a180
addq $0x18, %r14
cmpq $0x90, %r14
jne 0xad129
leaq -0x51b2f(%rip), %rdi    # 0x5b619
leaq 0xb70f1(%rip), %rdx     # 0x164240
xorl %esi, %esi
callq 0x590a0
leaq 0xbf193(%rip), %rdi     # 0x16c2f0
callq 0x57cf0
jmp 0xacc49
movq %rax, %rbx
testq %r14, %r14
je 0xad182
leaq (%r14,%r12), %rdi
addq $-0x18, %rdi
callq 0x5a588
addq $-0x18, %r14
jne 0xad16f
leaq 0xbf167(%rip), %rdi     # 0x16c2f0
callq 0x57520
jmp 0xad2bb
movq %rax, %rbx
jmp 0xad1b5
jmp 0xad19a
movq %rax, %rbx
jmp 0xad1d1
jmp 0xad1a1
movq %rax, %rbx
jmp 0xad1de
jmp 0xad214
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5a588
leaq 0x20(%rsp), %rdi
callq 0x5a588
jmp 0xad1c4
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x5a588
leaq 0x40(%rsp), %rdi
jmp 0xad20d
jmp 0xad225
jmp 0xad25e
jmp 0xad28f
jmp 0xad225
jmp 0xad28f
jmp 0xad205
jmp 0xad214
jmp 0xad205
jmp 0xad214
jmp 0xad205
jmp 0xad214
jmp 0xad205
jmp 0xad214
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5a588
jmp 0xad217
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5a588
jmp 0xad297
jmp 0xad28f
movq %rax, %rbx
jmp 0xad275
jmp 0xad24f
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x5a500
jmp 0xad241
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5a588
jmp 0xad252
jmp 0xad24f
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5a588
jmp 0xad261
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0xad27d
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0xd0(%rsp), %rdi
callq 0x5a588
jmp 0xad2a4
jmp 0xad28f
jmp 0xad28a
jmp 0xad28f
movq %rax, %rbx
jmp 0xad2ae
movq %rax, %rbx
jmp 0xad2a4
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x565f0
leaq 0x58(%rsp), %rdi
callq 0x5a588
leaq 0xb8(%rsp), %rdi
callq 0x57b90
movq %rbx, %rdi
callq 0x58260
nop