diff --git a/.gitignore b/.gitignore index 193d30e..cb14dc7 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ Cargo.lock # Added by cargo /target + +# Testing files +test/ +Makefile diff --git a/Cargo.toml b/Cargo.toml index cf61e08..0a1db99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1.9.5" diff --git a/src/main.rs b/src/main.rs index 0bc6add..f02172b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,78 @@ use std::fs; +use std::io::Read; +use std::path::Path; +use regex::Regex; -fn main() { - let files = fs::read_dir("./").unwrap(); +#[derive(Debug)] +struct CFile { + path: String, + filename: String, + headers: Vec, +} +fn parse_c_file(path: &Path, re: &Regex) -> CFile { + let strpath = path.to_str().unwrap(); + let filename = path.file_stem().unwrap().to_str().unwrap(); + + let mut file_handle = fs::File::open(path).unwrap(); + let mut contents = String::new(); + file_handle.read_to_string(&mut contents).unwrap(); + + let mut headers: Vec = Vec::new(); + let caps = re.captures_iter(&contents); + for (_, [include_file]) in caps.map(|c| c.extract()) { + headers.push(path.parent().unwrap().join(Path::new(include_file)).to_str().unwrap().to_string()); + } + + return CFile { path: strpath.to_string(), filename: filename.to_string(), headers } + +} + +fn recurse_into_dir(path: &Path, file_callback: &mut F) { + let files = fs::read_dir(path).unwrap(); for file in files { let file = file.unwrap(); + let meta = file.metadata().unwrap(); + let is_dir = meta.is_dir(); + let is_file = meta.is_file(); + let path = file.path(); + let name = file.file_name(); - let is_dir = file.metadata().unwrap().is_dir(); + // ignore hidden files + if name.to_str().unwrap().chars().nth(0).unwrap() == '.' { + continue; + } if is_dir { - println!("{}", file.path().to_str().unwrap()); + recurse_into_dir(&path, file_callback); + } else if is_file { + file_callback(file); } } } + +fn main() { + let mut project_files: Vec = Vec::new(); + let includes_regex = Regex::new(r#"#include\s+"(.*?)""#).unwrap(); + + recurse_into_dir(Path::new("./"), &mut |file| { + let path = file.path(); + match path.extension() { + Some(v) => { + let v = v.to_str().unwrap(); + if v != "c" { + return; + } + project_files.push(parse_c_file(&path, &includes_regex)); + }, + None => { + return; + }, + } + }); + + for file in project_files.iter() { + println!("{:?}", file); + } + +}