代码之家  ›  专栏  ›  技术社区  ›  jackw11111

链接到目录中所有文件的惯用方法(包括文件夹、src等)

  •  0
  • jackw11111  · 技术社区  · 6 年前

    我想从一个大的代码库/API中提取一个示例,但我希望避免使用指定的构建系统,而是手动将所有文件依赖项查找到一个大文件夹中,这样我就可以 g++ *.cpp

    main.cpp
    project
    │
    ├── src
    │   ├── source.cpp
    │   └── *.cpp
    └── include
        ├── source.h
        └── *.h
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   jackw11111    4 年前

    最后写了一个小脚本,尽管这可能不是最简单/最有效的方法,但它仍然达到了预期的效果。只需获取一个包含所有头文件和源文件的字符串就非常有用了,这些头文件和源文件可以与 cp [headers] /path/to/dst

    #include <iostream>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <list>
    
    /*
    * Recursively navigates the file tree and finds all .h header files
    * and all .cpp files, to be used to "scrape" large codebases
    *   
    */
    
    std::list<std::string> source_list = {};
    std::list<std::string> header_list = {};
    
    int is_dir(const char *path)
    {
        struct stat statbuf;
        if (stat(path, &statbuf) != 0)
            return 0;
        return S_ISDIR(statbuf.st_mode);
    }
    
    
    void dirSearch(std::string p)
    {
        FILE *fp;
        char path[100];
    
        std::string cd = "cd ";
        std::string ls = " && ls";
        std::string cmd = cd + p + ls;
    
        fp = popen(cmd.c_str(), "r");
        if (fp == NULL) {
            printf("Failed to run command\n");
            exit(1);
        }
        while (fgets(path, sizeof(path)-1, fp) != NULL) {
    
            std::string abs_path = p + "/" + path;
    
            // rm \n
            abs_path.pop_back();
    
            // any folder can be a path to search for headers in
            if ( is_dir(abs_path.c_str()) ) {
                dirSearch(abs_path);
            }
            // if not a directory, check if the file is a .cpp file and add to list of source files 
            else {
                std::string SOURCE_FILE_EXT = ".cpp";
    
                if (abs_path.substr(abs_path.size() - SOURCE_FILE_EXT.size(), abs_path.size()) == SOURCE_FILE_EXT) {
                    source_list.push_back(abs_path);
                }
    
                std::string HEADER_FILE_EXT = ".h";
    
                if (abs_path.substr(abs_path.size() - HEADER_FILE_EXT.size(), abs_path.size()) == HEADER_FILE_EXT) {
                    header_list.push_back(abs_path);
                }
            }
        }
        pclose(fp);
    }
    
    int main(int argc, char **argv)
    {
        dirSearch(argv[1]);
    
        std::string headers = "";
        std::string fsource = " ";
        
        // add header path
        for (std::string l : header_list) {
            all_header_files += " " + l;
        }
        // add source files
        for (std::string l : source_list) {
            all_source_files += " " + l;
        }
    
        std::cout << all_header_files << std::endl;
        std::cout << all_source_files << std::endl;
    }
    

    下面是一个示例用法:

    g++ scrape_source.cpp -o scrape_source
    ./scrape_source PhysX-4.1
    
    ... PhysX-4.1/physx/src/fastxml/src/PsFastXml.cpp PhysX-4.1/physx/src/foundation/src/PsAllocator.cpp PhysX-4.1/physx/src/foundation/src/PsAssert.cpp PhysX-4.1/physx/src/foundation/src/PsFoundation.cpp PhysX-4.1/physx/src/foundation/src/PsMathUtils.cpp PhysX-4.1/physx/src/foundation/src/PsString.cpp PhysX-4.1/physx/src/foundation/src/PsTempAllocator.cpp PhysX-4.1/physx/src/foundation/src/PsUtilities.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixAtomic.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixCpu.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixFPU.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixMutex.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixPrintString.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixSList.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixSocket.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixSync.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixThread.cpp PhysX-4.1/physx/src/foundation/src/unix/PsUnixTime.cpp PhysX-4.1/physx/src/foundation/src/windows/PsUWPThread.cpp PhysX-4.1/physx/src/foundation/src/windows/PsWindowsAtomic.cpp PhysX-4.1/physx/src/foundation/src/windows/PsWindowsCpu.cpp ...