Skip to content
Snippets Groups Projects
Commit 11ab0399 authored by iliya.saroukha's avatar iliya.saroukha
Browse files

importing modules, removed create_file, _dir and other functions, replaced...

importing modules, removed create_file, _dir and other functions, replaced with algo_template function that does the necessary work
parent a1917d1a
Branches
No related tags found
No related merge requests found
mod c_templates; mod code_templates;
use ptree::{print_tree, TreeBuilder}; mod from_stdin;
use std::fs::File; mod to_stdout;
use std::io::{stdin, stdout, Write}; use std::fs::{create_dir, File};
use std::process::Command; use std::io::Write;
fn input_from_stdin() -> String { fn algo_template() {
let mut input = String::new(); let struct_dir = "struct";
let slash = "/";
let _ = stdout().flush().unwrap(); print!("\nPlease specify the name of the data structure: ");
stdin().read_line(&mut input).expect("Failed to read input"); let dir_name = from_stdin::input_from_stdin();
return input; // Creating root path string
} let root_dir_name = &(dir_name.trim().to_owned() + slash);
fn create_file(filename: &str) {
let mut file = File::create(filename.trim()).expect("Cannot create file");
let code = c_templates::template_for_header(filename).to_string();
file.write_all(code.as_bytes()).unwrap();
}
fn create_directory(dir_name: &str) {
let mut create_dir = Command::new("mkdir");
// Creating 'structs' dir
create_dir
.arg(dir_name.trim())
.output()
.expect("Create directory -> failed");
}
fn change_directory(dir: &str) {
// let new_root = Path::new(dir);
// let mut cd = Command::new("cd"); // Creating root directory using std::fs
create_dir(&root_dir_name).expect(&format!("Failed at creating {} directory", root_dir_name));
// cd.arg(concat!(path.trim(), "/")) print!("\nPlease specify the name of the main file (w/ the extension): ");
// .output() let main_name = from_stdin::input_from_stdin();
// .expect("cd -> failed"); let main_path = &(root_dir_name.to_owned() + main_name.trim());
// assert!(set_current_dir(&new_root).is_ok()); // Create main file in root dir
// println!("You find yourself in the {} directory", new_root.display()); let mut main_file =
} File::create(&main_path).expect(&format!("Cannot create '{}' file", main_path.trim()));
fn create_project() {
println!();
println!("-----------------CREATE PROJECT-----------------");
println!();
// Taking input -> name of main file // Storing code template for main file into variable
print!("Please specify the name of the main file (w/ the extension): "); let main_template = code_templates::template_for_main().to_string();
create_file(&input_from_stdin());
println!(); // Writing to main file
main_file
.write_all(main_template.as_bytes())
.expect(&format!("Not able to write to {}", main_path.trim()));
// Taking input -> name of directory // Creating struct dir
print!("Please specify the name of the directory: "); let struct_path = &(root_dir_name.trim().to_owned() + struct_dir + slash);
let new_dir = &input_from_stdin(); create_dir(&struct_path).expect(&format!("Failed at creating {} directory", struct_path));
create_directory(&new_dir); // Creating string for header file path
change_directory(&new_dir); let header_file_name = &(dir_name.trim().to_owned() + ".h");
let data_struct_header = &(struct_path.to_owned() + header_file_name);
// println!("ATM, you find yourself in the {0} directory", new_dir); // Creating header file in struct dir
println!("Let's create a header file and implementation file.."); let mut header_file = File::create(&data_struct_header)
println!(); .expect(&format!("Cannot create '{}' file", data_struct_header));
print!("Please specify the name of the .h file (w/ the extension): "); // Storing code template for header file into variable
create_file(&input_from_stdin()); let header_template = code_templates::template_for_header(header_file_name).to_string();
print!("Please specify the name of the .c file (w/ the extension): "); // Writing to header file
create_file(&input_from_stdin()); header_file
.write_all(header_template.as_bytes())
.expect(&format!("Not able to write to {}", data_struct_header))
} }
fn algo_tree() {
let tree = TreeBuilder::new("data_struct".to_string())
.begin_child("struct".to_string())
.add_empty_child("struct.h".to_string())
.add_empty_child("struct.c".to_string())
.end_child()
.add_empty_child("main.c".to_string())
.build();
print_tree(&tree).expect("Not able to print");
println!();
}
fn exam_tree() {
let tree = TreeBuilder::new("prog_seq_exam_<month>".to_string())
.begin_child("ex1".to_string())
.add_empty_child("ex1.c".to_string())
.add_empty_child("Makefile".to_string())
.end_child()
.begin_child("ex...".to_string())
.add_empty_child("etc...".to_string())
.end_child()
.build();
print_tree(&tree).expect("Not able to print");
println!();
}
fn algo_template() {}
fn exam_template() {} fn exam_template() {}
fn print_menu() {
println!();
println!(" ----------------------------------------------- ");
println!("|\tWelcome to CLI_C_PROJECT_SETUP\t\t|");
println!(" ----------------------------------------------- ");
println!("What kind of template do you want?");
println!();
println!("1) Algo");
algo_tree();
println!("2) Exam");
exam_tree();
}
fn main() { fn main() {
print_menu(); to_stdout::print_menu();
print!("What's your choice: "); print!("What's your choice: ");
match input_from_stdin() match from_stdin::input_from_stdin()
.trim() .trim()
.parse::<u8>() .parse::<u8>()
.expect("Please input an integer") .expect("Please input an integer")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment