-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
200 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The Project Gutenberg eBook of Anna Karenina, by Leo Tolstoy | ||
This eBook is for the use of anyone anywhere in the United States and most other parts of the world at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org. If you are not located in the United States, you will have to check the laws of the country where you are located before using this eBook. | ||
Title: Anna Karenina | ||
Author: Leo Tolstoy | ||
Release Date: July 1, 1998 [eBook #1399]w |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
111 | ||
# 100100000 | ||
% 0100011 | ||
, 1000010 | ||
- 100100001 | ||
. 1001100 | ||
1 11000001 | ||
3 100100010 | ||
8 100100011 | ||
9 1000011 | ||
: 0000100 | ||
A 0000101 | ||
B 1001101 | ||
D 100100100 | ||
G 01000101 | ||
I 100100101 | ||
J 100100110 | ||
K 10001010 | ||
L 0000110 | ||
P 10001011 | ||
R 100100111 | ||
S 10001100 | ||
T 1001110 | ||
U 10001101 | ||
Y 110000000 | ||
[ 110000001 | ||
] 01000100 | ||
a 0011 | ||
b 1001111 | ||
c 110001 | ||
d 100101 | ||
e 011 | ||
f 100000 | ||
g 010000 | ||
h 10100 | ||
i 11001 | ||
j 10001110 | ||
k 1100001 | ||
l 00101 | ||
m 1000100 | ||
n 0101 | ||
o 1101 | ||
p 10001111 | ||
r 0001 | ||
s 10101 | ||
t 1011 | ||
u 01001 | ||
v 0000111 | ||
w 00000 | ||
y 00100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
use std::collections::BTreeMap; | ||
use std::io::{Read, Result,Write, BufReader, BufRead}; | ||
use std::fs::File; | ||
use std::fs; | ||
|
||
/* | ||
Read a BTreeMap from the key file. | ||
*/ | ||
pub fn get_hash_from_txt(file_name: &str) -> Result<BTreeMap<char, String> > { | ||
// Create a BTreeMap to store the data | ||
let mut data_map: BTreeMap<char, String> = BTreeMap::new(); | ||
|
||
if let Ok(file) = File::open(file_name) { | ||
let reader = BufReader::new(file); | ||
|
||
for (_line_number, line) in reader.lines().enumerate() { | ||
if let Ok(line_text) = line { | ||
|
||
// Check if the line has the expected format | ||
if let (Some(first_char), Some(rest)) = (line_text.chars().next(),line_text.get(1..)) { | ||
// Trim white space from the "rest" string | ||
let trimmed_rest = rest.trim(); | ||
// Insert the data into the BTreeMap | ||
data_map.insert(first_char, trimmed_rest.to_string()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(data_map) | ||
} | ||
|
||
pub fn read_file_to_string(filename: &str) -> Result<String> { | ||
// Open the file in read-only mode | ||
let mut file = File::open(filename)?; | ||
|
||
// Read the entire content of the file into a String | ||
let mut content = String::new(); | ||
file.read_to_string(&mut content)?; | ||
|
||
Ok(content) | ||
} | ||
|
||
|
||
pub fn get_file_size_bytes(filename: &str) -> Result<u64>{ | ||
match fs::metadata(filename) { | ||
Ok(metadata) => { | ||
let file_size = metadata.len(); | ||
return Ok(file_size); | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
} | ||
} | ||
} | ||
|
||
pub fn write_str_to_file(filename: &str, data: &Vec<bool>) -> std::io::Result<()>{ | ||
let mut file = File::create(filename)?; | ||
|
||
let _byte_buffer: u8 = 0; | ||
let _bit_position = data.len(); | ||
|
||
// Create a buffer to store bits | ||
let mut bit_buffer: u8 = 0; | ||
let mut bit_position = 0; // Start from the least significant bit | ||
|
||
// read the bits | ||
for bit in data.iter() { | ||
if *bit { | ||
bit_buffer |= 1 << bit_position; | ||
} | ||
|
||
bit_position += 1; | ||
|
||
// If we've filled a byte, write it to the file | ||
if bit_position == 8 { | ||
file.write(&[bit_buffer])?; | ||
bit_buffer = 0; | ||
bit_position = 0; | ||
} | ||
} | ||
|
||
// If there are any remaining bits in the buffer, write them and pad with '0's | ||
println!("bit position at end: {}",bit_position); | ||
let num_bits_to_write = 8 - bit_position; | ||
|
||
let two = 2; | ||
|
||
let bit_buffer = two.pow(num_bits_to_write)-1; | ||
|
||
if bit_position != 0 { | ||
file.write(&[bit_buffer])?; | ||
} | ||
|
||
let _ = file.flush(); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn print_hash_to_file(map: &BTreeMap<char,Vec<bool>>, filename: &str) -> std::io::Result<()>{ | ||
let mut file = File::create(filename)?; | ||
|
||
for (key,value) in map{ | ||
file.write(&[key.to_owned() as u8])?; | ||
file.write(&[' ' as u8])?; | ||
for bool_val in value{ | ||
if *bool_val{ | ||
file.write(&['1' as u8])?; | ||
}else{ | ||
file.write(&['0' as u8])?; | ||
} | ||
} | ||
file.write(&['\n' as u8])?; | ||
} | ||
file.flush()?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod huffman; | ||
pub mod node; | ||
pub mod node; | ||
pub mod file_reader; |
Oops, something went wrong.