Skip to content

Commit

Permalink
Merge pull request #19 from NAIST-SE/feature/#6-RefactorAdjustingCode
Browse files Browse the repository at this point in the history
(#6) string.h, stdio.h を必要に応じて追加する処理を追加
  • Loading branch information
nosfill authored Jun 19, 2024
2 parents d770335 + f6e6c3f commit a8d04a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/model/code_organizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl CodeOrganizer for TigressCodeOrganizer {
let source_code: String = fs::read_to_string(&src_path).unwrap();

let analyze_result: Vec<AnalyzedCodeData> = CSourceCodeParser::parse(&source_code)?;
// dbg!("{}", &analyze_result);
// dbg!(&analyze_result);

let extern_function_names: Vec<&str> = analyze_result
.iter()
Expand Down Expand Up @@ -85,12 +85,24 @@ impl CodeOrganizer for TigressCodeOrganizer {
organized_code += &x.contents.join("\n");
organized_code += &"\n";
}

let mut include_library = vec![];
if valid_extern_function_names
.iter()
.any(|x| x == &"printf" || x == &"sprintf")
{
include_library.push("#include <stdio.h>");
}
if valid_extern_function_names.iter().any(|x| x == &"strcat") {
include_library.push("#include <string.h>");
}
if valid_extern_function_names
.iter()
.any(|x| x == &"rand" || x == &"*malloc")
{
organized_code = "#include <stdlib.h>\n\n".to_string() + &organized_code;
include_library.push("#include <stdlib.h>");
}
organized_code = include_library.join("\n") + "\n\n" + &organized_code;

let mut file: File = File::create(dst_path)?;
write!(file, "{}", organized_code)?;
Expand Down
11 changes: 10 additions & 1 deletion src/model/code_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,16 @@ impl<'a> CSourceCodeParser<'a> {
}

fn parse_function_name(line: &str) -> &str {
let end: usize = line.find("(").unwrap();
let mut end: usize = line.rfind("(").unwrap();
loop {
let Some(t) = line.chars().nth(end - 1) else {
break;
};
if t != ')' {
break;
}
end -= 1;
}
let start: usize = line[..end].rfind(" ").unwrap();
line[start + 1..end].trim()
}
Expand Down

0 comments on commit a8d04a8

Please sign in to comment.