Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
vit1251 committed Aug 17, 2021
1 parent 3d6d5c2 commit 23e8164
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = "*"
60 changes: 47 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@

use log::{trace,debug};
use std::fs::File;
use std::io::Write;
use std::io::LineWriter;

struct AutoCfg {
config_dir: String
struct Definition {
name: String,
defined: bool,
value: String,
optional: bool,
}

pub struct AutoCfg {
config_dir: String,
defines: Vec<Definition>,
}

impl AutoCfg {

fn new() -> AutoCfg {
AutoCfg {
config_dir: String::from("."),
}
pub fn new() -> AutoCfg {
let mut result = AutoCfg {
config_dir: String::from("config.h"),
defines: Vec::<Definition>::new(),
};
result.init();
result
}

fn write(&self) -> Result<(), std::io::Error> {
fn init(&mut self) {
self.set_quoted("PACKAGE_NAME", "example");
self.set_quoted("VERSION", "1.0.0")
}

let mut stream = File::create(self.config_dir);
pub fn set(&mut self, name: &str, value: &str) {
trace!("set: name = {} value = {}", name, value);
// TODO - save in cache ...
self.defines.push(Definition{
name: String::from(name),
defined: true,
value: String::from(value),
optional: false,
});
}

let mut output = LineWriter::new(stream);
pub fn set_quoted(&mut self, name: &str, value: &str) {
let new_value = format!("\"{}\"", value);
self.set(name, &new_value);
}

pub fn write(&self) -> Result<(), std::io::Error> {

output.write_all("#define FOO_BAR_H 1\n");
let mut stream = File::create(&self.config_dir)?;

output.write_all(format!("#define PACKAGE_NAME \"{}\"\n", "example"));
let mut output = LineWriter::new(stream);

for def in &self.defines {
let rec = format!("#define {} {}\n", def.name, def.value);
output.write_all(rec.as_bytes())?;
}

output.write_all(format!("#define VERSION \"{}\"\n", "1.0.0"));

let _status = output.flush();
output.flush()?;

Ok(())
}
Expand Down

0 comments on commit 23e8164

Please sign in to comment.