2012-03-15 38 views
0

次の機能に問題があります。パスと許可されたファイル拡張子のセットを与えられ、そのパスにあるそれらの拡張子を持つすべてのファイルを探します。代わりに何も見つけず空のセットを返します。ブーストでディレクトリを移動すると失敗します

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"を含むセットが返されます。

私はそれがずっと前に働いていたと確信していますが、いつ壊れたのか、それをしたのか分かりません。私はそれがいくつかの小さく、迷惑な技術だと確信しています。

+2

注AWFULであることが知られてもらおう私の作業コード

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 ; // Extract directory and filename boostfs::path file = p.filename() ; boostfs::path dir = p.parent_path() ; boostfs::recursive_directory_iterator itr(dir), itr_end ; while(itr != itr_end) { if (boostfs::is_directory(*itr)) { itr.no_push(!recurse) ; } else { // Don't include the original source if (*itr != p) { // Include only allowed file types ext = itr->path().extension().string() ; if (allowed.find(ext) != allowed.end()) { incs.insert(*itr) ; } } } itr++ ; } return incs ; } 

です。 :recursive_directory_iterator'。 – Xeo

+0

@ Xeoありがとう、私はそれを見ていきます。今私はちょうどここで何がうまくいかないのか理解したい。 –

+0

デバッガを使用してコードをステップ実行してみませんか? –

答えて

1

私の問題が見つかりました。 directory_iterator(またはrecursive_directory_iterator)の代わりにpath::iteratorを使用していたので、ディレクトリの内容の代わりにパスのコンポーネントをループしていました。私はそれが早く働いたと断言できたかもしれないが、それはたぶん運が良かったかもしれない。

は、ここで私はブーストのマニュアルでは、ディレクトリを反復処理の例では、ディレクトリを再帰的にスキャンのためのより良い選択肢が `FSであることを

+0

あなたの問題を発見してうれしいです。 Boostはオープンソースなのですか?ドキュメントパッチを提出することができます! –

+0

@JohnZwinck私はすべての資格を持っているかどうかはわかりませんが、おそらく私はそれを行うことができます。 –

関連する問題