mirror of
https://github.com/8051Enthusiast/regex2fat
synced 2024-12-04 22:19:57 +00:00
Merge pull request #12 from jaybosamiya/nomatch
Add an (optional) NOMATCH file
This commit is contained in:
commit
eb725ddbf9
@ -170,8 +170,7 @@ pub fn generate_dir_short(letter: u8, target: UFat) -> Vec<u8> {
|
||||
dir_entry
|
||||
}
|
||||
|
||||
pub fn generate_match(target: UFat) -> Vec<u8> {
|
||||
let name_8_3 = *b"MATCH ";
|
||||
pub fn generate_file(name_8_3: [u8; 11], target: UFat) -> Vec<u8> {
|
||||
let mut dir_entry = ENTRY_TEMPLATE.to_vec();
|
||||
for (x, &y) in dir_entry.iter_mut().take(11).zip(name_8_3.iter()) {
|
||||
*x = y;
|
||||
|
66
src/main.rs
66
src/main.rs
@ -19,8 +19,9 @@ const FORBIDDEN_PRINT_ASCII: [u8; 17] = [
|
||||
fn determine_state_positions<D: DFA>(
|
||||
dfa: &D,
|
||||
validlist: &[u8],
|
||||
nomatch: bool,
|
||||
) -> Result<StateFatMap<D>, &'static str> {
|
||||
let nomatch_len = validlist.len() * 32;
|
||||
let nomatch_len = (validlist.len() + if nomatch { 1 } else { 0 }) * 32;
|
||||
let match_len = (validlist.len() + 1) * 32;
|
||||
// root directory starts at 2
|
||||
let mut current_block: UFat = 2;
|
||||
@ -73,8 +74,9 @@ fn regex_to_fat32<D: DFA, W: Write>(
|
||||
dfa: &D,
|
||||
validlist: &[u8],
|
||||
mut vol: W,
|
||||
nomatch: bool,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let state_blocks = determine_state_positions(&dfa, &validlist)?;
|
||||
let state_blocks = determine_state_positions(&dfa, &validlist, nomatch)?;
|
||||
// pad until at least 65536 blocks, since otherwise ideologically
|
||||
// I would have to implement fat12/fat16
|
||||
// also keep at least one free block for match file (which is 0 bytes,
|
||||
@ -93,7 +95,9 @@ fn regex_to_fat32<D: DFA, W: Write>(
|
||||
}
|
||||
// if accepting state, put match file into dir
|
||||
if dfa.is_match_state(state) {
|
||||
current_dir.append(&mut fat32::generate_match(state_blocks.blocks + 2))
|
||||
current_dir.append(&mut fat32::generate_file(*b"MATCH ", state_blocks.blocks + 2))
|
||||
} else if nomatch {
|
||||
current_dir.append(&mut fat32::generate_file(*b"NOMATCH ", state_blocks.blocks + 2))
|
||||
}
|
||||
if current_dir.len() % fat32::BLOCK_SIZE == 0 {
|
||||
vol.write_all(¤t_dir)?;
|
||||
@ -114,29 +118,36 @@ fn regex_to_fat32<D: DFA, W: Write>(
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("regex2fat")
|
||||
.version("0.1.0")
|
||||
.author("8051Enthusiast")
|
||||
.about("Convert regex DFAs to FAT32 file systems")
|
||||
.arg(
|
||||
Arg::with_name("anchor")
|
||||
.short("a")
|
||||
.long("anchor")
|
||||
.help("Anchor regex at beginning (off by default)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("pattern")
|
||||
.required(true)
|
||||
.index(1)
|
||||
.help("The regex pattern to match"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("outfile")
|
||||
.required(true)
|
||||
.index(2)
|
||||
.help("The file to write the fat fs to"),
|
||||
)
|
||||
.get_matches();
|
||||
let matches =
|
||||
App::new("regex2fat")
|
||||
.version("0.1.0")
|
||||
.author("8051Enthusiast")
|
||||
.about("Convert regex DFAs to FAT32 file systems")
|
||||
.arg(
|
||||
Arg::with_name("anchor")
|
||||
.short("a")
|
||||
.long("anchor")
|
||||
.help("Anchor regex at beginning (off by default)"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("pattern")
|
||||
.required(true)
|
||||
.index(1)
|
||||
.help("The regex pattern to match"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("outfile")
|
||||
.required(true)
|
||||
.index(2)
|
||||
.help("The file to write the fat fs to"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("nomatch")
|
||||
.short("n")
|
||||
.long("nomatch")
|
||||
.help("Generate NOMATCH files (off by default)"),
|
||||
)
|
||||
.get_matches();
|
||||
let pattern = matches.value_of("pattern").unwrap();
|
||||
let dfa = dense::Builder::new()
|
||||
// fat32 is case insensitive
|
||||
@ -156,7 +167,8 @@ fn main() {
|
||||
eprintln!("Could not open file '{}': {}", outfile, err);
|
||||
exit(1);
|
||||
});
|
||||
regex_to_fat32(&dfa, &validlist, file).unwrap_or_else(|err| {
|
||||
let nomatch = matches.is_present("nomatch");
|
||||
regex_to_fat32(&dfa, &validlist, file, nomatch).unwrap_or_else(|err| {
|
||||
eprintln!("Could not write DFA to '{}': {}", outfile, err);
|
||||
exit(1);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user