summaryrefslogtreecommitdiff
path: root/src/parsers
diff options
context:
space:
mode:
Diffstat (limited to 'src/parsers')
-rw-r--r--src/parsers/csv/mod.rs93
-rw-r--r--src/parsers/ini/mod.rs64
-rw-r--r--src/parsers/mod.rs2
3 files changed, 159 insertions, 0 deletions
diff --git a/src/parsers/csv/mod.rs b/src/parsers/csv/mod.rs
new file mode 100644
index 0000000..c906202
--- /dev/null
+++ b/src/parsers/csv/mod.rs
@@ -0,0 +1,93 @@
+use std::fs::File;
+use std::io;
+use std::io::BufRead;
+use std::collections::HashMap;
+
+/**
+ Represents a CSV file
+ **/
+
+pub struct CsvFile {
+ path : String,
+ delim : String,
+ pub header : Vec<String>,
+ pub content : Vec<Vec<String>>,
+ has_header : bool
+}
+
+/*
+ * Iterator for the CSV file
+ */
+pub struct CsvFileIterator<'a> {
+ file : &'a CsvFile,
+ line : usize,
+ column : usize,
+ max_column : usize
+}
+
+impl <'a> Iterator for CsvFileIterator<'a> {
+ type Item = HashMap<&'a String,&'a String>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let k = self.file.content.get(self.line)?;
+ let it : HashMap<&'a String, &'a String> = self.file.header.iter().zip(k.iter()).collect();
+ self.line = self.line + 1;
+ Some(it)
+ }
+}
+
+impl CsvFile {
+ pub fn new(path: &str, delim: &str, header: bool) -> CsvFile {
+ CsvFile {
+ path : String::from(path),
+ delim: String::from(delim),
+ header : Vec::new(),
+ content : Vec::new(),
+ has_header : header
+ }
+
+ }
+
+ // TODO nicht gleich viele spalten
+ fn parse_line(line : String, delim: &str) -> Vec<String> {
+ let mut columns : Vec<String> = Vec::new();
+ for column in line.split(delim).collect::<Vec<&str>>() {
+ columns.push(column.trim_matches(|c| c == '\'' || c == '\"').to_string());
+ }
+ columns
+ }
+
+
+ pub fn from_file(path: &str, delim: &str, header: bool) -> Result<CsvFile, io::Error> {
+ let fd = File::open(path)?;
+ let reader = io::BufReader::new(fd);
+ let mut column_names : Vec<String> = Vec::new();
+ let mut lines : Vec<Vec<String>> = Vec::new();
+ let mut first = true;
+
+ for line in reader.lines() {
+ let line = line?;
+ if header && first {
+ column_names.append(&mut CsvFile::parse_line(line, delim));
+ first = false;
+ } else {
+ lines.push(CsvFile::parse_line(line, delim));
+ }
+ }
+
+ Ok(CsvFile { path: String::from(path),
+ delim : String::from(delim),
+ header : column_names,
+ content : lines,
+ has_header: header } )
+ }
+
+ pub fn iter(&self) -> CsvFileIterator {
+ CsvFileIterator { file : self,
+ line : 0,
+ column : 0,
+ max_column : self.header.len()
+ }
+ }
+
+}
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 })
+ }
+
+}
diff --git a/src/parsers/mod.rs b/src/parsers/mod.rs
new file mode 100644
index 0000000..d8bce3c
--- /dev/null
+++ b/src/parsers/mod.rs
@@ -0,0 +1,2 @@
+pub mod csv;
+//pub mod ini;