2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// Path_WIN32.cpp
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/src/Path_WIN32.cpp#4 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Filesystem
|
|
|
|
// Module: Path
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Path_WIN32.h"
|
|
|
|
#include "Poco/Environment_WIN32.h"
|
|
|
|
#include "Poco/UnWindows.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
std::string PathImpl::currentImpl()
|
|
|
|
{
|
|
|
|
char buffer[MAX_PATH];
|
|
|
|
DWORD n = GetCurrentDirectoryA(sizeof(buffer), buffer);
|
|
|
|
if (n > 0 && n < sizeof(buffer))
|
|
|
|
{
|
|
|
|
std::string result(buffer, n);
|
|
|
|
if (result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else throw SystemException("Cannot get current directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 19:09:23 +01:00
|
|
|
std::string PathImpl::systemImpl()
|
|
|
|
{
|
|
|
|
char buffer[MAX_PATH];
|
|
|
|
DWORD n = GetSystemDirectoryA(buffer, sizeof(buffer));
|
|
|
|
if (n > 0 && n < sizeof(buffer))
|
|
|
|
{
|
|
|
|
std::string result(buffer, n);
|
|
|
|
if (result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else throw SystemException("Cannot get system directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
std::string PathImpl::homeImpl()
|
|
|
|
{
|
2012-12-14 19:09:23 +01:00
|
|
|
std::string result;
|
2014-12-09 11:03:09 +01:00
|
|
|
if (EnvironmentImpl::hasImpl("USERPROFILE"))
|
2012-12-14 19:09:23 +01:00
|
|
|
{
|
2014-12-09 11:03:09 +01:00
|
|
|
result = EnvironmentImpl::getImpl("USERPROFILE");
|
2012-12-14 19:09:23 +01:00
|
|
|
}
|
2014-12-09 11:03:09 +01:00
|
|
|
else if (EnvironmentImpl::hasImpl("HOMEDRIVE") && EnvironmentImpl::hasImpl("HOMEPATH"))
|
2014-12-09 10:38:11 +01:00
|
|
|
{
|
2014-12-09 11:03:09 +01:00
|
|
|
result = EnvironmentImpl::getImpl("HOMEDRIVE");
|
|
|
|
result.append(EnvironmentImpl::getImpl("HOMEPATH"));
|
2014-12-09 10:38:11 +01:00
|
|
|
}
|
|
|
|
else
|
2012-12-14 19:09:23 +01:00
|
|
|
{
|
|
|
|
result = systemImpl();
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
std::string::size_type n = result.size();
|
|
|
|
if (n > 0 && result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 17:05:48 +01:00
|
|
|
std::string PathImpl::configHomeImpl()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// if APPDATA environment variable not exist, return home directory instead
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = EnvironmentImpl::getImpl("APPDATA");
|
|
|
|
}
|
|
|
|
catch (NotFoundException&)
|
|
|
|
{
|
|
|
|
result = homeImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string::size_type n = result.size();
|
|
|
|
if (n > 0 && result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string PathImpl::dataHomeImpl()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// if LOCALAPPDATA environment variable not exist, return config home instead
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = EnvironmentImpl::getImpl("LOCALAPPDATA");
|
|
|
|
}
|
|
|
|
catch (NotFoundException&)
|
|
|
|
{
|
|
|
|
result = configHomeImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string::size_type n = result.size();
|
|
|
|
if (n > 0 && result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string PathImpl::cacheHomeImpl()
|
|
|
|
{
|
|
|
|
return tempImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string PathImpl::tempHomeImpl()
|
|
|
|
{
|
|
|
|
return tempImpl();
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
std::string PathImpl::tempImpl()
|
|
|
|
{
|
|
|
|
char buffer[MAX_PATH];
|
|
|
|
DWORD n = GetTempPathA(sizeof(buffer), buffer);
|
|
|
|
if (n > 0 && n < sizeof(buffer))
|
|
|
|
{
|
|
|
|
n = GetLongPathNameA(buffer, buffer, static_cast<DWORD>(sizeof buffer));
|
|
|
|
if (n <= 0) throw SystemException("Cannot get temporary directory long path name");
|
|
|
|
std::string result(buffer, n);
|
|
|
|
if (result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else throw SystemException("Cannot get temporary directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 17:05:48 +01:00
|
|
|
std::string PathImpl::configImpl()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// if PROGRAMDATA environment variable not exist, return system directory instead
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = EnvironmentImpl::getImpl("PROGRAMDATA");
|
|
|
|
}
|
|
|
|
catch (NotFoundException&)
|
|
|
|
{
|
|
|
|
result = systemImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string::size_type n = result.size();
|
|
|
|
if (n > 0 && result[n - 1] != '\\')
|
|
|
|
result.append("\\");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-04-29 20:52:25 +02:00
|
|
|
std::string PathImpl::nullImpl()
|
|
|
|
{
|
|
|
|
return "NUL:";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string PathImpl::expandImpl(const std::string& path)
|
|
|
|
{
|
|
|
|
char buffer[MAX_PATH];
|
|
|
|
DWORD n = ExpandEnvironmentStringsA(path.c_str(), buffer, sizeof(buffer));
|
|
|
|
if (n > 0 && n < sizeof(buffer))
|
|
|
|
return std::string(buffer, n - 1);
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PathImpl::listRootsImpl(std::vector<std::string>& roots)
|
|
|
|
{
|
|
|
|
roots.clear();
|
|
|
|
char buffer[128];
|
|
|
|
DWORD n = GetLogicalDriveStrings(sizeof(buffer) - 1, buffer);
|
|
|
|
char* it = buffer;
|
|
|
|
char* end = buffer + (n > sizeof(buffer) ? sizeof(buffer) : n);
|
|
|
|
while (it < end)
|
|
|
|
{
|
|
|
|
std::string dev;
|
|
|
|
while (it < end && *it) dev += *it++;
|
|
|
|
roots.push_back(dev);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|