Skip to content

Commit

Permalink
Update (#2)
Browse files Browse the repository at this point in the history
* fix: list_files function is repaired

* chore: Updated version

* fix: Fixed the extract option
  • Loading branch information
vschwaberow authored Aug 11, 2024
1 parent fbd98ca commit 00cefc3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dtools"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"
authors = ["Volker Schwaberow <[email protected]>"]
Expand Down
39 changes: 29 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Author: Volker Schwaberow <[email protected]>
// Copyright (c) 2024 Volker Schwaberow


use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use thiserror::Error;
Expand Down Expand Up @@ -65,7 +64,7 @@ pub fn ascii_to_petscii(ascii: &str) -> Vec<u8> {
.map(|c| match c {
' '..='_' => c as u8,
'a'..='z' => (c as u8) - 32,
_ => 0x3F,
_ => 0x3F,
})
.collect()
}
Expand Down Expand Up @@ -123,7 +122,7 @@ impl D64 {
self.write_sector(18, 0, &bam)?;

let mut dir = [0u8; 256];
dir[1] = 0xFF;
dir[1] = 0xFF;
self.write_sector(18, 1, &dir)?;

Ok(())
Expand Down Expand Up @@ -200,23 +199,43 @@ impl D64 {
let mut files = Vec::new();
let dir_track = 18;
let mut sector = 1;
let mut visited_sectors = std::collections::HashSet::new();

loop {
if visited_sectors.contains(&(dir_track, sector)) {
return Err(D64Error::InvalidTrackSector);
}
visited_sectors.insert((dir_track, sector));

let data = self.read_sector(dir_track, sector)?;

for i in (0..256).step_by(32) {
let file_type = data[i + 2];
if file_type == 0 {
break;
continue;
}
if file_type != 0 && file_type & 0x07 != 0 {
let name = petscii_to_ascii(&data[i + 5..i + 21]);
let name_end = data[i + 5..i + 21]
.iter()
.position(|&x| x == 0xA0)
.unwrap_or(16);
let name = petscii_to_ascii(&data[i + 5..i + 5 + name_end]);
files.push(name);
}
}
sector = data[1];
if sector == 0 {

let next_track = data[0];
let next_sector = data[1];

if next_track == 0 || (next_track == 18 && next_sector == 1) {
break;
}

if next_track != 18 || next_sector >= SECTORS_PER_TRACK[17] {
return Err(D64Error::InvalidTrackSector);
}

sector = next_sector;
}

Ok(files)
Expand Down Expand Up @@ -402,8 +421,8 @@ impl BAM {

fn to_sector_data(&self) -> Vec<u8> {
let mut data = vec![0; 256];
data[0] = 18;
data[1] = 1;
data[0] = 18;
data[1] = 1;
data[2] = self.dos_type;

for track in 0..self.tracks as usize {
Expand All @@ -429,7 +448,7 @@ impl BAM {
let bit_idx = sector % 8;

if self.bitmap[track_idx][byte_idx] & (1 << bit_idx) == 0 {
return Ok(());
return Ok(());
}

self.bitmap[track_idx][byte_idx] &= !(1 << bit_idx);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ enum Commands {
Extract {
#[arg(short, long)]
file: String,
#[arg(short, long)]
#[arg(short = 'n', long)]
filename: String,
#[arg(short, long)]
output: String,
Expand Down

0 comments on commit 00cefc3

Please sign in to comment.