Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds file_exists function #43

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The default features are:
Additional features are:
* noise: 2d Perlin noise.
* url: Faster replacements for `url_encode` and `url_decode`.
* file: Faster replacements for `file2text` and `text2file`.
* file: Faster replacements for `file2text` and `text2file`, as well as reading or checking if files exist.
* hash: Faster replacement for `md5`, support for SHA-1, SHA-256, and SHA-512. Requires OpenSSL on Linux.

## Installing
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn main() {
f,
r#"
#define rustg_file_read(fname) call(RUST_G, "file_read")(fname)
#define rustg_file_exists(fname) call(RUST_G, "file_exists")(fname)
#define rustg_file_write(text, fname) call(RUST_G, "file_write")(text, fname)
#define rustg_file_append(text, fname) call(RUST_G, "file_append")(text, fname)

Expand Down
9 changes: 9 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ byond_fn! { file_read(path) {
read(path).ok()
} }

byond_fn! { file_exists(path) {
Some(exists(path))
} }

byond_fn! { file_write(data, path) {
write(data, path).err()
} }
Expand All @@ -27,6 +31,11 @@ fn read(path: &str) -> Result<String> {
Ok(content)
}

fn exists(path: &str) -> String {
let path = std::path::Path::new(path);
path.exists().to_string()
}

fn write(data: &str, path: &str) -> Result<usize> {
let path: &std::path::Path = path.as_ref();
if let Some(parent) = path.parent() {
Expand Down