// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/capsimg/Core/DiskFile.cpp
// Function: CDiskFile::FindFile(char*, char const*, char const*)
int CDiskFile::FindFile(char *result, const char *filename, const char *filter)
{
	// result is not valid by default
	int resvalid = 0;

	// copy source as result by default
	int rescopy = 1;

	// stop if no buffer specified for the result
	if (!result)
		return resvalid;

	// the default result is an empty string
	result[0] = 0;

	// stop if the filename is not specified
	if (!filename)
		return resvalid;

	// length of path and file name components separated
	int pathlen = 0, namelen = 0;

	// wildcard usage counter, wildcards enabled
	int wcused = 0;
	int wcenabled = 1;

	// process the entire source filename or pattern
	for (int rpos = 0; wcenabled && filename[rpos]; rpos++) {
		namelen++;

		switch (filename[rpos]) {
			// if path delimiter found
		case '/':
		case '\\':
			// stop if a wilcard has already been used; only allow wildcards in filename part, not in the path
			if (wcused) {
				// disable write card processing; the result will be the path upto the illegal wildcard segment
				wcenabled = 0;
				break;
			}

			// set new path length as the entire name until now including the delimiter
			pathlen = rpos + 1;

			// clear the filename part
			namelen = 0;
			break;

			// count the wildcards
		case '?':
		case '*':
			wcused++;
			break;
		}
	}

	// if wildcards used
	if (wcused) {
		// and wilcardcards are enabled, ie no error in the path
		if (wcenabled) {
			char *pathbuf = NULL;
			const char *dirpath;

			// make a copy of the path if it's defined or use the current directory as the path
			if (pathlen) {
				pathbuf = new char[pathlen + 1];
				memcpy(pathbuf, filename, pathlen);
				pathbuf[pathlen] = 0;
				dirpath = pathbuf;
			} else
				dirpath = ".";

			// open the selected path
			DIR *pdir = opendir(dirpath);
			if (pdir) {
				dirent *pent;
				const char *pattern = filename + pathlen;

				// iterate all directory entries
				while (pent = readdir(pdir)) {
					// skip any entry that is not a regular file
#if defined _DIRENT_HAVE_D_TYPE || defined HAVE_STRUCT_DIRENT_D_TYPE
					if (pent->d_type != DT_REG)
						continue;
#endif

					// check entry to match the filename pattern
					char *fn = pent->d_name;
					if (FileNameMatch(pattern, fn)) {
						// if matches, check entry to match the filter pattern if filter is specified
						if (!filter || FileNameMatch(filter, fn)) {
							// success; copy the original path and add the entry found
							memcpy(result, filename, pathlen);
							strcpy(result + pathlen, fn);

							// keep this result, don't copy the source
							rescopy = 0;

							// result is valid
							resvalid = 1;
							break;
						}
					}
				}

				closedir(pdir);
			}

			// free path buffer
			delete[] pathbuf;
		}
	} else {
		// wilcards unused, result is valid
		resvalid = 1;
	}

	// copy the source if that's the result
	if (rescopy) {
		int reslen = pathlen + namelen;
		memcpy(result, filename, reslen);
		result[reslen] = 0;
	}

	return resvalid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %r12d, %r12d
testq %rdi, %rdi
je 0xe512
movq %rsi, %r14
movq %rdi, %r13
movb $0x0, (%rdi)
testq %rsi, %rsi
je 0xe512
movb (%r14), %sil
testb %sil, %sil
je 0xe3f0
xorl %r15d, %r15d
movl $0x1, %eax
xorl %ecx, %ecx
xorl %ebx, %ebx
incl %r15d
movzbl %sil, %esi
cmpl $0x3e, %esi
jg 0xe3c4
cmpl $0x2a, %esi
je 0xe3db
cmpl $0x2f, %esi
je 0xe3ce
jmp 0xe3dd
cmpl $0x3f, %esi
je 0xe3db
cmpl $0x5c, %esi
jne 0xe3dd
testl %ecx, %ecx
jne 0xe44d
xorl %r15d, %r15d
xorl %ecx, %ecx
movl %eax, %ebx
jmp 0xe3dd
incl %ecx
movb (%r14,%rax), %sil
incq %rax
testb %sil, %sil
jne 0xe3ac
testl %ecx, %ecx
sete %al
jmp 0xe3f7
movb $0x1, %al
xorl %ebx, %ebx
xorl %r15d, %r15d
movl $0x1, %r12d
testb %al, %al
jne 0xe4f8
movq %rdx, 0x18(%rsp)
movq %r13, 0x8(%rsp)
testl %ebx, %ebx
je 0xe455
leal 0x1(%rbx), %eax
movslq %eax, %rdi
callq 0x4040
movq %rax, %r13
movslq %ebx, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4190
movb $0x0, (%r13,%r12)
movq %r13, %rdi
callq 0x42f0
movq %rax, %rbp
testq %rax, %rax
jne 0xe473
jmp 0xe4e8
xorl %r12d, %r12d
jmp 0xe4f8
leaq 0x3fca(%rip), %rdi      # 0x12426
callq 0x42f0
xorl %r12d, %r12d
testq %rax, %rax
je 0xe564
movq %rax, %rbp
xorl %r13d, %r13d
movq %r13, 0x10(%rsp)
movq %rbp, %rdi
callq 0x4300
testq %rax, %rax
je 0xe4d6
movq %rax, %r13
leaq (%r14,%r12), %rax
movq %rax, 0x20(%rsp)
cmpb $0x8, 0x12(%r13)
jne 0xe4c6
addq $0x13, %r13
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0xe568
testl %eax, %eax
je 0xe4c6
cmpq $0x0, 0x18(%rsp)
je 0xe524
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xe568
testl %eax, %eax
jne 0xe524
movq %rbp, %rdi
callq 0x4300
movq %rax, %r13
testq %rax, %rax
jne 0xe491
movq %rbp, %rdi
callq 0x4320
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0xe4f0
movq %r13, %rdi
callq 0x4280
xorl %r12d, %r12d
movq 0x8(%rsp), %r13
addl %ebx, %r15d
movslq %r15d, %r15
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x4190
movb $0x0, (%r13,%r15)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x4190
addq %r12, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x41e0
movq %rbp, %rdi
callq 0x4320
movl $0x1, %r12d
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0xe512
callq 0x4280
jmp 0xe512
xorl %ebx, %ebx
jmp 0xe4f3