From 8192cdfecb703a5c1a363cbcf51d0de045765977 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 11 Oct 2022 20:44:29 +0200 Subject: [PATCH] Sorting Elements --- src/Tools/cdtypes/src/types/dir_record.rs | 2 +- src/Tools/psxcdgen_ex/src/main.rs | 2 + src/Tools/psxcdgen_ex/src/types/helper.rs | 38 +++++++++++++ src/Tools/psxcdgen_ex/src/types/mod.rs | 69 +++++++++++++++++------ 4 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 src/Tools/psxcdgen_ex/src/types/helper.rs diff --git a/src/Tools/cdtypes/src/types/dir_record.rs b/src/Tools/cdtypes/src/types/dir_record.rs index 01c2cad6..783f4349 100644 --- a/src/Tools/cdtypes/src/types/dir_record.rs +++ b/src/Tools/cdtypes/src/types/dir_record.rs @@ -26,7 +26,7 @@ pub struct CDXASystemUse { pub file_attribute: FileAttribute, pub signature: [u8; 2], pub file_number: [u8; 1], - _reserved: [u8; 5], + _reserved: [u8; 5], } #[repr(packed(1))] diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index dc056e27..f4d16c46 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -24,12 +24,14 @@ fn populate() -> Result { desc.root.add_file(file); desc.root.add_file(file2); + desc.update_dir_records(); Ok(desc) } fn run_main() -> Result<(), Error> { let desc = populate()?; + println!("\n<== Planschbecken ==>"); for element in desc.get_memory_layout().iter() { match element { Layout::SystemArea(_) => println!("SystemArea:"), diff --git a/src/Tools/psxcdgen_ex/src/types/helper.rs b/src/Tools/psxcdgen_ex/src/types/helper.rs new file mode 100644 index 00000000..0ba4a55a --- /dev/null +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -0,0 +1,38 @@ +use super::*; + +pub enum DirectoryRecordMember { + Directory{name: String}, + File{name: String}, +} + +impl DirectoryRecordMember { + pub fn get_name(&self) -> &String { + match self { + DirectoryRecordMember::Directory{name} => name, + DirectoryRecordMember::File{name} => name, + } + } +} + +pub fn collect_directory_record_member(files: &Vec, dirs: &Vec) -> Vec { + let mut collection = Vec::new(); + + for file in files { + if !file.content.is_hidden { + if let Some(name) = file.name.as_string() { + collection.push(DirectoryRecordMember::File{name}); + } + } + } + + for dir in dirs { + if !dir.content.is_hidden { + if let Some(name) = dir.name.as_string() { + collection.push(DirectoryRecordMember::Directory{name}); + } + } + } + + collection.sort_by(|a, b| {a.get_name().cmp(b.get_name())}); + collection +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index c79128f7..c9883676 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -1,3 +1,4 @@ +mod helper; pub mod layout; use cdtypes::types::cdstring::DString; @@ -24,6 +25,18 @@ impl CDDesc { pub fn get_memory_layout_mut(&mut self) -> layout::MemoryLayoutMut { layout::DefaultLayoutMut::new(self) } + + pub fn update_dir_records(&mut self) { + fn update_dir_record_of(dir: &mut Directory) { + dir.update_content(); + + for sub_dir in &mut dir.dirs { + update_dir_record_of(sub_dir); + } + } + + update_dir_record_of(&mut self.root); + } } pub struct SystemArea { @@ -45,10 +58,10 @@ impl PrimaryVolumeDescriptor { } pub struct Directory { - name: DirectoryName, - content: Content, - files: Vec, - dirs: Vec + pub name: DirectoryName, + pub content: Content, + files: Vec, + dirs: Vec } impl Directory { @@ -67,6 +80,16 @@ impl Directory { pub fn get_lba(&self) -> usize { self.content.lba } + + fn update_content(&mut self) { + let dir_member = helper::collect_directory_record_member(&self.files, &self.dirs); + + self.content.data.clear(); + println!("{} updating content", self.name.as_string().unwrap_or("".to_owned())); + for member in dir_member { + println!(">>> {}", member.get_name()); + } + } } impl std::fmt::Display for Directory { @@ -76,8 +99,8 @@ impl std::fmt::Display for Directory { } pub struct File { - name: FileName, - content: Content + pub name: FileName, + pub content: Content } impl File { @@ -106,11 +129,19 @@ impl DirectoryName { let dir_name = dir_name.to_uppercase(); Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?, len: dir_name.len()}) } + + pub fn as_string(&self) -> Option { + Some(self.as_str()?.to_owned()) + } + + fn as_str(&self) -> Option<&str> { + dstring_as_str(&self.name, self.len) + } } impl std::fmt::Display for DirectoryName { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", dstring_as_str(&self.name, self.len)) + write!(f, "{}", self.as_str().unwrap_or("")) } } @@ -137,17 +168,21 @@ impl FileName { Ok(FileName{name: DString::from_str(name)?, len: name.len(), ext, ext_len}) } + + pub fn as_string(&self) -> Option { + if let Some(ext) = &self.ext { + Some(format!("{}.{};1", dstring_as_str(&self.name, self.len)?, dstring_as_str(&ext, self.ext_len.unwrap_or(0))?)) + } + + else { + Some(format!("{}", dstring_as_str(&self.name, self.len)?)) + } + } } impl std::fmt::Display for FileName { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if let Some(ext) = &self.ext { - write!(f, "{}.{};1", dstring_as_str(&self.name, self.len), dstring_as_str(&ext, self.ext_len.unwrap_or(0))) - } - - else { - write!(f, "{}", dstring_as_str(&self.name, self.len)) - } + write!(f, "{}", self.as_string().unwrap_or("".to_owned())) } } @@ -164,9 +199,9 @@ impl Default for Content { } } -fn dstring_as_str(string: &DString, len: usize) -> &str { +fn dstring_as_str(string: &DString, len: usize) -> Option<&str> { match std::str::from_utf8(&string.as_raw()[0..len]) { - Ok(str) => str, - Err(_) => "???", + Ok(str) => Some(str), + Err(_) => None, } } \ No newline at end of file