summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbogdasar1985 <bogdasar1985@gmail.com>2023-10-28 16:50:50 +0500
committerDennis van der Schagt <dennisschagt@gmail.com>2024-04-05 01:01:16 +0200
commit43edb9d8483e60785d2d1a6ab74fad49405f90f9 (patch)
tree860ce37a57cc5f27e3f89940d0f664fb82da66f5
parentcac1f9d55ae2961a8da1739f58a428beac23867b (diff)
Add starts_with() method to Filepath
-rw-r--r--include/filepath.h3
-rw-r--r--rust/libnewsboat-ffi/src/filepath.rs5
-rw-r--r--src/controller.cpp10
-rw-r--r--src/filepath.cpp5
4 files changed, 16 insertions, 7 deletions
diff --git a/include/filepath.h b/include/filepath.h
index 1db6e0ab..12a424f8 100644
--- a/include/filepath.h
+++ b/include/filepath.h
@@ -103,6 +103,9 @@ public:
// Return `false` and do nothing if Filepath is empty, set extension and return `true` otherwise.
bool set_extension(const std::string& str);
+ // Return `true` if Filepath start with `str`, `false` otherwise.
+ bool starts_with(const std::string& str) const;
+
Filepath(Filepath&&) = default;
Filepath& operator=(Filepath&&) = default;
diff --git a/rust/libnewsboat-ffi/src/filepath.rs b/rust/libnewsboat-ffi/src/filepath.rs
index 888afe24..e9a0f6da 100644
--- a/rust/libnewsboat-ffi/src/filepath.rs
+++ b/rust/libnewsboat-ffi/src/filepath.rs
@@ -23,6 +23,7 @@ mod bridged {
fn clone(filepath: &PathBuf) -> Box<PathBuf>;
fn is_absolute(filepath: &PathBuf) -> bool;
fn set_extension(filepath: &mut PathBuf, extension: &str) -> bool;
+ fn starts_with(filepath: &PathBuf, str: &str) -> bool;
// These functions are actually in utils.rs, but I couldn't find a way to return
// `Box<PathBuf>` from libnewsboat-ffi/src/utils.rs, so I moved the bindings here
@@ -94,3 +95,7 @@ fn is_absolute(filepath: &PathBuf) -> bool {
fn set_extension(filepath: &mut PathBuf, extension: &str) -> bool {
filepath.0.set_extension(extension)
}
+
+fn starts_with(filepath: &PathBuf, str: &str) -> bool {
+ filepath.0.starts_with(str)
+}
diff --git a/src/controller.cpp b/src/controller.cpp
index ab737574..7940ff7b 100644
--- a/src/controller.cpp
+++ b/src/controller.cpp
@@ -959,16 +959,12 @@ void Controller::write_item(std::shared_ptr<RssItem> item,
Filepath spath = save_path.back() == '/' ? save_path : save_path + "/";
Filepath path;
- switch (filename.to_locale_string()[0]) {
- case '/':
+ if (filename.starts_with("/")) {
path.push(filename);
- break;
- case '~':
+ } else if (filename.starts_with("~")) {
path = utils::resolve_tilde(filename);
- break;
- default:
+ } else {
path = spath.join(filename);
- break;
}
std::fstream f(path, std::fstream::out);
diff --git a/src/filepath.cpp b/src/filepath.cpp
index 3e5cbcff..e7b84778 100644
--- a/src/filepath.cpp
+++ b/src/filepath.cpp
@@ -70,4 +70,9 @@ bool Filepath::set_extension(const std::string& str)
return filepath::bridged::set_extension(*rs_object, str);
}
+bool Filepath::starts_with(const std::string& str) const
+{
+ return filepath::bridged::starts_with(*rs_object, str);
+}
+
} // namespace newsboat