C++Builderで、可変長引数を用いて絶対パスを取得する関数。
IncludeTrailingPathDelimiter関数でパスが\か/かは環境によって変化する。
C++11以上で動作するので、
プロジェクト->オプション->C++コンパイラ->'従来の'Borlandコンパイラを使用
のチェックを外してfalseにする必要がある。
Visual C++版
http://qiita.com/mm_sys/items/aba7297f5589c6d174f0
C++11
# include <vector>
# include <IOUtils.hpp>
// 省略
template<typename ... Args>
UnicodeString __fastcall JoinPath(const Args ... strs) {
std::vector<UnicodeString>tmp_vector_str = {strs ...};
UnicodeString tmp_path;
if (TDirectory::IsRelativePath(tmp_vector_str[0])) {
tmp_path = ExpandFileName(tmp_vector_str[0]);
}
else {
tmp_path = tmp_vector_str[0];
}
for (int i = 1; i < tmp_vector_str.size(); i++) {
tmp_path = IncludeTrailingPathDelimiter(tmp_path)
+static_cast<UnicodeString>(tmp_vector_str[i]);
}
return tmp_path;
}
// 使用例
JoinPath(L"1");
// C:\(省略)\Win32\Debug\1
JoinPath(L"1",L"2",L"3");
// C:\(省略)\Win32\Debug\1\2\3
JoinPath(L"C:\\",L"1",L"2",L"3");
// C:\1\2\3