path
Abstract
path.hpp
contains functions for path editing.
path.hpp
はパス編集用の関数を収録します.
Header file
namespace sstd{
std::string path2basePath (const char* pPath);
std::string path2basePath_woWC (const char* pPath); // _woWC: without wild card
std::string path2dirName (const char* pPath);
uint path2dirName_begin_idx (const char* pPath);
uint path2dirName_end_idx (const char* pPath);
uint path2dirName_end_idx_woWC(const char* pPath); // _woWC: without wild card
char* path2fileName (const char* pPath);
std::string path2fileName (const std::string& path);
std::string path2fileName_woExt (const char* pPath);
char* path2fileExt (const char* pPath);
std::string path2fileExt (const std::string& path);
std::vector<std::string> parsePath (const char* pPath);
std::vector<std::string> parsePath_wBasePath(const char* pPath); // _wBasePath: with base path
bool isFile(const char* pPath);
bool isFile(const std::string& path);
bool isDir (const char* pPath);
bool isDir (const std::string& path);
bool fileExist(const char* pPath);
bool fileExist(const std::string& path);
bool dirExist(const char* pPath);
bool dirExist(const std::string& path);
bool pathExist(const char* pPath);
bool pathExist(const std::string& path);
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
// Below functions are the not recommended to use. (Below functions will be delete on sstd ver 3.x.x).
namespace sstd{
std::string getPath (const char* pPath); // new name: path2basePath()
std::string getPath_woWC (const char* pPath); // new name: path2basePath_woWC()
std::string getDirName (const char* pPath); // new name: path2dirName()
uint getDirName_begin_idx (const char* pPath); // new name: path2dirName_begin_idx()
uint getDirName_end_idx (const char* pPath); // new name: path2dirName_end_idx()
uint getDirName_end_idx_woWC (const char* pPath); // new name: path2dirName_end_idx_woWC()
char* getFileName (const char* pPath); // new name: path2fileName()
std::string getFileName_withoutExtension(const char* pPath); // new name: path2fileName_woExt()
char* getExtension (const char* pPath); // new name: path2fileExt()
std::vector<std::string> parsePath_withBase(const char* pPath); // new name: parsePath_wBasePath()
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
Description
Function name | Description |
---|---|
path2basePath() |
A function to get a path when a string of 1st arg is consisted of a path and a file name. |
path2basePath_woWC() |
A function to get a path without wildcard (* or ? ). (_woWC means without wildcard). |
path2dirName() |
A function to get a nearest directory name from file name when a string of 1st arg which is consisted of a path and a file name. |
path2dirName_begin_idx() |
A function to get a beginning index number of the nearest directory name from file name when a string of 1st arg is consisted of a path and a file name. |
path2dirName_end_idx() |
A function to get a ending index number of the nearest directory name from file name when a string of 1st arg is consisted of a path and a file name. In detail, the ending index number is defined next to the ending character. |
path2dirName_end_idx_woWC() |
A function to get a ending index number of the nearest directory name from file name without wildcard when a string of 1st arg which is consisted of a path and a file name. In detail, the ending index number is defined next to the ending character. |
path2fileName() |
A function to get a file name when a string of 1st arg is consisted of a path and a file name. |
path2fileName_woExt() |
A function to get a file name without file extension when a string of 1st arg is consisted of a path and a file name. |
path2fileExt() |
A function to get a file extension. When a string of 1st arg does not have a file extension, 0 length string is returned. |
parsePath() | A function to parse a path by / or \\ . |
parsePath_wBasePath() |
A function to parse a path by / or \\ and return with the base path from the 1st arg. |
isFile() | Exactory same as the fileExist() |
isDir() | Exactory same as the dirExist() |
fileExist() | A function to judge whether a file is exist or not. |
dirExist() | A function to judge whether a directory is exist or not. |
pathExist() | A function to judge whether a path is exist or not. |
Usage
path2basePath(), path2basePath_woWC()
-
main.cpp
NOTE: This sample use#include <sstd/sstd.hpp> int main(){ sstd::printn( sstd::path2basePath("./abc/def/example.txt") ); sstd::printn( sstd::path2basePath("./abc/def/") ); sstd::printn( sstd::path2basePath("./abc/def") ); printf("\n"); sstd::printn( sstd::path2basePath_woWC("./abc/def/example.txt") ); sstd::printn( sstd::path2basePath_woWC(R"(./abc/d??/*.txt)") ); }
raw string literals R"()"
to avoidunknown escape sequence: '\*'
error. -
Execution result
sstd::path2basePath("./abc/def/example.txt") = "./abc/def" sstd::path2basePath("./abc/def/") = "./abc/def" sstd::path2basePath("./abc/def") = "./abc" sstd::path2basePath_woWC("./abc/def/example.txt") = "./abc/def" sstd::path2basePath_woWC(R"(./abc/d??/*.txt)") = "./abc"
path2dirName(), path2dirName_begin_idx(), path2dirName_end_idx(), path2dirName_end_idx_owWC()
- main.cpp
#include <sstd/sstd.hpp> int main(){ sstd::printn( sstd::path2dirName("./abc/def") ); sstd::printn( sstd::path2dirName("./abc/def/") ); printf("\n"); sstd::printn( sstd::path2dirName("./abc/def/example.txt") ); // NOTE: path2dirName() treats end name as a direcotry name. If you want to parse this type of path, write like bolow line. sstd::printn( sstd::path2dirName(sstd::path2basePath("./abc/def/example.txt")) ); printf("\n"); sstd::printn( sstd::path2dirName_begin_idx("./abc/def/") ); printf("\n"); sstd::printn( sstd::path2dirName_end_idx("./abc/def/") ); sstd::printn( sstd::path2dirName_end_idx_woWC("./abc/def/") ); sstd::printn( sstd::path2dirName_end_idx_woWC(R"(./abc/d??/)") ); }
- Execution result
sstd::path2dirName("./abc/def") = "def" sstd::path2dirName("./abc/def/") = "def" sstd::path2dirName("./abc/def/example.txt") = "example.txt" sstd::path2dirName(sstd::path2basePath("./abc/def/example.txt")) = "def" sstd::path2dirName_begin_idx("./abc/def/") = 6 sstd::path2dirName_end_idx("./abc/def/") = 10 sstd::path2dirName_end_idx_woWC("./abc/def/") = 10 sstd::path2dirName_end_idx_woWC(R"(./abc/d??/)") = 6
path2fileName(), path2fileName_woExt()
- main.cpp
#include <sstd/sstd.hpp> int main(){ sstd::printn( sstd::path2fileName("./abc/def/example.txt") ); sstd::printn( sstd::path2fileName("./abc/def/example") ); printf("\n"); sstd::printn( sstd::path2fileName_woExt("./abc/def/example.txt") ); sstd::printn( sstd::path2fileName_woExt("./abc/def/example") ); sstd::printn( sstd::path2fileName_woExt("./abc/def/example.txt.zip") ); }
- Execution result
sstd::path2fileName("./abc/def/example.txt") = "example.txt" sstd::path2fileName("./abc/def/example") = "example" sstd::path2fileName_woExt("./abc/def/example.txt") = "example" sstd::path2fileName_woExt("./abc/def/example") = "example" sstd::path2fileName_woExt("./abc/def/example.txt.zip") = "example.txt"
path2fileExt()
- main.cpp
#include <sstd/sstd.hpp> int main(){ sstd::printn( sstd::path2fileExt("./abc/def/example.txt") ); sstd::printn( sstd::path2fileExt("./abc/def/example") ); }
- Execution result
sstd::path2fileExt("./abc/def/example.txt") = "txt" sstd::path2fileExt("./abc/def/example") = "(null)"
parsePath(), parsePath_wBasePath()
- main.cpp
#include <sstd/sstd.hpp> int main(){ sstd::printn( sstd::parsePath("./abc/def/example.txt") ); sstd::printn( sstd::parsePath_wBasePath("./abc/def/example") ); }
- Execution result
sstd::parsePath("./abc/def/example.txt") = ["." "abc" "def" "example.txt"] sstd::parsePath_wBasePath("./abc/def/example") = ["." "./abc" "./abc/def" "./abc/def/example"]
isFile(), isDir(), fileExist(), dirExist(), pathExist()
- main.cpp
#include <sstd/sstd.hpp> int main(){ // sstd::isFile() is exactory same as the `sstd::fileExist()`. // sstd::isDir() is exactory same as the `sstd::dirExist()`. sstd::printn( sstd::fileExist("./tmp/abc/def/example.txt") ); sstd::printn( sstd::dirExist("./tmp/abc/def") ); sstd::printn( sstd::pathExist("./tmp/abc/def") ); sstd::printn( sstd::pathExist("./tmp/abc/def/example.txt") ); printf("\n"); sstd::mkdir("tmp"); sstd::mkdir("./tmp/abc/def"); sstd::system("touch ./tmp/abc/def/example.txt"); sstd::printn( sstd::fileExist("./tmp/abc/def/example.txt") ); sstd::printn( sstd::dirExist("./tmp/abc/def") ); sstd::printn( sstd::pathExist("./tmp/abc/def") ); sstd::printn( sstd::pathExist("./tmp/abc/def/example.txt") ); sstd::rm("tmp"); }
- Execution result
sstd::fileExist("./tmp/abc/def/example.txt") = false sstd::dirExist("./tmp/abc/def") = false sstd::pathExist("./tmp/abc/def") = false sstd::pathExist("./tmp/abc/def/example.txt") = false sstd::fileExist("./tmp/abc/def/example.txt") = true sstd::dirExist("./tmp/abc/def") = true sstd::pathExist("./tmp/abc/def") = true sstd::pathExist("./tmp/abc/def/example.txt") = true
Implementation
- Source: sstd/src/file/path.cpp
- Header: sstd/src/file/path.hpp
- Test: test/src_test/file/path.cpp