feat add Path
This commit is contained in:
parent
abc1a88bd0
commit
333281ec06
@ -20,7 +20,9 @@ add_subdirectory(3party/fmt EXCLUDE_FROM_ALL)
|
||||
target_include_directories(sled PUBLIC 3party/eigen)
|
||||
target_sources(
|
||||
sled
|
||||
PRIVATE src/log/log.cc
|
||||
PRIVATE
|
||||
src/filesystem/path.cc
|
||||
src/log/log.cc
|
||||
src/network/async_resolver.cc
|
||||
src/network/ip_address.cc
|
||||
src/network/null_socket_server.cc
|
||||
|
24
include/sled/filesystem/path.h
Normal file
24
include/sled/filesystem/path.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#ifndef SLED_FILESYSTEM_PATH_H
|
||||
#define SLED_FILESYSTEM_PATH_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace sled {
|
||||
class Path {
|
||||
public:
|
||||
// cwd = current working directory
|
||||
static Path Current();
|
||||
static Path Home();
|
||||
static Path TempDir();
|
||||
|
||||
Path();
|
||||
Path(const std::string &path);
|
||||
|
||||
std::string ToString() const { return path_; };
|
||||
|
||||
private:
|
||||
std::string path_;
|
||||
};
|
||||
}// namespace sled
|
||||
#endif// SLED_FILESYSTEM_PATH_H
|
13
include/sled/filesystem/temporary_file.h
Normal file
13
include/sled/filesystem/temporary_file.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#ifndef SLED_FILESYSTEM_TEMPORARY_FILE_H
|
||||
#define SLED_FILESYSTEM_TEMPORARY_FILE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace sled {
|
||||
class TemporaryFile {
|
||||
TemporaryFile();
|
||||
TemporaryFile(const std::string &tmp_dir);
|
||||
};
|
||||
}// namespace sled
|
||||
#endif// SLED_FILESYSTEM_TEMPORARY_FILE_H
|
46
src/filesystem/path.cc
Normal file
46
src/filesystem/path.cc
Normal file
@ -0,0 +1,46 @@
|
||||
#include "sled/filesystem/path.h"
|
||||
#include "sled/log/log.h"
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace sled {
|
||||
Path
|
||||
Path::Current()
|
||||
{
|
||||
char tmp_path[PATH_MAX];
|
||||
if (getcwd(tmp_path, PATH_MAX)) {
|
||||
auto cwd = Path(tmp_path);
|
||||
return cwd;
|
||||
}
|
||||
|
||||
LOGE("Path", "getcwd failed: {}", strerror(errno));
|
||||
|
||||
return Path();
|
||||
}
|
||||
|
||||
Path
|
||||
Path::Home()
|
||||
{
|
||||
char *home = getenv("HOME");
|
||||
if (home) {
|
||||
return Path(home);
|
||||
} else {
|
||||
return Path();
|
||||
}
|
||||
}
|
||||
|
||||
Path
|
||||
Path::TempDir()
|
||||
{
|
||||
char *tmp = getenv("TMPDIR");
|
||||
if (tmp) {
|
||||
return Path(tmp);
|
||||
} else {
|
||||
return Path("/tmp/");
|
||||
}
|
||||
}
|
||||
|
||||
Path::Path() {}
|
||||
|
||||
Path::Path(const std::string &path) : path_(path) {}
|
||||
}// namespace sled
|
Loading…
Reference in New Issue
Block a user