Commit 44c2cbc1 authored by Bart van der Velden's avatar Bart van der Velden

Changed PrepareLongFileName to use boost::filesystem::path instead of string (#65)

parent 139921bc
......@@ -77,7 +77,7 @@ void InitLogging(const string& logFile)
}
// Parse the command line
bool HandleCommandLine(int argc, char **argv, vector<string>& folders, string& logFile)
bool HandleCommandLine(int argc, char **argv, vector<fs::path>& folders, string& logFile)
{
auto retVal = true;
try
......@@ -91,7 +91,7 @@ bool HandleCommandLine(int argc, char **argv, vector<string>& folders, string& l
("help,h", "show this help")
("version,v", "show version information")
("logfile", po::value<string>(), "the file to log to")
("folder", po::value<vector<string> >(), "folder to scan");
("folder", po::value<vector<fs::path> >(), "folder to scan");
po::store(po::command_line_parser(argc, argv).options(desc).positional(pod).run(), vm);
ifstream ifs(DefaultConfigFile);
po::store(po::parse_config_file<char>(ifs, desc), vm);
......@@ -123,8 +123,8 @@ bool HandleCommandLine(int argc, char **argv, vector<string>& folders, string& l
else
{
cout << "Folders are: \n";
folders = vm["folder"].as< vector<string> >();
for (vector<string>::iterator it = folders.begin(); it != folders.end(); ++it)
folders = vm["folder"].as< vector<fs::path> >();
for (vector<fs::path>::iterator it = folders.begin(); it != folders.end(); ++it)
{
cout << "\t" << *it << "\n";
}
......@@ -239,7 +239,7 @@ int ConnnectToDatabase()
int main(int argc, char **argv)
{
int retVal = 0;
vector<string> folders;
vector<fs::path> folders;
string logFile;
if (HandleCommandLine(argc, argv, folders, logFile))
{
......
......@@ -3,36 +3,46 @@
using namespace std;
using namespace mc::utils;
namespace fs = boost::filesystem;
static const uintmax_t Kilo = 1024;
static const uintmax_t Mega = Kilo * Kilo;
static const uintmax_t Giga = Kilo * Mega;
static const uintmax_t Tera = Kilo * Giga;
TEST(UtilsTest, PrepareLongFileName1)
TEST(UtilsTest, PrepareLongFileNameWithDrive)
{
string res = PrepareLongFileName("c:\\autoexec.bat");
fs::path res = PrepareLongFileName("c:\\autoexec.bat");
#if defined(_WIN32)
EXPECT_STREQ("\\\\?\\c:\\autoexec.bat", res.c_str());
res = PrepareLongFileName("relative\\path");
EXPECT_STREQ("relative\\path", res.c_str());
res = PrepareLongFileName("a");
EXPECT_STREQ("a", res.c_str());
res = PrepareLongFileName("a:b");
EXPECT_STREQ("a:b", res.c_str());
EXPECT_STREQ(L"\\\\?\\c:\\autoexec.bat", res.c_str());
#else
EXPECT_STREQ("c:\\autoexec.bat", res.c_str());
#endif
}
TEST(UtilsTest, PrepareLongFileNameNoDrive1)
{
fs::path res = PrepareLongFileName("relative\\path");
EXPECT_STREQ(L"relative\\path", res.c_str());
}
TEST(UtilsTest, PrepareLongFileNameNoDrive2)
{
fs::path res = PrepareLongFileName("a");
EXPECT_STREQ(L"a", res.c_str());
}
TEST(UtilsTest, PrepareLongFileNameNoDrive3)
{
fs::path res = PrepareLongFileName("a:b");
EXPECT_STREQ(L"a:b", res.c_str());
}
TEST(UtilsTest, PrepareLongFileNameNonAnsiCharacters)
{
string res = PrepareLongFileName("c:\\autoexec_テスト.bat");
fs::path res = PrepareLongFileName("c:\\autoexec_テスト.bat");
#if defined(_WIN32)
EXPECT_STREQ("\\\\?\\c:\\autoexec.bat", res.c_str());
EXPECT_STREQ(L"\\\\?\\c:\\autoexec_テスト.bat", res.c_str());
#else
EXPECT_STREQ("c:\\autoexec_テスト.bat", res.c_str());
#endif
......
......@@ -20,6 +20,7 @@
using namespace std;
using boost::format;
namespace fs = boost::filesystem;
namespace mc
{
......@@ -32,15 +33,16 @@ namespace mc
Note: this only works for absolute paths
*/
string PrepareLongFileName(const string& Path)
fs::path PrepareLongFileName(const fs::path& Path)
{
#if defined(_WIN32)
string retval(Path);
if (retval.size() > 2 &&
retval[1] == ':' &&
(retval[2] == '\\' || retval[2] == '/'))
fs::path retval(Path);
wstring rootPath = retval.root_path().native();
if (rootPath.size() > 2 &&
rootPath[1] == ':' &&
(rootPath[2] == '\\' || rootPath[2] == '/'))
{
retval = "\\\\?\\" + retval;
retval = L"\\\\?\\" + retval.native();
}
return retval;
#else
......
......@@ -20,6 +20,7 @@
#include <string>
#include <boost/cstdint.hpp>
#include <boost/filesystem.hpp>
namespace mc
{
......@@ -33,7 +34,7 @@ namespace mc
WinApi functions into handling file names that are longer than
MAX_PATH, we need to put the magic "\\?" before the drive letter.
*/
std::string PrepareLongFileName(const std::string& Path);
boost::filesystem::path PrepareLongFileName(const boost::filesystem::path& Path);
} // namespace utils
} // namespace mc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment