ezStringView ezPathUtils::GetFileName(const char* szPath, const char* szPathEnd) { // make sure szPathEnd is valid ezStringUtils::UpdateStringEnd(szPath, szPathEnd); const char* szSeparator = FindPreviousSeparator(szPath, szPathEnd); const char* szDot = ezStringUtils::FindLastSubString(szPath, ".", szPathEnd); if (szDot < szSeparator) // includes (szDot == nullptr), szSeparator will never be nullptr here -> no extension { return ezStringView(szSeparator + 1, szPathEnd); } if (szSeparator == nullptr) { if (szDot == nullptr) // no folder, no extension -> the entire thing is just a name return ezStringView(szPath, szPathEnd); return ezStringView(szPath, szDot); // no folder, but an extension -> remove the extension } // now: there is a separator AND an extension return ezStringView(szSeparator + 1, szDot); }
ezStringView ezPathUtils::GetFileNameAndExtension(const char* szPath, const char* szPathEnd) { ezStringUtils::UpdateStringEnd(szPath, szPathEnd); const char* szSeparator = FindPreviousSeparator(szPath, szPathEnd); if (szSeparator == nullptr) return ezStringView(szPath, szPathEnd); return ezStringView(szSeparator + 1, szPathEnd); }
ezStringView ezPathUtils::GetFileExtension(const char* szPath, const char* szPathEnd) { ezStringUtils::UpdateStringEnd(szPath, szPathEnd); const char* szDot = ezStringUtils::FindLastSubString(szPath, ".", nullptr, szPathEnd); if (szDot == nullptr) return ezStringView(nullptr); // find the last separator in the string const char* szSeparator = FindPreviousSeparator(szPath, szPathEnd); if (szSeparator > szDot) return ezStringView(nullptr); return ezStringView(szDot + 1, szPathEnd); }
ezStringView ezPathUtils::GetFileDirectory(const char* szPath, const char* szPathEnd) { // make sure szPathEnd is valid ezStringUtils::UpdateStringEnd(szPath, szPathEnd); ezStringView end (szPath, szPathEnd); end.ResetToBack(); // if it already ends in a path separator, do not return a different directory if (IsPathSeparator(end.GetCharacter())) return ezStringView(szPath, szPathEnd); // find the last separator in the string const char* szSeparator = FindPreviousSeparator(szPath, szPathEnd); // no path separator -> root dir -> return the empty path if (szSeparator == nullptr) return ezStringView(nullptr); return ezStringView(szPath, szSeparator + 1); }
void ezCommandLineUtils::SplitCommandLineString(const char* commandString, bool addExecutableDir, ezDynamicArray<ezString>& outArgs, ezDynamicArray<const char*>& outArgsV) { // Add application dir as first argument as customary on other platforms. if (addExecutableDir) { #if EZ_ENABLED(EZ_PLATFORM_WINDOWS) wchar_t moduleFilename[256]; GetModuleFileNameW(nullptr, moduleFilename, 256); outArgs.PushBack(ezStringUtf8(moduleFilename).GetData()); #else EZ_ASSERT_NOT_IMPLEMENTED; #endif } // Simple args splitting. Not as powerful as Win32's CommandLineToArgvW. const char* currentChar = commandString; const char* lastEnd = currentChar; bool inQuotes = false; while (*currentChar != '\0') { if (*currentChar == '\"') inQuotes = !inQuotes; else if (*currentChar == ' ' && !inQuotes) { ezStringBuilder path = ezStringView(lastEnd, currentChar); path.Trim(" \""); outArgs.PushBack(path); lastEnd = currentChar + 1; } ezUnicodeUtils::MoveToNextUtf8(currentChar); } outArgsV.Reserve(outArgsV.GetCount()); for (ezString& str : outArgs) outArgsV.PushBack(str.GetData()); }