Commit d123ad0a authored by captainwong's avatar captainwong

path_op and test

parent e561276a
......@@ -11,56 +11,72 @@
namespace jlib {
namespace win32 {
inline std::wstring getExePath()
{
wchar_t path[1024] = { 0 };
GetModuleFileNameW(nullptr, path, 1024);
std::wstring::size_type pos = std::wstring(path).find_last_of(L"\\/");
return std::wstring(path).substr(0, pos);
inline std::wstring getExePath() {
wchar_t path[1024] = { 0 }; ::GetModuleFileNameW(nullptr, path, 1024); return path;
}
inline std::string getExePathA()
{
char path[1024] = { 0 };
GetModuleFileNameA(nullptr, path, 1024);
std::string::size_type pos = std::string(path).find_last_of("\\/");
return std::string(path).substr(0, pos);
inline std::string getExePathA() {
char path[1024] = { 0 }; ::GetModuleFileNameA(nullptr, path, 1024); return path;
}
static constexpr wchar_t* DEFAULT_PATH_FILTERW = L"\\/:*?\"<>| ";
static constexpr char* DEFAULT_PATH_FILTER = "\\/:*?\"<>| ";
inline std::wstring getExeFolderPath() {
auto path = getExePath(); auto pos = path.find_last_of(L"\\/"); return path.substr(0, pos);
}
inline std::string getExeFolderPathA() {
auto path = getExePathA(); auto pos = path.find_last_of("\\/"); return path.substr(0, pos);
}
static constexpr const wchar_t* DEFAULT_PATH_FILTERW = L"\\/:*?\"<>| ";
static constexpr const char* DEFAULT_PATH_FILTER = "\\/:*?\"<>| ";
inline std::wstring integratePath(const std::wstring& path, const std::wstring& filter = DEFAULT_PATH_FILTERW, wchar_t replace_by = L'_') {
auto ret = path;
for (auto c : filter) {
std::replace(ret.begin(), ret.end(), c, replace_by);
}
return ret;
inline std::wstring integrateFileName(const std::wstring& name, const std::wstring& filter = DEFAULT_PATH_FILTERW, wchar_t replace_by = L'_') {
auto ret = name; for (auto c : filter) { std::replace(ret.begin(), ret.end(), c, replace_by); } return ret;
}
inline std::string integratePath(const std::string& path, const std::string& filter = DEFAULT_PATH_FILTER, char replace_by = '_') {
auto ret = path;
for (auto c : filter) {
std::replace(ret.begin(), ret.end(), c, replace_by);
}
return ret;
inline std::string integrateFileName(const std::string& name, const std::string& filter = DEFAULT_PATH_FILTER, char replace_by = '_') {
auto ret = name; for (auto c : filter) { std::replace(ret.begin(), ret.end(), c, replace_by); } return ret;
}
inline std::wstring getSpecialFolder(int csidl) {
wchar_t path[MAX_PATH] = { 0 };
if (SHGetSpecialFolderPathW(nullptr, path, csidl, false)) {
return std::wstring(path);
}
return std::wstring();
wchar_t path[MAX_PATH] = { 0 };
return ::SHGetSpecialFolderPathW(nullptr, path, csidl, false) ? std::wstring(path) : std::wstring();
}
inline std::string getSpecialFolderA(int csidl) {
char path[MAX_PATH] = { 0 };
return ::SHGetSpecialFolderPathA(nullptr, path, csidl, false) ? std::string(path) : std::string();
}
/**
* @brief 获取%APPDATA%路径,一般用于存储程序配置文件
* @note 一般xp的结果为: C:\Documents and Setting\[UserName]\Application Data
* @note 一般vista及以上的结果为:C:\Users\[UserName]\AppData\Roaming
*/
inline std::wstring getAppDataPath() { return getSpecialFolder(CSIDL_APPDATA); }
inline std::string getAppDataPathA() { return getSpecialFolderA(CSIDL_APPDATA); }
inline std::wstring getTempPath() { wchar_t path[MAX_PATH] = { 0 }; ::GetTempPathW(MAX_PATH, path); return path; }
inline std::string getTempPathA() { char path[MAX_PATH] = { 0 }; ::GetTempPathA(MAX_PATH, path); return path; }
inline std::wstring getTempFileName(const std::wstring& folder, const std::wstring& pre) {
wchar_t path[MAX_PATH] = { 0 };
UINT ret = ::GetTempFileNameW(folder.c_str(), pre.c_str(), 0, path);
if (ret != 0) { return path; } return std::wstring();
}
inline std::string getTempFileName(const std::string& folder, const std::string& pre) {
char path[MAX_PATH] = { 0 };
if (SHGetSpecialFolderPathA(nullptr, path, csidl, false)) {
return std::string(path);
}
UINT ret = ::GetTempFileNameA(folder.c_str(), pre.c_str(), 0, path);
if (ret != 0) { return path; } return std::string();
}
inline std::wstring getTempFileName(const std::wstring& pre = L"JLIB") {
return getTempFileName(getTempPath(), pre);
}
return std::string();
inline std::string getTempFileNameA(const std::string& pre = "JLIB") {
return getTempFileName(getTempPathA(), pre);
}
}
......
......@@ -228,6 +228,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hds", "hds\hds.vcxproj", "{
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_file_op", "test_file_op\test_file_op.vcxproj", "{82EF7CB9-D551-43FC-A2A5-485C37C7895B}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_path_op", "test_path_op\test_path_op.vcxproj", "{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
......@@ -354,6 +356,14 @@ Global
{82EF7CB9-D551-43FC-A2A5-485C37C7895B}.Release|x64.Build.0 = Release|x64
{82EF7CB9-D551-43FC-A2A5-485C37C7895B}.Release|x86.ActiveCfg = Release|Win32
{82EF7CB9-D551-43FC-A2A5-485C37C7895B}.Release|x86.Build.0 = Release|Win32
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Debug|x64.ActiveCfg = Debug|x64
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Debug|x64.Build.0 = Debug|x64
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Debug|x86.ActiveCfg = Debug|Win32
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Debug|x86.Build.0 = Debug|Win32
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Release|x64.ActiveCfg = Release|x64
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Release|x64.Build.0 = Release|x64
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Release|x86.ActiveCfg = Release|Win32
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -395,6 +405,7 @@ Global
{4AB0552E-F2D7-4FF0-B019-90D83847A25C} = {ABCB8CF8-5E82-4C47-A0FC-E82DF105DF99}
{42F7EE20-C9AC-49CE-9D92-479576295810} = {0E6598D3-602D-4552-97F7-DC5AB458D553}
{82EF7CB9-D551-43FC-A2A5-485C37C7895B} = {0E6598D3-602D-4552-97F7-DC5AB458D553}
{65C310A4-DAA5-4D94-91B3-848F4E5F5C17} = {0E6598D3-602D-4552-97F7-DC5AB458D553}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A8EBEA58-739C-4DED-99C0-239779F57D5D}
......
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif // _CRT_SECURE_NO_WARNINGS
#include "../../jlib/util/chrono_wrapper.h"
#include "../../jlib/win32/path_op.h"
#include <stdio.h>
using namespace jlib::win32;
int main()
{
printf("getExePathA=%s\n", getExePathA().data());
printf("getExeFolderPathA=%s\n", getExeFolderPathA().data());
auto name = jlib::now_to_string(true) + ".txt";
auto name2 = integrateFileName(name);
printf("integrateFileName:\nold=%s\nnew=%s\n", name.data(), name2.data());
printf("getAppDataPathA=%s\n", getAppDataPathA().data());
printf("getTempFileName=%s\n", getTempFileNameA().data());
system("pause");
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{65C310A4-DAA5-4D94-91B3-848F4E5F5C17}</ProjectGuid>
<RootNamespace>testpathop</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="test_path_op.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="test_path_op.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
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