环境路径目录迭代
Environment PATH Directories Iteration
我找不到任何代码(无论是C还是C++Boost.Filsystem)关于如何以独立于平台的方式迭代(解析)PATH
环境变量中存在的目录。编写起来并不难,但如果标准模块可用,我想重用它们。有人链接或建议吗?
这是我
之前使用的:
const vector<string>& get_environment_PATH()
{
static vector<string> result;
if( !result.empty() )
return result;
#if _WIN32
const std::string PATH = convert_to_utf8( _wgetenv(L"PATH") ); // Handle Unicode, just remove if you don't want/need this. convert_to_utf8 uses WideCharToMultiByte in the Win32 API
const char delimiter = ';';
#else
const std::string PATH = getenv( "PATH" );
const char delimiter = ':';
#endif
if( PATH.empty() )
throw runtime_error( "PATH should not be empty" );
size_t previous = 0;
size_t index = PATH.find( delimiter );
while( index != string::npos )
{
result.push_back( PATH.substr(previous, index-previous));
previous=index+1;
index = PATH.find( delimiter, previous );
}
result.push_back( PATH.substr(previous) );
return result;
}
这只会在每次程序运行时"计算"一次。它也不是真正的线程安全,但哎呀,与环境无关。
这是我自己的代码片段,没有高级提升库:
if( exe.GetLength() )
{
wchar_t* pathEnvVariable = _wgetenv(L"PATH");
for( wchar_t* pPath = wcstok( pathEnvVariable, L";" ) ; pPath ; pPath = wcstok( nullptr, L";" ) )
{
CStringW exePath = pPath;
exePath += L"\";
exePath += exe;
if( PathFileExists(exePath) )
{
exe = exePath;
break;
}
} //for
} //if
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 迭代时从向量和内存中删除对象
- 如何在c++迭代器类型中包装std::chrono
- 带过滤器的现代迭代c++集合
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C++矢量迭代
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 擦除while循环中迭代的元素
- 实现一个在集合上迭代的模板函数
- 对于set上的循环-获取next元素迭代器
- 在向量内的向量上迭代
- 使用迭代深度优先搜索算法的未加权图的最短路径
- N 个价格路径在 N 次迭代中不变的平均估计值
- 是否可以从一系列路径迭代器或子路径创建文件系统 TS basic_path
- 环境路径目录迭代
- Boost:: Dijkstra 最短路径,如何从路径迭代器获取顶点索引
- 无法将Boost路径迭代器转换为字符串