From f6e6c3fc90b5aa71798715346db8f352725f6069 Mon Sep 17 00:00:00 2001 From: Tetsuya Kitaoka Date: Wed, 19 Jun 2024 18:57:59 +0000 Subject: [PATCH] =?UTF-8?q?(#6)=20string.h,=20stdio.h=20=E3=82=92=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E3=81=AB=E5=BF=9C=E3=81=98=E3=81=A6=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/code_organizer.rs | 16 ++++++++++++++-- src/model/code_parser.rs | 11 ++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/model/code_organizer.rs b/src/model/code_organizer.rs index cf81915..57af912 100644 --- a/src/model/code_organizer.rs +++ b/src/model/code_organizer.rs @@ -17,7 +17,7 @@ impl CodeOrganizer for TigressCodeOrganizer { let source_code: String = fs::read_to_string(&src_path).unwrap(); let analyze_result: Vec = CSourceCodeParser::parse(&source_code)?; - // dbg!("{}", &analyze_result); + // dbg!(&analyze_result); let extern_function_names: Vec<&str> = analyze_result .iter() @@ -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 "); + } + if valid_extern_function_names.iter().any(|x| x == &"strcat") { + include_library.push("#include "); + } if valid_extern_function_names .iter() .any(|x| x == &"rand" || x == &"*malloc") { - organized_code = "#include \n\n".to_string() + &organized_code; + include_library.push("#include "); } + organized_code = include_library.join("\n") + "\n\n" + &organized_code; let mut file: File = File::create(dst_path)?; write!(file, "{}", organized_code)?; diff --git a/src/model/code_parser.rs b/src/model/code_parser.rs index cd11666..a36c15a 100644 --- a/src/model/code_parser.rs +++ b/src/model/code_parser.rs @@ -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() }