// Utility.cpp : implementation of the utility apis declared in utility.h // //******************************************************************** // // Copyright (c) 2005 James Memmott // // This software is provided 'as-is', without any express or implied warranty. // In no event will the author be held liable for any damages arising from the // use of this software. // // Permission is granted to anyone to use this software for any purpose, including // commercial applications, and to alter it and redistribute it freely, subject to // the following restrictions: // // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the disclaimer. // // 2. The origin of this software must not be misrepresented; you must not claim // that you wrote the original software. If you use this software in a product, // an acknowledgment in the product documentation would be appreciated but is // not required. // // 3. Altered source versions must be plainly marked as such, and must not be // misrepresented as being the original software. // //******************************************************************** // #include #include "Utility.h" #include #include #include #include namespace bfs = boost::filesystem; // Returns the directory location corresponding to some path std::string GetFileDirFromPath(const char *tszPath) { bfs::path path = bfs::path(tszPath, bfs::native); return path.branch_path().native_file_string(); } // Returns the filename from the supplied path std::string GetFileNameFromPath(const char *tszPath) { bfs::path path = bfs::path(tszPath, bfs::native); return path.leaf(); } // Returns the filetitle from the supplied path std::string GetFileTitleFromPath(const char *tszPath) { bfs::path path = bfs::path(tszPath, bfs::native); std::string fname = path.leaf(); std::string::size_type lastdot = fname.rfind('.'); if (lastdot != std::string::npos) { return fname.substr(0, lastdot); } return fname; } // Returns the file extension from the supplied path std::string GetFileExtFromPath(const char *tszPath) { bfs::path path = bfs::path(tszPath, bfs::native); std::string fname = path.leaf(); std::string::size_type lastdot = fname.rfind('.'); if (lastdot != std::string::npos) { return fname.substr(lastdot); } return fname; } std::string ExtractFilePath(std::string FileName) { bfs::path path = bfs::path(FileName, bfs::native); return path.branch_path().native_file_string(); } bool IsFileNameValid(const char * lpFileName) { if (lpFileName == NULL) return false; return bfs::native (lpFileName); } bool MakeSureDirectoryPathExists(std::string Dir) { bfs::path pth = bfs::path(Dir, bfs::native).branch_path(); if (bfs::exists(pth)) { if (bfs::is_directory(pth)) { return true; } else { return false; } } bfs::create_directories (pth); return true; } bool DirectoryExists(const std::string Name) { bfs::path pth (Name, bfs::native); return bfs::exists(pth) && bfs::is_directory(pth); } #ifdef WIN32 std::string t2sprintf(const char* format,...) { va_list arguments; va_start(arguments,format); char buffer[1024]; vsprintf(buffer,format,arguments); va_end(arguments); std::string rtvalue = buffer; return rtvalue; } #endif using boost::format; using boost::str; std::string SrtTimeString( int nTime ) { std::string csTime; int h = nTime / ( 60 * 60 * 1000 ); nTime = nTime - h * ( 60 * 60 * 1000 ); int m = nTime / ( 60 * 1000 ); nTime = nTime - m * ( 60 * 1000 ); int s = nTime / 1000; int t = nTime - s * 1000; csTime = str(format( "%02d:%02d:%02d,%03d") % h % m % s % t ); return csTime; }