diff --git a/BaseLib/FileTools.cpp b/BaseLib/FileTools.cpp index bbf7f4b14de1238180b615d1dd50c1cfdff9e788..124ba0bfea864e3445d91965d5a23b9d96e3d167 100644 --- a/BaseLib/FileTools.cpp +++ b/BaseLib/FileTools.cpp @@ -12,6 +12,7 @@ */ #include "FileTools.h" +#include "StringTools.h" #include <sys/stat.h> @@ -59,56 +60,80 @@ void truncateFile( std::string const& filename) ofs.close(); } -std::string getFileNameFromPath(const std::string &str, bool with_extension) +/** Finds the position of last file path separator. + * Checks for unix or windows file path separators in given path and returns the + * position of the last one or std::string::npos if no file path separator was + * found. + */ +size_t findLastPathSeparator(std::string const& path) +{ + return path.find_last_of("/\\"); +} + +/** Finds the position of last dot. + * This could be used to extract file extension. + */ +size_t findLastDot(std::string const& path) +{ + return path.find_last_of("."); +} + +/** Returns a string with file extension as found by getFileExtension() + * dropped. + */ +std::string dropFileExtension(std::string const& filename) +{ + const size_t p = findLastDot(filename); + if (p == std::string::npos) + return filename; + + return filename.substr(0, filename.length() - p); +} + +std::string extractBaseName(std::string const& pathname) +{ + const size_t p = findLastPathSeparator(pathname); + return pathname.substr(p+1); +} + +std::string extractBaseNameWithoutExtension(std::string const& pathname) +{ + std::string basename = extractBaseName(pathname); + return dropFileExtension(basename); +} + +std::string getFileExtension(const std::string &path) { - std::string::size_type beg1 = str.find_last_of('/'); - std::string::size_type beg2 = str.find_last_of('\\'); - std::string::size_type beg; - if (beg1 == std::string::npos && beg2 == std::string::npos) beg = -1; - else if (beg1 == std::string::npos) beg = beg2; - else if (beg2 == std::string::npos) beg = beg1; - else beg = (beg1<beg2) ? beg2 : beg1; - std::string file ( str.substr(beg+1) ); - if (with_extension) return file; - // cut extension - std::string::size_type end = file.find_last_of('.'); - return file.substr(0,end); + const std::string str = extractBaseName(path); + const size_t p = findLastDot(str); + if (p == std::string::npos) + return std::string(); + return str.substr(p + 1); } -std::string getSuffixFromPath(const std::string &str) +bool hasFileExtension(std::string const& extension, std::string const& filename) { - std::string::size_type beg(str.find_last_of('.')); - return str.substr(beg+1, str.length()-beg-1); + std::string ext = stringToUpper(extension); // Copy for modification. + std::string file_ext = stringToUpper(getFileExtension(filename)); + + return ext == file_ext; } -std::string copyPathToFileName(const std::string &file_name, const std::string &source) +std::string copyPathToFileName(const std::string &file_name, + const std::string &source) { // check if file_name already contains a full path - size_t pos(file_name.rfind("/")); // linux, mac delimiter - if (pos == std::string::npos) - { - pos = file_name.rfind("\\"); // windows delimiter - if (pos == std::string::npos) - { - std::string path; - BaseLib::extractPath(source, path); - return path.append(file_name); - } - else return std::string(file_name); - } - else return std::string(file_name); + const size_t pos = findLastPathSeparator(file_name); + if (pos != std::string::npos) + return file_name; + + return BaseLib::extractPath(source).append(file_name); } -void extractPath(std::string const& fname, std::string& path) +std::string extractPath(std::string const& pathname) { - // extract path for reading external files - size_t pos(fname.rfind("/")); // linux, mac delimiter - if (pos == std::string::npos) { - pos = fname.rfind("\\"); // windows delimiter - if (pos == std::string::npos) - pos = 0; - } - path = fname.substr(0, pos==0 ? pos : pos + 1); + const size_t pos = findLastPathSeparator(pathname); + return pathname.substr(0, pos + 1); } } // end namespace BaseLib diff --git a/BaseLib/FileTools.h b/BaseLib/FileTools.h index 2649ac0f81f59f9afbbd5f7c0e43ea45e0d743ed..50cbc3282d56fc74b3cce165f7eb2def11596584 100644 --- a/BaseLib/FileTools.h +++ b/BaseLib/FileTools.h @@ -48,27 +48,45 @@ template <typename T> void writeValueBinary(std::ostream &out, T const& val) void truncateFile( std::string const& file_path); /** - * Extract the filename from a path + * Extracts basename from given pathname with extension. + * + * Returns a string containing everything after the last path separator. + * If the the pathname does not contain a path separator original pathname is + * returned. + */ +std::string extractBaseName(std::string const& pathname); + +/** + * Extracts basename from given pathname without its extension. + * + * Same as extractBaseName(), but drops the file extension too. */ -std::string getFileNameFromPath(const std::string &str, bool with_extension = false); +std::string extractBaseNameWithoutExtension(std::string const& pathname); /** - * Extract the file type / suffix from a path + * Extract extension from filename */ -std::string getSuffixFromPath(const std::string &str); +std::string getFileExtension(std::string const& filename); +/** + * Compares filename's extension with query extension. The comparison is case + * insensitive done by converting to upper case with the std::toupper() + * function. + */ +bool hasFileExtension(std::string const& extension, std::string const& filename); /** - * Checks if file_name already contains a qualified path and if not copies the path from source. + * Checks if file_name already contains a qualified path and if not copies the + * path from source. */ std::string copyPathToFileName(const std::string &file_name, const std::string &source); /** - * extracts the path of a fully qualified path name of the file - * @param fname [input] the fully qualified path name of the file - * @param path [output] the path of the fully qualified path name of the file + * Extracts the path of a pathname. + * + * Returns a string up to the last path separator not including it. */ -void extractPath(std::string const& fname, std::string& path); +std::string extractPath(std::string const& pathname); } // end namespace BaseLib #endif // FILETOOLS_H diff --git a/BaseLib/StringTools.cpp b/BaseLib/StringTools.cpp index e752f605f9ec5168b04a0adc7eb5688b87462903..dda2c607923479a05598be69754a0c2004562c97 100644 --- a/BaseLib/StringTools.cpp +++ b/BaseLib/StringTools.cpp @@ -12,6 +12,9 @@ #include "StringTools.h" +#include <algorithm> +#include <cctype> + namespace BaseLib { std::list<std::string> splitString(const std::string &str, char delim) @@ -49,6 +52,13 @@ void trim(std::string &str, char ch) else str.erase(str.begin(), str.end()); } +std::string stringToUpper(std::string const& str) +{ + std::string s = str; + std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) std::toupper); + return s; +} + } // end namespace BaseLib diff --git a/BaseLib/StringTools.h b/BaseLib/StringTools.h index 29061d08d24ec1d551ed44479826416033abbe3e..6b4bbca2cd38cc519644f970c9897bef010e6817 100644 --- a/BaseLib/StringTools.h +++ b/BaseLib/StringTools.h @@ -70,6 +70,13 @@ template<typename T> T str2number (const std::string &str) */ void trim(std::string &str, char ch=' '); +/** + * Returs same string with all characters in upper case. + * + * This uses std::toupper() function, and does not care about unicode. + */ +std::string stringToUpper(std::string const& str); + } // end namespace BaseLib #ifdef MSVC diff --git a/FileIO/GMSInterface.cpp b/FileIO/GMSInterface.cpp index a65c3cd22984813df4c96d3f1f73a237004d5121..3559d4d1ba3fa16ba8788b2097eda8d9dcfcd00b 100644 --- a/FileIO/GMSInterface.cpp +++ b/FileIO/GMSInterface.cpp @@ -328,6 +328,6 @@ MeshLib::Mesh* GMSInterface::readGMS3DMMesh(std::string filename) in.close(); std::cout << "finished" << std::endl; - std::string mesh_name (BaseLib::getFileNameFromPath(filename)); + const std::string mesh_name = BaseLib::extractBaseNameWithoutExtension(filename); return new MeshLib::Mesh(mesh_name, nodes, elements); } diff --git a/FileIO/Legacy/MeshIO.cpp b/FileIO/Legacy/MeshIO.cpp index 0717314aaf01c4d973167b7486b8d3423f409e51..ca866900e235b13a0049710a1a2eb40372e3eb11 100644 --- a/FileIO/Legacy/MeshIO.cpp +++ b/FileIO/Legacy/MeshIO.cpp @@ -107,7 +107,7 @@ MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name) } - MeshLib::Mesh* mesh (new MeshLib::Mesh(BaseLib::getFileNameFromPath(file_name), nodes, elements)); + MeshLib::Mesh* mesh (new MeshLib::Mesh(BaseLib::extractBaseNameWithoutExtension(file_name), nodes, elements)); mesh->setEdgeLengthRange(sqrt(edge_length[0]), sqrt(edge_length[1])); std::cout << "finished." << std::endl; diff --git a/FileIO/Legacy/OGSIOVer4.cpp b/FileIO/Legacy/OGSIOVer4.cpp index 2d41e601fdd322bb1e1393eb94b9827fb1005f38..3f9332e415d3acfe49fad319d3b6d29bde9d37a1 100644 --- a/FileIO/Legacy/OGSIOVer4.cpp +++ b/FileIO/Legacy/OGSIOVer4.cpp @@ -551,13 +551,12 @@ bool readGLIFileV4(const std::string& fname, GEOObjects* geo, std::string& uniqu tag = readPoints(in, pnt_vec, zero_based_idx, pnt_id_names_map); std::cout << " ok, " << pnt_vec->size() << " points read" << std::endl; - unique_name = BaseLib::getFileNameFromPath(fname, true); + unique_name = BaseLib::extractBaseName(fname); if (!pnt_vec->empty()) geo->addPointVec(pnt_vec, unique_name, pnt_id_names_map); // KR: insert into GEOObjects if not empty // extract path for reading external files - std::string path; - BaseLib::extractPath(fname, path); + const std::string path = BaseLib::extractPath(fname); // read names of plys into temporary string-vec std::map<std::string,size_t>* ply_names (new std::map<std::string,size_t>); @@ -669,8 +668,7 @@ void writeAllDataToGLIFileV4 (const std::string& fname, const GeoLib::GEOObjects geo.getGeometryNames (geo_names); // extract path for reading external files - std::string path; - BaseLib::extractPath(fname, path); + const std::string path = BaseLib::extractPath(fname); std::ofstream os (fname.c_str()); diff --git a/FileIO/MeshIO/GMSHInterface.cpp b/FileIO/MeshIO/GMSHInterface.cpp index 7c60bad6f89ebe029638bafd24b6113b8dfb0b1e..9ab147541f9cb27f0ca948785da8b2f5f4a956a0 100644 --- a/FileIO/MeshIO/GMSHInterface.cpp +++ b/FileIO/MeshIO/GMSHInterface.cpp @@ -211,7 +211,7 @@ MeshLib::Mesh* GMSHInterface::readGMSHMesh(std::string const& fname) } } in.close(); - return new MeshLib::Mesh(BaseLib::getFileNameFromPath(fname), nodes, elements); + return new MeshLib::Mesh(BaseLib::extractBaseNameWithoutExtension(fname), nodes, elements); } void GMSHInterface::readNodeIDs(std::ifstream &in, unsigned n_nodes, std::vector<unsigned> &node_ids, std::map<unsigned, unsigned> &id_map) diff --git a/FileIO/RapidXmlIO/RapidVtuInterface.cpp b/FileIO/RapidXmlIO/RapidVtuInterface.cpp index 1dc0bd180c83eb653ab08f5289ebf52d40eb658b..154452a6d8961e29ddb58edf5129af6a8f5c547c 100644 --- a/FileIO/RapidXmlIO/RapidVtuInterface.cpp +++ b/FileIO/RapidXmlIO/RapidVtuInterface.cpp @@ -178,7 +178,7 @@ MeshLib::Mesh* RapidVtuInterface::readVTUFile(const std::string &file_name) std::cout << "Nr. Nodes: " << nodes.size() << std::endl; std::cout << "Nr. Elements: " << elements.size() << std::endl; delete [] buffer; - return new MeshLib::Mesh(BaseLib::getFileNameFromPath(file_name), nodes, elements); + return new MeshLib::Mesh(BaseLib::extractBaseNameWithoutExtension(file_name), nodes, elements); } else { std::cout << "Error in RapidVtuInterface::readVTUFile() - Number of nodes and elements not specified." << std::endl; diff --git a/FileIO/readMeshFromFile.cpp b/FileIO/readMeshFromFile.cpp index faa38a619bf1b10d0f8f8e40b89d53ec60cda4fc..9ffa784b16ae5d1fb4e6a6f94bfa6b5bdc7b0d96 100644 --- a/FileIO/readMeshFromFile.cpp +++ b/FileIO/readMeshFromFile.cpp @@ -23,20 +23,19 @@ namespace FileIO { MeshLib::Mesh* readMeshFromFile(const std::string &file_name) { - MeshLib::Mesh* mesh (NULL); - std::string suffix (BaseLib::getSuffixFromPath(file_name)); - - if (suffix.compare("msh") == 0 || suffix.compare("MSH") == 0) + if (BaseLib::hasFileExtension("msh", file_name)) { FileIO::MeshIO meshIO; - mesh = meshIO.loadMeshFromFile(file_name); + return meshIO.loadMeshFromFile(file_name); + } + + if (BaseLib::hasFileExtension("vtu", file_name)) + { + return FileIO::RapidVtuInterface::readVTUFile(file_name); } - else if (suffix.compare("vtu") == 0 || suffix.compare("VTU") == 0) - mesh = FileIO::RapidVtuInterface::readVTUFile(file_name); - else - std::cout << "Unknown mesh file format" << std::endl; - return mesh; + std::cout << "Unknown mesh file format" << std::endl; + return 0; } } diff --git a/SimpleTests/MeshTests/MeshRead.cpp b/SimpleTests/MeshTests/MeshRead.cpp index a45edf5ffb61a57a238b8c756f06d44b42e65f98..9576e9dc5ebd8c6d6113d27ed57ec2cf9b710f03 100644 --- a/SimpleTests/MeshTests/MeshRead.cpp +++ b/SimpleTests/MeshTests/MeshRead.cpp @@ -16,8 +16,7 @@ #include "logog.hpp" // FileIO -#include "RapidXmlIO/RapidVtuInterface.h" -#include "Legacy/MeshIO.h" +#include "readMeshFromFile.h" // MeshLib #include "Node.h" @@ -47,19 +46,13 @@ int main(int argc, char *argv[]) std::string fname (mesh_arg.getValue()); - FileIO::MeshIO mesh_io; #ifndef WIN32 BaseLib::MemWatch mem_watch; unsigned long mem_without_mesh (mem_watch.getVirtMemUsage()); #endif BaseLib::RunTime run_time; run_time.start(); - MeshLib::Mesh* mesh(NULL); - if (BaseLib::getSuffixFromPath(fname).compare("msh") == 0) { - mesh = mesh_io.loadMeshFromFile(fname); - } else { - mesh = FileIO::RapidVtuInterface::readVTUFile(fname); - } + MeshLib::Mesh* mesh = FileIO::readMeshFromFile(fname); #ifndef WIN32 unsigned long mem_with_mesh (mem_watch.getVirtMemUsage()); // std::cout << "mem for mesh: " << (mem_with_mesh - mem_without_mesh)/(1024*1024) << " MB" << std::endl; diff --git a/Tests/BaseLib/TestFilePathStringManipulation.cpp b/Tests/BaseLib/TestFilePathStringManipulation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a40f5565b6c72440ed550ce192b401c705758e --- /dev/null +++ b/Tests/BaseLib/TestFilePathStringManipulation.cpp @@ -0,0 +1,329 @@ +/** + * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + */ + +#include "gtest.h" + +#include "FileTools.h" +#include "FileTools.cpp" + +TEST(BaseLib, FindLastPathSeparatorWin) +{ + ASSERT_EQ ( BaseLib::findLastPathSeparator("file"), std::string::npos ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("\\file"), 0 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("path\\"), 4 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("\\path\\"), 5 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("path\\file"), 4 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("\\path\\file"), 5 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("\\path\\path\\file"), 10 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("\\path\\path\\path\\"), 15 ); +} + +TEST(BaseLib, FindLastPathSeparatorUnix) +{ + ASSERT_EQ ( BaseLib::findLastPathSeparator("file"), std::string::npos ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("/file"), 0 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("path/"), 4 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("/path/"), 5 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("path/file"), 4 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("/path/file"), 5 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("/path/path/file"), 10 ); + ASSERT_EQ ( BaseLib::findLastPathSeparator("/path/path/path/"), 15 ); +} + +TEST(BaseLib, GetFileExtensionWin) +{ + ASSERT_EQ ( BaseLib::getFileExtension("file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path\\"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path\\file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\path\\file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\path\\path\\"), "" ); + + ASSERT_EQ ( BaseLib::getFileExtension("file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\path\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path\\path\\path.ext\\"), "" ); + + ASSERT_EQ ( BaseLib::getFileExtension("path.wrong\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path.wrong\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path.wrong0\\path.wrong\\file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("\\path.wrong0\\path.wrong\\path.ext\\"), "" ); +} + +TEST(BaseLib, getFileExtensionUnix) +{ + ASSERT_EQ ( BaseLib::getFileExtension("file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path/"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path/file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/path/file"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/path/path/"), "" ); + + ASSERT_EQ ( BaseLib::getFileExtension("file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("path.ext/"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path.ext/"), "" ); + ASSERT_EQ ( BaseLib::getFileExtension("path/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/path/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path/path/path.ext/"), "" ); + + ASSERT_EQ ( BaseLib::getFileExtension("path.wrong/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path.wrong/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path.wrong0/path.wrong/file.ext"), "ext" ); + ASSERT_EQ ( BaseLib::getFileExtension("/path.wrong0/path.wrong/path.ext/"), "" ); +} + +TEST(BaseLib, CopyPathToFileNameWin) +{ + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend"), "file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path\\file", "extend"), "path\\file" ); + + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend\\"), "extend\\file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path\\file", "extend\\"), "path\\file" ); + + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend\\smth"), "extend\\file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path\\file", "extend\\smth"), "path\\file" ); +} + +TEST(BaseLib, CopyPathToFileNameUnix) +{ + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend"), "file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path/file", "extend"), "path/file" ); + + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend/"), "extend/file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path/file", "extend/"), "path/file" ); + + ASSERT_EQ ( BaseLib::copyPathToFileName("file", "extend/smth"), "extend/file" ); + ASSERT_EQ ( BaseLib::copyPathToFileName("path/file", "extend/smth"), "path/file" ); +} + +TEST(BaseLib, ExtractPathWin) +{ + ASSERT_EQ ( BaseLib::extractPath("file"), "" ); + ASSERT_EQ ( BaseLib::extractPath("/file"), "/" ); + ASSERT_EQ ( BaseLib::extractPath("path/"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("path/file"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/file"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/file"), "/path/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/path/"), "/path/path/path/" ); + + ASSERT_EQ ( BaseLib::extractPath("file.ext"), "" ); + ASSERT_EQ ( BaseLib::extractPath("/file.ext"), "/" ); + ASSERT_EQ ( BaseLib::extractPath("path.ext/"), "path.ext/" ); + ASSERT_EQ ( BaseLib::extractPath("/path.ext/"), "/path.ext/" ); + ASSERT_EQ ( BaseLib::extractPath("path/file.ext"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/file.ext"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/file.ext"), "/path/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/path.ext/"), "/path/path/path.ext/" ); +} + +TEST(BaseLib, ExtractBaseNameWithoutExtensionWin) +{ + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\path\\path\\"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\path\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path\\path\\path.ext\\"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path.wrong\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path.wrong\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path.wrong0\\path.wrong\\file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("\\path.wrong0\\path.wrong\\path.ext\\"), "" ); +} + +TEST(BaseLib, ExtractBaseNameWithoutExtensionUnix) +{ + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/path/path/"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path.ext/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path.ext/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/path/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path/path/path.ext/"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("path.wrong/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path.wrong/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path.wrong0/path.wrong/file.ext"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseNameWithoutExtension("/path.wrong0/path.wrong/path.ext/"), "" ); +} + +TEST(BaseLib, ExtractBaseNameWin) +{ + ASSERT_EQ ( BaseLib::extractBaseName("file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("path\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\path\\file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\path\\path\\"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseName("file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path.ext\\"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("path\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\path\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path\\path\\path.ext\\"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseName("path.wrong\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path.wrong\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path.wrong0\\path.wrong\\file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("\\path.wrong0\\path.wrong\\path.ext\\"), "" ); +} + +TEST(BaseLib, HasFileExtensionWin) +{ + ASSERT_TRUE ( BaseLib::hasFileExtension("", "file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path\\")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path\\")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path\\file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path\\file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path\\path\\file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path\\path\\path\\")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path.ext\\")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path.ext\\")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "\\path\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "\\path\\path\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path\\path\\path.ext\\")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path.wrong\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "\\path.wrong\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "\\path.wrong0\\path.wrong\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "\\path.wrong0\\path.wrong\\path.ext\\")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("Ext", "file.exT")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "path\\file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "path\\file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path\\file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("Ext", "path\\file.exT")); +} + +TEST(BaseLib, HasFileExtensionUnix) +{ + ASSERT_TRUE ( BaseLib::hasFileExtension("", "file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path/")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path/")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path/file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path/file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path/path/file")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path/path/path/")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "path.ext/")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path.ext/")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "/path/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "/path/path/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path/path/path.ext/")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path.wrong/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "/path.wrong/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "/path.wrong0/path.wrong/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("", "/path.wrong0/path.wrong/path.ext/")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("Ext", "file.exT")); + + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "path/file.ext")); + ASSERT_TRUE ( BaseLib::hasFileExtension("EXT", "path/file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("ext", "path/file.EXT")); + ASSERT_TRUE ( BaseLib::hasFileExtension("Ext", "path/file.exT")); +} + +TEST(BaseLib, ExtractBaseNameUnix) +{ + ASSERT_EQ ( BaseLib::extractBaseName("file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("path/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/path/file"), "file" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/path/path/"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseName("file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("path.ext/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path.ext/"), "" ); + ASSERT_EQ ( BaseLib::extractBaseName("path/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/path/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path/path/path.ext/"), "" ); + + ASSERT_EQ ( BaseLib::extractBaseName("path.wrong/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path.wrong/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path.wrong0/path.wrong/file.ext"), "file.ext" ); + ASSERT_EQ ( BaseLib::extractBaseName("/path.wrong0/path.wrong/path.ext/"), "" ); +} + +TEST(BaseLib, ExtractPathUnix) +{ + ASSERT_EQ ( BaseLib::extractPath("file"), "" ); + ASSERT_EQ ( BaseLib::extractPath("/file"), "/" ); + ASSERT_EQ ( BaseLib::extractPath("path/"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("path/file"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/file"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/file"), "/path/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/path/"), "/path/path/path/" ); + + ASSERT_EQ ( BaseLib::extractPath("file.ext"), "" ); + ASSERT_EQ ( BaseLib::extractPath("/file.ext"), "/" ); + ASSERT_EQ ( BaseLib::extractPath("path.ext/"), "path.ext/" ); + ASSERT_EQ ( BaseLib::extractPath("/path.ext/"), "/path.ext/" ); + ASSERT_EQ ( BaseLib::extractPath("path/file.ext"), "path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/file.ext"), "/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/file.ext"), "/path/path/" ); + ASSERT_EQ ( BaseLib::extractPath("/path/path/path.ext/"), "/path/path/path.ext/" ); +} diff --git a/Utils/FileConverter/generateMatPropsFromMatID.cpp b/Utils/FileConverter/generateMatPropsFromMatID.cpp index 1361a6d0891215cf7f5196729780c5104f30d9a5..d5b475f37bb5b2b1d0f8eefe2df3657ca7cd3cba 100644 --- a/Utils/FileConverter/generateMatPropsFromMatID.cpp +++ b/Utils/FileConverter/generateMatPropsFromMatID.cpp @@ -28,7 +28,7 @@ int main (int argc, char* argv[]) // create file std::string filename(argv[1]); - std::string name(BaseLib::getFileNameFromPath(filename)); + std::string name = BaseLib::extractBaseNameWithoutExtension(filename); std::string new_matname(name + "_prop"); std::ofstream out_prop( new_matname.c_str(), std::ios::out );