boost::filesystemとワイド文字 その2
以前boost::filesystemがどんな挙動をするか試すために、ディレクトリを再帰的に探査していってディレクトリ・ファイル名を表示するというプログラムを書いたんだが、それが0x5c問題をスルーしていることに気付いた。ソースは以下。
#include <boost/filesystem/path.hpp> #include <boost/filesystem/operations.hpp> #include <iostream> namespace fs = boost::filesystem; // 再帰的にディレクトリを表示する // nest_countは表示上、スペースを打つ回数にしか使ってない bool print_directory_recursive (const fs::path & path_name, unsigned nest_count = 0) { // 存在しないパス渡されたら失敗 if(fs::exists(path_name) != true) return false; // 渡されたパスがディレクトリだった場合 if(fs::is_directory(path_name) == true) { fs::directory_iterator end; for(fs::directory_iterator ite(path_name) ; ite!=end ; ++ite) { // ディレクトリであった場合再帰的に関数適用 if(fs::is_directory(*ite) == true) { std::cout << "d"; for(int i=0 ; i<nest_count+1 ; ++i) std::cout << " "; std::cout << ite->leaf() << std::endl; print_directory_recursive(*ite, nest_count+1); } // ファイルなら表示 else { std::cout << "f"; for(int i=0 ; i<nest_count+1 ; ++i) std::cout << " "; std::cout << ite->leaf() << std::endl; } } } // ファイルだった場合表示する else { std::cout << "f"; for(int i=0 ; i<nest_count+1 ; ++i) std::cout << " "; std::cout << path_name.leaf() << std::endl; } return true; } int main(int argc, char *argv[]) { // カレントディレクトリ取得 fs::path current_path(fs::initial_path()); // 何も指定されなかったらカレントディレクトリ以下を表示する if(argc < 2) print_directory_recursive(current_path); else print_directory_recursive(fs::path(argv[1], fs::native)); return 0; }
コンパイルしてファイル名にダメ文字を使ったディレクトリを指定して実行すると何故かきちんと表示される(Windows2000 with VC++6.0)。いったんboost::filesystem::pathオブジェクトにパス名が格納されてるはずなので(さらに言えば表示するときにleaf()メンバ関数を使っているのでダメ文字を使った場合その部分でファイル名がちょん切られるハズ)、ダメだと思うんだけど…。
directory_iterator経由だと大丈夫だとかなんかそういうのがあるんだろうか?