branch と leaf の分け方

メモ.あるディレクトリで複数のプログラムを実行するので実行時のパスを“ディレクトリ (branch)”と“プログラム名 (leaf)”に分けようと思ったのですが,そう言えば branch と leaf の分け方ってどうするのが一般的なのだろうと疑問に思いました(最後がファイル名で終端している場合とディレクトリの区切り文字 ("/" or "\\" or ... ?) で終端している場合の扱い方).

取りあえず Boot 本で確認してみたところ,以下のような記述になっていました.

leaf() はディレクトリの区切りを含まない,パス文字列の最後の部分のみを返します.逆に branch_path() は,パスから leaf() を取り除いた残りの部分を返します.

Boost C++ Libraries プログラミング

やはり,ディレクトリの区切り文字で終端している場合に具体的にどうなるかちょっと不明だったので,Boost.Filesystem - Life like a clown で実験した方法でいくつか試してみる事に.

[clown@stinger example]$ pwd
/cygdrive/c/code/cpp/example
[clown@stinger example]$ ./a ex_boost_filesystem2.cpp 
C:/code/cpp/example/ex_boost_filesystem2.cpp
-----
root_path       : C:/
root_name       : C:
root_directory  : /
relative_path   : code/cpp/example/ex_boost_filesystem2.cpp
leaf            : ex_boost_filesystem2.cpp
branch_path     : C:/code/cpp/example

string          : C:/code/cpp/example/ex_boost_filesystem2.cpp
file_string     : C:\code\cpp\example\ex_boost_filesystem2.cpp
directory_string: C:\code\cpp\example\ex_boost_filesystem2.cpp
[clown@stinger example]$ ./a c:\\cpp\\example
c:/cpp/example
-----
root_path       : c:/
root_name       : c:
root_directory  : /
relative_path   : cpp/example
leaf            : example
branch_path     : c:/cpp

string          : c:/cpp/example
file_string     : c:\cpp\example
directory_string: c:\cpp\example
[clown@stinger example]$ ./a c:\\cpp\\example\\
c:/cpp/example/
-----
root_path       : c:/
root_name       : c:
root_directory  : /
relative_path   : cpp/example/
leaf            : .
branch_path     : c:/cpp/example

string          : c:/cpp/example/
file_string     : c:\cpp\example\
directory_string: c:\cpp\example\

どうやら,基本は branch は“最後の区切り文字の直前までの文字列”で leaf は“最後の区切り文字の次の文字から文字列の最後の文字までの文字列”になっているようです.ただし,区切り文字で終端している場合(leaf が空の場合)は,“.”が代入されるようです.