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

Refactor API for reading/writing data #109

Merged
merged 3 commits into from
Sep 21, 2024
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
22 changes: 20 additions & 2 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::io::register_io;
use crate::loc::register_loc;
use crate::utils::register_utils;
use crate::writer::Writer;
use alloc::sync::Arc;
use alloc::{collections::BTreeMap, sync::Arc};
use core::mem;
use parking_lot::{Mutex, RwLock};
use rair_env::Environment;
use rair_io::RIO;
use rair_io::{IoError, RIO};
use serde::{Deserialize, Serialize};
use std::io;
use std::io::Write;
Expand Down Expand Up @@ -194,6 +194,24 @@ impl Core {
self.command_not_found(command);
}
}
pub fn read_sparce(&mut self, loc: u64, size: u64) -> Result<BTreeMap<u64, u8>, IoError> {
match self.mode {
AddrMode::Phy => self.io.pread_sparce(loc, size),
AddrMode::Vir => self.io.vread_sparce(loc, size),
}
}
pub fn read(&mut self, loc: u64, buf: &mut [u8]) -> Result<(), IoError> {
match self.mode {
AddrMode::Phy => self.io.pread(loc, buf),
AddrMode::Vir => self.io.vread(loc, buf),
}
}
pub fn write(&mut self, loc: u64, buf: &[u8]) -> Result<(), IoError> {
match self.mode {
AddrMode::Phy => self.io.pwrite(loc, buf),
AddrMode::Vir => self.io.vwrite(loc, buf),
}
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions core/src/hex/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::Core;
use rair_env::Environment;

pub fn one_byte(_: &str, value: &str, _: &Environment<Core>, _: &mut Core) -> bool {
value.len() == 1
}
130 changes: 130 additions & 0 deletions core/src/hex/hex_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use super::helper::one_byte;
use crate::{is_color, Core, Writer};
use std::io::Write;
use yansi::Paint;

#[derive(Clone)]
pub struct HexEnv {
// color for banner and offsets
pub banner: (u8, u8, u8),
// color for the ascii part that
// is not printable
pub na: (u8, u8, u8),
// character to print in case of gap
pub gap: char,
// character to print (with na color)
pub noprint: char,
}

impl HexEnv {
pub(super) fn new(core: &mut Core) -> Self {
let env = core.env.clone();
env.write()
.add_str_with_cb(
"hex.headerColor",
"color.6",
"Color used in the header of when using commands working with hex data",
core,
is_color,
)
.unwrap();
env.write()
.add_str_with_cb(
"hex.nonPrintColor",
"color.5",
"Color used in the Ascii section for non printable ASCII when using commands that work with hex data",
core,
is_color,
)
.unwrap();
env.write()
.add_str_with_cb(
"hex.nonPrintReplace",
".",
"Text used in the Ascii section to replace non printable ASCII when using when using commands that work with hex data",
core,
one_byte,
)
.unwrap();
env.write()
.add_str_with_cb(
"hex.gapReplace",
"#",
"Text used to replace gaps when using when using commands that work with hex data",
core,
one_byte,
)
.unwrap();

Self {
banner: (0, 0, 0),
na: (0, 0, 0),
gap: char::default(),
noprint: char::default(),
}
}
pub(super) fn get_env(&mut self, core: &mut Core) -> &Self {
let env = core.env.read();
let color = env.get_str("hex.headerColor").unwrap();
self.banner = env.get_color(color).unwrap();
let color = env.get_str("hex.nonPrintColor").unwrap();
self.na = core.env.read().get_color(color).unwrap();
self.gap = env
.get_str("hex.gapReplace")
.unwrap()
.chars()
.next()
.unwrap();
self.noprint = env
.get_str("hex.nonPrintReplace")
.unwrap()
.chars()
.next()
.unwrap();
self
}
pub fn print_banner_with_newline(&self, writer: &mut Writer, newline: bool) {
let nl = if newline { "\n" } else { "" };
write!(
writer,
"{}{nl}",
"- offset - 0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF".rgb(
self.banner.0,
self.banner.1,
self.banner.2,
)
)
.unwrap();
}
pub fn print_banner(&self, writer: &mut Writer) {
self.print_banner_with_newline(writer, true);
}
pub fn print_addr(&self, writer: &mut Writer, loc: u64) {
let loc = format!("0x{loc:08x}");
let (r, g, b) = self.banner;
let loc_colored = loc.rgb(r, g, b);
write!(writer, "{loc_colored} ").unwrap();
}
// print hex data all while taking care of extra white space
pub fn print_hex(&self, data: Option<u8>, writer: &mut Writer, space_after: bool) {
let space = if space_after { " " } else { "" };
if let Some(c) = data {
write!(writer, "{c:02x}{space}").unwrap();
} else {
write!(writer, "{}{}{space}", self.gap, self.gap).unwrap();
}
}
// print ascii data while taking care of non printable characters and coloring
pub fn print_ascii(&self, data: Option<u8>, writer: &mut Writer) {
let (r, g, b) = self.na;
if let Some(c) = data {
if (0x21..=0x7E).contains(&c) {
write!(writer, "{}", c as char).unwrap();
} else {
write!(writer, "{}", self.noprint.rgb(r, g, b)).unwrap();
}
} else {
write!(writer, "{}", self.gap.rgb(r, g, b)).unwrap();
}
}
}
17 changes: 17 additions & 0 deletions core/src/hex/hex_without_env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::HexEnv;
use crate::Core;

pub struct HexWithoutEnv {
inner: HexEnv,
}

impl HexWithoutEnv {
pub fn new(core: &mut Core) -> Self {
Self {
inner: HexEnv::new(core),
}
}
pub fn get_env(&mut self, core: &mut Core) -> &HexEnv {
self.inner.get_env(core)
}
}
8 changes: 8 additions & 0 deletions core/src/hex/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! module for working with hex data

mod helper;
mod hex_env;
mod hex_without_env;

pub use hex_env::HexEnv;
pub use hex_without_env::HexWithoutEnv;
Loading
Loading