Initial push
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/target
|
||||||
|
/root
|
||||||
|
*.swp
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "npons"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "npons"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
0
src/encode.rs
Normal file
0
src/encode.rs
Normal file
139
src/files.rs
Normal file
139
src/files.rs
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
use std::fs;
|
||||||
|
pub fn read_file(file_name: &str, depth: u8) -> Vec<u8>
|
||||||
|
{
|
||||||
|
if let Ok(file_contents) = fs::read_to_string(format!("root/{}",file_name))
|
||||||
|
{
|
||||||
|
let bytes = encode_npon(&file_contents, depth);
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
|
return vec![]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_npon(file_contents: &String, depth: u8) -> Vec<u8>
|
||||||
|
{
|
||||||
|
let mut bytes: Vec<u8> = vec![];
|
||||||
|
if depth > 63 {
|
||||||
|
println!("Depth level safety reached (63), recursion error suspected");
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
if depth == 0 {
|
||||||
|
bytes.push(01);
|
||||||
|
}
|
||||||
|
|
||||||
|
// flag
|
||||||
|
// 0 = nothing
|
||||||
|
// 1 = in a string
|
||||||
|
// 2 = in a name
|
||||||
|
// 3 = in import
|
||||||
|
let mut index_flag: usize = 0;
|
||||||
|
let mut flag: u8 = 0;
|
||||||
|
let mut backslash_flag: bool = false;
|
||||||
|
for (index, character) in file_contents.bytes().enumerate()
|
||||||
|
{
|
||||||
|
if backslash_flag
|
||||||
|
{
|
||||||
|
match character
|
||||||
|
{
|
||||||
|
110 => // newline
|
||||||
|
{
|
||||||
|
bytes.push(14);
|
||||||
|
},
|
||||||
|
116 => // tab
|
||||||
|
{
|
||||||
|
bytes.push(15);
|
||||||
|
},
|
||||||
|
_ => bytes.push(character), // Otherwise just push the character
|
||||||
|
}
|
||||||
|
backslash_flag = false;
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
backslash_flag = false;
|
||||||
|
//print!("{} ", character);
|
||||||
|
match flag
|
||||||
|
{
|
||||||
|
0 =>
|
||||||
|
{
|
||||||
|
match character
|
||||||
|
{
|
||||||
|
40 =>
|
||||||
|
{
|
||||||
|
bytes.push(03); // ( for name start
|
||||||
|
flag = 2;
|
||||||
|
}
|
||||||
|
123 => bytes.push(05),
|
||||||
|
125 => bytes.push(06),
|
||||||
|
58 =>
|
||||||
|
{
|
||||||
|
index_flag = index + 1;
|
||||||
|
flag = 3;
|
||||||
|
},
|
||||||
|
39 | 34 =>
|
||||||
|
{
|
||||||
|
bytes.push(07);
|
||||||
|
flag = 1;
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
},
|
||||||
|
1 => // in a string
|
||||||
|
{
|
||||||
|
match character
|
||||||
|
{
|
||||||
|
39 | 34 =>
|
||||||
|
{
|
||||||
|
bytes.push(08);
|
||||||
|
flag = 0;
|
||||||
|
},
|
||||||
|
92 =>
|
||||||
|
{
|
||||||
|
backslash_flag = true;
|
||||||
|
},
|
||||||
|
_ => bytes.push(character),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
},
|
||||||
|
2 => // in a name
|
||||||
|
{
|
||||||
|
match character
|
||||||
|
{
|
||||||
|
41 =>
|
||||||
|
{
|
||||||
|
bytes.push(04); // ) for name end
|
||||||
|
flag = 0;
|
||||||
|
},
|
||||||
|
92 =>
|
||||||
|
{
|
||||||
|
backslash_flag = true;
|
||||||
|
},
|
||||||
|
_ => bytes.push(character),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
},
|
||||||
|
3 => // in import
|
||||||
|
{
|
||||||
|
match character
|
||||||
|
{
|
||||||
|
58 =>
|
||||||
|
{
|
||||||
|
let new_bytes = read_file(&file_contents[index_flag..index], depth+1);
|
||||||
|
for byte in new_bytes.iter()
|
||||||
|
{
|
||||||
|
bytes.push(*byte);
|
||||||
|
}
|
||||||
|
flag = 0;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
},
|
||||||
|
_ => println!("ERROR: Invalid flag code or 'null'"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if depth == 0 {
|
||||||
|
bytes.push(02);
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes
|
||||||
|
}
|
||||||
32
src/main.rs
Normal file
32
src/main.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::
|
||||||
|
{
|
||||||
|
io::{BufReader, prelude::*},
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
};
|
||||||
|
|
||||||
|
mod files;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()>
|
||||||
|
{
|
||||||
|
let bind_address: &str = "127.0.0.1:2025";
|
||||||
|
let listener = TcpListener::bind(bind_address).unwrap();
|
||||||
|
println!("Listening at {}", bind_address);
|
||||||
|
|
||||||
|
for stream in listener.incoming() {
|
||||||
|
let stream = stream.unwrap();
|
||||||
|
handle_connection(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_connection(mut stream: TcpStream)
|
||||||
|
{
|
||||||
|
let buf_reader = BufReader::new(&stream);
|
||||||
|
println!("CONNECTION: {buf_reader:#?}");
|
||||||
|
let bytes = files::read_file("index.npon", 0);
|
||||||
|
stream.write_all(bytes.as_slice()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user