Skip to content

Commit

Permalink
Add functions for seeking lines and getting line counts in functions (#…
Browse files Browse the repository at this point in the history
…95)

dds rustg_file_get_line_count for getting the line count of a given filename, and rustg_file_seek_line for getting a specific line.

These are useful as to not load the entire file into memory in DM, which is useful for very large files like dictionaries.
  • Loading branch information
Mothblocks authored Apr 26, 2022
1 parent b8ee3af commit 28b3864
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
for (key, _value) in std::env::vars() {
// CARGO_FEATURE_<name> — For each activated feature of the package being built, this environment variable will be present where <name> is the name of the feature uppercased and having - translated to _.
if let Some(uprfeature) = key.strip_prefix("CARGO_FEATURE_") {
let feature = uprfeature.to_lowercase().replace("_", "-"); // actual proper name of the enabled feature
let feature = uprfeature.to_lowercase().replace('_', "-"); // actual proper name of the enabled feature
if feature_dm_exists!(&feature) {
writeln!(
f,
Expand Down
2 changes: 2 additions & 0 deletions dmsrc/file.dm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#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)
#define rustg_file_get_line_count(fname) text2num(call(RUST_G, "file_get_line_count")(fname))
#define rustg_file_seek_line(fname, line) call(RUST_G, "file_seek_line")(fname, "[line]")

#ifdef RUSTG_OVERRIDE_BUILTINS
#define file2text(fname) rustg_file_read("[fname]")
Expand Down
25 changes: 23 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::Result;
use std::{
fs::{File, OpenOptions},
io::{BufReader, BufWriter, Read, Write},
io::{BufRead, BufReader, BufWriter, Read, Write},
};

byond_fn!(fn file_read(path) {
Expand All @@ -20,14 +20,25 @@ byond_fn!(fn file_append(data, path) {
append(data, path).err()
});

byond_fn!(fn file_get_line_count(path) {
Some(get_line_count(path).ok()?.to_string())
});

byond_fn!(fn file_seek_line(path, line) {
seek_line(path, match line.parse::<usize>() {
Ok(line) => line,
Err(_) => return None,
})
});

fn read(path: &str) -> Result<String> {
let file = File::open(path)?;
let metadata = file.metadata()?;
let mut file = BufReader::new(file);

let mut content = String::with_capacity(metadata.len() as usize);
file.read_to_string(&mut content)?;
let content = content.replace("\r", "");
let content = content.replace('\r', "");

Ok(content)
}
Expand Down Expand Up @@ -70,3 +81,13 @@ fn append(data: &str, path: &str) -> Result<usize> {

Ok(written)
}

fn get_line_count(path: &str) -> Result<u32> {
let file = BufReader::new(File::open(path)?);
Ok(file.lines().count() as u32)
}

fn seek_line(path: &str, line: usize) -> Option<String> {
let file = BufReader::new(File::open(path).ok()?);
file.lines().nth(line).and_then(|line| line.ok())
}

0 comments on commit 28b3864

Please sign in to comment.