次の機能に問題があります。パスと許可されたファイル拡張子のセットを与えられ、そのパスにあるそれらの拡張子を持つすべてのファイルを探します。代わりに何も見つけず空のセットを返します。ブーストでディレクトリを移動すると失敗します
std::set<boostfs::path> scan_directory(const boostfs::path& p,
const bool recurse,
const std::set<std::string>& allowed) {
std::string ext ;
std::set<boostfs::path> incs, incs2 ;
boostfs::path::iterator itr ;
// Extract directory and filename
boostfs::path file = p.filename() ;
boostfs::path dir = p.parent_path() ;
std::cout << "path: " << p.string() << std::endl ;
for (itr = dir.begin(); itr != dir.end(); ++itr) {
if (boostfs::is_directory(*itr)) {
if (recurse) {
std::cout << "dir: " << itr->string() << std::endl ;
incs2 = scan_directory(*itr, true, allowed) ;
incs.insert(incs2.begin(), incs2.end()) ;
}
} else {
// Don't include the original source
if (*itr != p) {
// Include only allowed file types
ext = itr->extension().string() ;
std::cout << "file: " << itr->string() << std::endl ;
std::cout << "ext: " << ext << std::endl ;
if (allowed.find(ext) != allowed.end()) {
incs.insert(*itr) ;
}
}
}
}
return incs ;
}
プリントアウトは、デバッグ用です。私は、次のディレクトリ構造とそれをテストしています:
./test/cpp/
foo.cpp
foo.h
test.cpp
./test/cpp/bar/
baz.cpp
baz.h
私は、パス「テスト/ CPP/TEST.CPP」で関数を呼び出し、真の再帰と1つの文字列「の.cpp」を含むセット。私は
path: test/cpp/test.cpp
dir: test
path: test
file: cpp
ext:
は次に、関数は終了し、プログラムの残りの部分は、それが上で動作するようにそんなにいないファイルの空のセットが指定されます、続けて、印刷物からの次の出力を取得します。テストディレクトリを指定すると、 "test/cpp/foo.cpp"と "test/cpp/bar/baz.cpp"を含むセットが返されます。
私はそれがずっと前に働いていたと確信していますが、いつ壊れたのか、それをしたのか分かりません。私はそれがいくつかの小さく、迷惑な技術だと確信しています。
注AWFULであることが知られてもらおう私の作業コード
です。 :recursive_directory_iterator'。 – Xeo
@ Xeoありがとう、私はそれを見ていきます。今私はちょうどここで何がうまくいかないのか理解したい。 –
デバッガを使用してコードをステップ実行してみませんか? –