Skip to content

Commit 24d87db

Browse files
committed
Add 'mode' main configuration entry to set file open mode
Valid options are 'truncate' (the default) and 'append'.
1 parent 6b28be1 commit 24d87db

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/main.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ extern crate quick_xml;
22
extern crate yaml_rust;
33

44
use std::io::{Read, Write, stdout};
5-
use std::fs::File;
5+
use std::fs::{File, OpenOptions};
66
use std::path::Path;
77
use std::env;
88
use std::cell::RefCell;
@@ -18,12 +18,18 @@ struct Table<'a> {
1818
columns: Vec<Column<'a>>
1919
}
2020
impl<'a> Table<'a> {
21-
fn new(path: &str, file: Option<&str>) -> Table<'a> {
21+
fn new(path: &str, file: Option<&str>, filemode: &str) -> Table<'a> {
2222
Table {
2323
path: String::from(path),
2424
file: match file {
2525
None => RefCell::new(Box::new(stdout())),
26-
Some(ref file) => RefCell::new(Box::new(File::create(&Path::new(file)).unwrap()))
26+
Some(ref file) => RefCell::new(Box::new(
27+
match filemode {
28+
"truncate" => File::create(&Path::new(file)).unwrap(),
29+
"append" => OpenOptions::new().append(true).create(true).open(&Path::new(file)).unwrap(),
30+
mode => panic!("Invalid 'mode' setting in configuration file: {}", mode)
31+
}
32+
))
2733
},
2834
columns: Vec::new()
2935
}
@@ -97,8 +103,8 @@ fn gml_to_ewkb(cell: &RefCell<String>, geom: &Geometry) {
97103
}
98104
}
99105

100-
fn add_table<'a>(rowpath: &str, outfile: Option<&str>, colspec: &'a [Yaml]) -> Table<'a> {
101-
let mut table = Table::new(rowpath, outfile);
106+
fn add_table<'a>(rowpath: &str, outfile: Option<&str>, filemode: &str, colspec: &'a [Yaml]) -> Table<'a> {
107+
let mut table = Table::new(rowpath, outfile, filemode);
102108
for col in colspec {
103109
let name = col["name"].as_str().expect("Column has no 'name' entry in configuration file");
104110
let colpath = col["path"].as_str().expect("Column has no 'path' entry in configuration file");
@@ -108,7 +114,7 @@ fn add_table<'a>(rowpath: &str, outfile: Option<&str>, colspec: &'a [Yaml]) -> T
108114
true => None,
109115
false => {
110116
let file = col["file"].as_str().expect("Subtable has no 'file' entry");
111-
Some(add_table(&path, Some(&file), col["cols"].as_vec().expect("Subtable 'cols' entry is not an array")))
117+
Some(add_table(&path, Some(&file), filemode, col["cols"].as_vec().expect("Subtable 'cols' entry is not an array")))
112118
}
113119
};
114120
let attr = col["attr"].as_str();
@@ -147,7 +153,11 @@ fn main() -> std::io::Result<()> {
147153
let rowpath = config["path"].as_str().expect("No valid 'path' entry in configuration file");
148154
let colspec = config["cols"].as_vec().expect("No valid 'cols' array in configuration file");
149155
let outfile = config["file"].as_str();
150-
let maintable = add_table(rowpath, outfile, colspec);
156+
let filemode = match config["mode"].is_badvalue() {
157+
true => "truncate",
158+
false => config["mode"].as_str().expect("Invalid 'mode' entry in configuration file")
159+
};
160+
let maintable = add_table(rowpath, outfile, filemode, colspec);
151161
let mut tables: Vec<&Table> = Vec::new();
152162
let mut table = &maintable;
153163

0 commit comments

Comments
 (0)