summaryrefslogtreecommitdiff
path: root/src/parsers/ini
diff options
context:
space:
mode:
authorBenedict Börger <benedict@0xb8000.de>2019-03-23 15:40:35 +0100
committerBenedict Börger <benedict@0xb8000.de>2019-03-23 15:41:11 +0100
commit2e0a6909cbfb2479edd7fba78fa4d0135a79ae3f (patch)
tree83070d9e1489faaea9a94609e7ff5bccedeb66d5 /src/parsers/ini
parent7fcdc3ecc0f077ff7ff4ec57c912beae4f974fdb (diff)
[global] refactoring code base
Diffstat (limited to 'src/parsers/ini')
-rw-r--r--src/parsers/ini/mod.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/parsers/ini/mod.rs b/src/parsers/ini/mod.rs
new file mode 100644
index 0000000..9633231
--- /dev/null
+++ b/src/parsers/ini/mod.rs
@@ -0,0 +1,64 @@
+use std::fs::File;
+use std::io;
+use std::io::BufRead;
+use std::collections::HashMap;
+
+pub struct IniFile {
+ pub section : HashMap<String, Vec<IniSection>>
+}
+
+pub struct IniSection {
+ pub section_name : String,
+ pub properties : HashMap<String, Vec<String>>
+}
+
+impl IniFile {
+ pub fn from_file(path : &str) -> Result<IniFile, io::Error> {
+ let mut file = HashMap::new();
+ let fd = File::open(path)?;
+ let reader = io::BufReader::new(fd);
+ let mut current_section = String::from("");
+
+ for line in reader.lines() {
+ let line = line?;
+ let line = line.trim().to_string();
+
+ if line.starts_with("#") || line.is_empty() {
+ println!("kommentar or empts: {}", line);
+ continue;
+ }
+ // another section begins
+ if line.starts_with("[") && line.ends_with("]") {
+ let nam = line.get(1..(line.len()-1));
+ let n;
+ match nam {
+ Some(sec_name) => n = sec_name,
+ // TODO no name given, what should be done
+ None => n = ""
+ }
+ current_section = n.to_string();
+ // TODO maybe the sections has been specified twice?
+ file.insert(n.to_string(), Vec::new() );
+ continue;
+
+ }
+ let kv : Vec<&str> = line.split("=").collect();
+ let mut key = String::from("");
+ if let Some(t) = kv.get(0) {
+ key = t.to_string();
+ }
+ let mut value = String::from("");
+ if let Some(t) = kv.get(1) {
+ value = t.to_string();
+ }
+ if let Some(section) = file.get_mut(&current_section) {
+ // get the entry with key from vector
+ if let Some(ent) = section.get_mut(&key) {
+ ent.insert(value.to_string());
+ }
+ }
+ }
+ Ok(IniFile { sections : file })
+ }
+
+}