Further filesystem fixes.

This commit is contained in:
Par Winzell 2019-04-07 18:32:39 -07:00
parent e53b2fffb1
commit 02e5eb684d
4 changed files with 18 additions and 23 deletions

View File

@ -264,7 +264,7 @@ int main(int argc, char* argv[]) {
if (outputPath.empty()) { if (outputPath.empty()) {
// if -o is not given, default to the basename of the .fbx // if -o is not given, default to the basename of the .fbx
outputPath = "./" + FileUtils::GetFileBaseString(inputPath); outputPath = "./" + FileUtils::GetFileBase(inputPath);
} }
// the output folder in .gltf mode, not used for .glb // the output folder in .gltf mode, not used for .glb
std::string outputFolder; std::string outputFolder;
@ -273,7 +273,7 @@ int main(int argc, char* argv[]) {
std::string modelPath; std::string modelPath;
if (gltfOptions.outputBinary) { if (gltfOptions.outputBinary) {
const auto& suffix = FileUtils::GetFileSuffix(outputPath); const auto& suffix = FileUtils::GetFileSuffix(outputPath);
// add .glb to output path, unless it already ends in exactly that // add .glb to output path, unless it already ends in exactly that
if (suffix.has_value() && suffix.value() == "glb") { if (suffix.has_value() && suffix.value() == "glb") {
modelPath = outputPath; modelPath = outputPath;
} else { } else {
@ -282,7 +282,7 @@ int main(int argc, char* argv[]) {
} else { } else {
// in gltf mode, we create a folder and write into that // in gltf mode, we create a folder and write into that
outputFolder = fmt::format("{}_out/", outputPath.c_str()); outputFolder = fmt::format("{}_out/", outputPath.c_str());
modelPath = outputFolder + FileUtils::GetFileNameString(outputPath) + ".gltf"; modelPath = outputFolder + FileUtils::GetFileName(outputPath) + ".gltf";
} }
if (!FileUtils::CreatePath(modelPath.c_str())) { if (!FileUtils::CreatePath(modelPath.c_str())) {
fmt::fprintf(stderr, "ERROR: Failed to create folder: %s'\n", outputFolder.c_str()); fmt::fprintf(stderr, "ERROR: Failed to create folder: %s'\n", outputFolder.c_str());

View File

@ -924,8 +924,7 @@ static std::string GetInferredFileName(
return fbxFileName; return fbxFileName;
} }
// Get the file name with file extension. // Get the file name with file extension.
const std::string fileName = const std::string fileName = FileUtils::GetFileName(fbxFileName);
FileUtils::GetFileNameString(FileUtils::GetCanonicalPath(fbxFileName));
// Try to find a match with extension. // Try to find a match with extension.
for (const auto& file : directoryFileList) { for (const auto& file : directoryFileList) {
@ -935,12 +934,12 @@ static std::string GetInferredFileName(
} }
// Get the file name without file extension. // Get the file name without file extension.
const std::string fileBase = FileUtils::GetFileBaseString(fileName); const std::string fileBase = FileUtils::GetFileBase(fileName);
// Try to find a match without file extension. // Try to find a match without file extension.
for (const auto& file : directoryFileList) { for (const auto& file : directoryFileList) {
// If the two extension-less base names match. // If the two extension-less base names match.
if (StringUtils::CompareNoCase(fileBase, FileUtils::GetFileBaseString(file)) == 0) { if (StringUtils::CompareNoCase(fileBase, FileUtils::GetFileBase(file)) == 0) {
// Return the name with extension of the file in the directory. // Return the name with extension of the file in the directory.
return std::string(directory) + file; return std::string(directory) + file;
} }
@ -964,10 +963,10 @@ static void FindFbxTextures(
const std::set<std::string>& extensions, const std::set<std::string>& extensions,
std::map<const FbxTexture*, FbxString>& textureLocations) { std::map<const FbxTexture*, FbxString>& textureLocations) {
// Get the folder the FBX file is in. // Get the folder the FBX file is in.
const std::string folder = FileUtils::GetFolderString(fbxFileName); const std::string folder = FileUtils::getFolder(fbxFileName);
// Check if there is a filename.fbm folder to which embedded textures were extracted. // Check if there is a filename.fbm folder to which embedded textures were extracted.
const std::string fbmFolderName = folder + FileUtils::GetFileBaseString(fbxFileName) + ".fbm/"; const std::string fbmFolderName = folder + FileUtils::GetFileBase(fbxFileName) + ".fbm/";
// Search either in the folder with embedded textures or in the same folder as the FBX file. // Search either in the folder with embedded textures or in the same folder as the FBX file.
const std::string searchFolder = FileUtils::FolderExists(fbmFolderName) ? fbmFolderName : folder; const std::string searchFolder = FileUtils::FolderExists(fbmFolderName) ? fbmFolderName : folder;

View File

@ -49,7 +49,7 @@ std::shared_ptr<TextureData> TextureBuilder::combine(
if (rawTexIx >= 0) { if (rawTexIx >= 0) {
const RawTexture& rawTex = raw.GetTexture(rawTexIx); const RawTexture& rawTex = raw.GetTexture(rawTexIx);
const std::string& fileLoc = rawTex.fileLocation; const std::string& fileLoc = rawTex.fileLocation;
const std::string& name = FileUtils::GetFileBaseString(FileUtils::GetFileNameString(fileLoc)); const std::string& name = FileUtils::GetFileBase(FileUtils::GetFileName(fileLoc));
if (!fileLoc.empty()) { if (!fileLoc.empty()) {
info.pixels = stbi_load(fileLoc.c_str(), &info.width, &info.height, &info.channels, 0); info.pixels = stbi_load(fileLoc.c_str(), &info.width, &info.height, &info.channels, 0);
if (!info.pixels) { if (!info.pixels) {
@ -179,8 +179,8 @@ std::shared_ptr<TextureData> TextureBuilder::simple(int rawTexIndex, const std::
} }
const RawTexture& rawTexture = raw.GetTexture(rawTexIndex); const RawTexture& rawTexture = raw.GetTexture(rawTexIndex);
const std::string textureName = FileUtils::GetFileBaseString(rawTexture.name); const std::string textureName = FileUtils::GetFileBase(rawTexture.name);
const std::string relativeFilename = FileUtils::GetFileNameString(rawTexture.fileLocation); const std::string relativeFilename = FileUtils::GetFileName(rawTexture.fileLocation);
ImageData* image = nullptr; ImageData* image = nullptr;
if (options.outputBinary) { if (options.outputBinary) {
@ -202,7 +202,7 @@ std::shared_ptr<TextureData> TextureBuilder::simple(int rawTexIndex, const std::
} else if (!relativeFilename.empty()) { } else if (!relativeFilename.empty()) {
image = new ImageData(relativeFilename, relativeFilename); image = new ImageData(relativeFilename, relativeFilename);
std::string outputPath = FileUtils::GetCanonicalPath(outputFolder + "/" + relativeFilename); std::string outputPath = outputFolder + "/" + relativeFilename;
if (FileUtils::CopyFile(rawTexture.fileLocation, outputPath, true)) { if (FileUtils::CopyFile(rawTexture.fileLocation, outputPath, true)) {
if (verboseOutput) { if (verboseOutput) {
fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath); fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath);

View File

@ -46,24 +46,20 @@ inline bool FolderExists(const std::string& folderPath) {
return std::filesystem::exists(folderPath) && std::filesystem::is_directory(folderPath); return std::filesystem::exists(folderPath) && std::filesystem::is_directory(folderPath);
} }
inline std::string GetFolderString(const std::string& path) { inline std::string getFolder(const std::string& path) {
return std::filesystem::path(path).parent_path().string(); return std::filesystem::path(path).parent_path().string();
} }
inline std::string GetCanonicalPath(const std::string& path) { inline std::string GetFileName(const std::string& path) {
return std::filesystem::canonical(path).string(); return std::filesystem::path(path).filename().string();
} }
inline std::string GetFileNameString(const std::string& path) { inline std::string GetFileBase(const std::string& path) {
return std::filesystem::canonical(path).filename().string(); return std::filesystem::path(path).stem().string();
}
inline std::string GetFileBaseString(const std::string& path) {
return std::filesystem::canonical(path).stem().string();
} }
inline std::optional<std::string> GetFileSuffix(const std::string& path) { inline std::optional<std::string> GetFileSuffix(const std::string& path) {
const auto& extension = std::filesystem::canonical(path).extension(); const auto& extension = std::filesystem::path(path).extension();
if (extension.empty()) { if (extension.empty()) {
return std::nullopt; return std::nullopt;
} }