0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++Builderで複数の引数から絶対パスを返す関数

Last updated at Posted at 2017-05-16

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?