Skip to content

Commit

Permalink
Ignore duplicate keywords and add CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
x87 committed Nov 30, 2020
1 parent d24e2b8 commit 59077b5
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 72 deletions.
82 changes: 81 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "make_opcodes"
version = "0.1.0"
version = "0.1.1"
authors = ["Seemann <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sanny_builder_core = { git = "https://github.com/sannybuilder/core", branch = "master", package = "sanny_builder_core" }
sanny_builder_core = { git = "https://github.com/sannybuilder/core", branch = "master", package = "sanny_builder_core" }
argh = "0.1.4"
164 changes: 95 additions & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,104 +1,130 @@
extern crate argh;
extern crate sanny_builder_core as sb;
use argh::FromArgs;
use sb::dictionary::dictionary_num_by_str::DictNumByStr;
use sb::dictionary::ffi::*;
use sb::namespaces::namespaces::Namespaces;
use std::ffi::CString;

#[derive(FromArgs)]
///
struct Options {
/// input text file to read
#[argh(positional)]
input: String,

/// path to output file
#[argh(option)]
output: Option<String>,

/// path to classes.db
#[argh(option)]
classes: Option<String>,

/// path to keywords.txt
#[argh(option)]
keywords: Option<String>,
}
fn main() {
if let Some(input_file) = std::env::args().nth(1) {
match std::fs::read(input_file) {
Ok(buf) => {
let file_content = String::from_utf8_lossy(&buf);
let mut uniq_opcodes = vec![];
let mut output = vec![];
let mut keywords = DictNumByStr::new(
Duplicates::Replace,
CaseFormat::NoFormat,
String::from(";"),
String::from("=,"),
true,
true,
);
let mut classes = Namespaces::new();
let options: Options = argh::from_env();

// todo: read from command line
let has_keywords = keywords.load_file("keywords.txt").is_some();
let has_classes = classes.load_classes("classes.db").is_some();
match std::fs::read(options.input) {
Ok(buf) => {
let file_content = String::from_utf8_lossy(&buf);
let mut uniq_opcodes: Vec<String> = vec![];
let mut output = vec![];
let mut keywords = DictNumByStr::new(
Duplicates::Replace,
CaseFormat::LowerCase,
String::from(";"),
String::from("=,"),
true,
true,
);
let mut classes = Namespaces::new();

for line in file_content.lines() {
let line = line.trim();
let has_keywords = keywords
.load_file(options.keywords.unwrap_or(String::new()).as_str())
.is_some();
let has_classes = classes
.load_classes(options.classes.unwrap_or(String::new()).as_str())
.is_some();

// opcode
if let Some(':') = line.chars().nth(4) {
if let Some(opcode) = line.get(0..4) {
if !uniq_opcodes.contains(&opcode) {
uniq_opcodes.push(opcode);
output.push(line);
continue;
}
for line in file_content.lines() {
let line = line.trim();

// opcode
if let Some(':') = line.chars().nth(4) {
if let Some(opcode) = line.get(0..4) {
let opcode = String::from(opcode);
if !uniq_opcodes.contains(&opcode) {
uniq_opcodes.push(opcode);
output.push(line);
continue;
}
}
}

if !has_classes && !has_keywords {
continue;
}
if !has_classes && !has_keywords {
continue;
}

if let Some(first_word) = line
.split(|c: char| {
c.is_ascii_whitespace() || c == '(' || c == ')' || c == ','
})
.next()
{
// keyword
if has_keywords {
if let Some(key) = CString::new(first_word).ok() {
if keywords.map.contains_key(&key) {
if !uniq_opcodes.contains(&first_word) {
uniq_opcodes.push(first_word);
output.push(line);
continue;
}
if let Some(first_word) = line
.split(|c: char| c.is_ascii_whitespace() || c == '(' || c == ')' || c == ',')
.map(|s| s.to_ascii_lowercase())
.next()
{
// keyword
if has_keywords {
if let Some(key) = CString::new(first_word.clone()).ok() {
if keywords.map.contains_key(&key) {
if !uniq_opcodes.contains(&first_word) {
uniq_opcodes.push(first_word);
output.push(line);
continue;
}
}
}
}

// classes
if has_classes {
if first_word.contains('.') {
let mut class = first_word.split(|c| c == '.');
// classes
if has_classes {
if first_word.contains('.') {
let mut class = first_word.split(|c| c == '.');

if let Some(class_name) = class.next() {
if let Some(member_name) = class.next() {
// bug: see https://github.com/sannybuilder/dev/issues/92
if classes
.get_opcode_index_by_name(class_name, member_name)
.is_some()
{
if !uniq_opcodes.contains(&first_word) {
uniq_opcodes.push(first_word);
output.push(line);
continue;
}
if let Some(class_name) = class.next() {
if let Some(member_name) = class.next() {
// bug: see https://github.com/sannybuilder/dev/issues/92
if classes
.get_opcode_index_by_name(class_name, member_name)
.is_some()
{
if !uniq_opcodes.contains(&first_word) {
uniq_opcodes.push(first_word);
output.push(line);
continue;
}
}
}
}
}
}
}
}

output.sort();
output.sort();

if let Some(output_file) = options.output {
std::fs::write(output_file, output.join("\n")).unwrap();
} else {
for line in output {
println!("{}", line);
}
}
Err(e) => {
println!("{}", e);
std::process::exit(2);
}
}
} else {
println!("Usage: make_opcodes.exe <input file>");
Err(e) => {
println!("{}", e);
std::process::exit(2);
}
}
}

0 comments on commit 59077b5

Please sign in to comment.