Skip to content

glob

Abstract

Functions to search files and directories

Header file

namespace sstd{
    struct pathAndType{
        std::string path;
        char type; // 'f': file, 'd': directory

        bool operator < (const struct pathAndType& rhs){
            return (*this).path < rhs.path;
        }
    };

    std::vector<std::string> glob(const        char* path, const char* opt);
    std::vector<std::string> glob(const std::string& path, const char* opt);
    std::vector<std::string> glob(const        char* path);
    std::vector<std::string> glob(const std::string& path);

    std::vector<struct pathAndType> glob_pt(const        char* path, const char* opt); // _pt: with path type
    std::vector<struct pathAndType> glob_pt(const std::string& path, const char* opt);
    std::vector<struct pathAndType> glob_pt(const        char* path);
    std::vector<struct pathAndType> glob_pt(const std::string& path);
}

Description

Function name Description
glob() glob can search file and directory with wildcard (* and ?).

glob は,wildcard (*?) を用いた,ファイルとディレクトリを探索できます.
glob_pt() glob_pt is a type of glob with a variable indicating the file type in the returning value. Options can take d, f, r and p. There options can be specified independently in no particular order.
Options:
- d: getting directory name
- f: getting file name
- r: recursive directory
- p: using sstd::pathmatch() instead of sstd::strmatch() internally

glob_pt は,glob の戻り値にファイルタイプを示す変数が追加されたバージョンです.オプションには d, f, r, p があり,それぞれ独立に,順不同で指定できます.

Usage01: getting only file without recursive option

  • main.cpp
    #include <sstd/sstd.hpp>
    
    int main(){
        sstd::mkdir("./tmp");
        sstd::system("touch ./tmp/a.txt");
        sstd::system("touch ./tmp/b.csv");
        sstd::system("touch ./tmp/c.txt");
    
        std::vector<std::string> vStr = sstd::glob("./tmp/*.txt");
        sstd::printn(vStr);
    
        std::vector<struct sstd::pathAndType> vPt = sstd::glob_pt("./tmp/*.txt");
        sstd::printn(vPt);
    
        sstd::rm("./tmp");
    }
    
  • example.txt
    vStr = ["./tmp/a.txt" "./tmp/c.txt"]
    vPt = ["path: ./tmp/a.txt, type: file" "path: ./tmp/c.txt, type: file"]
    

Usage02: getting file and directory with recursive option

  • main.cpp
    #include <sstd/sstd.hpp>
    
    int main(){
        sstd::mkdir("./tmp");
        sstd::mkdir("./tmp/a");
        sstd::mkdir("./tmp/b");
        sstd::mkdir("./tmp/c");
        sstd::system("touch ./tmp/a.txt");
        sstd::system("touch ./tmp/b.csv");
        sstd::system("touch ./tmp/c.txt");
    
        std::vector<std::string> vStr = sstd::glob("./tmp/*", "dfr");
        sstd::printn(vStr);
    
        std::vector<struct sstd::pathAndType> vPt = sstd::glob_pt("./tmp/*", "dfr");
        sstd::printn(vPt);
    
        sstd::rm("./tmp");
    }
    
  • example.txt
    vStr = ["./tmp/a" "./tmp/a.txt" "./tmp/b" "./tmp/b.csv" "./tmp/c" "./tmp/c.txt"]
    vPt = ["path: ./tmp/a, type: directory" "path: ./tmp/a.txt, type: file" "path: ./tmp/b, type: directory" "path: ./tmp/b.csv, type: file" "path: ./tmp/c, type: directory" "path: ./tmp/c.txt, type: file"]
    

Implementation