directory tree parsing done (and its a mess)
This commit is contained in:
		
							parent
							
								
									a1bcfa5a94
								
							
						
					
					
						commit
						c7e42eba71
					
				
					 3 changed files with 72 additions and 4 deletions
				
			
		
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -19,3 +19,7 @@ Cargo.lock
 | 
			
		|||
# Added by cargo
 | 
			
		||||
 | 
			
		||||
/target
 | 
			
		||||
 | 
			
		||||
# Testing files
 | 
			
		||||
test/
 | 
			
		||||
Makefile
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										71
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										71
									
								
								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<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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<String> = 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<F: FnMut(fs::DirEntry)>(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<CFile> = 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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue