diff --git a/Cargo.lock b/Cargo.lock index 53a77ce..fb42caa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,27 +167,6 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" -[[package]] -name = "csv" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac574ff4d437a7b5ad237ef331c17ccca63c46479e5b5453eb8e10bb99a759fe" -dependencies = [ - "csv-core", - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "csv-core" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70" -dependencies = [ - "memchr", -] - [[package]] name = "env_filter" version = "0.1.0" @@ -430,7 +409,6 @@ version = "0.10.0" dependencies = [ "chrono", "clap", - "csv", "env_logger", "hostname", "libc", diff --git a/Cargo.toml b/Cargo.toml index 758d65d..40e0334 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ subprocess = "0.2" chrono = "0.4" hostname = "0.3" clap = { version = "4.5", features = ["derive"] } -csv = "1.3" log = "0.4" env_logger = "0.11" page_size = "0.6" diff --git a/src/ps.rs b/src/ps.rs index 3d8cf4b..88ac250 100644 --- a/src/ps.rs +++ b/src/ps.rs @@ -8,9 +8,8 @@ use crate::jobs; use crate::nvidia; use crate::procfs; use crate::procfsapi; -use crate::util::three_places; +use crate::util::{csv_quote,three_places}; -use csv::{Writer, WriterBuilder}; use std::collections::{HashMap, HashSet}; use std::env; use std::io::{self, Result, Write}; @@ -424,9 +423,7 @@ fn do_create_snapshot( // Once we start printing we'll print everything and not check the interrupted flag any more. - let mut writer = WriterBuilder::new() - .flexible(true) - .from_writer(io::stdout()); + let mut writer = io::stdout(); let hostname = hostname::get().unwrap().into_string().unwrap(); const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -609,8 +606,8 @@ struct PrintParameters<'a> { opts: &'a PsOptions<'a>, } -fn print_record( - writer: &mut Writer, +fn print_record( + writer: &mut dyn io::Write, params: &PrintParameters, proc_info: &ProcInfo, ) -> Result { @@ -679,7 +676,17 @@ fn print_record( if proc_info.rolledup > 0 { fields.push(format!("rolledup={}", proc_info.rolledup)); } - writer.write_record(fields)?; + + let mut s = "".to_string(); + for f in fields { + if !s.is_empty() { + s += "," + } + s += &csv_quote(&f); + } + s += "\n"; + + writer.write(s.as_bytes())?; Ok(true) } diff --git a/src/util.rs b/src/util.rs index e07e5b5..c008c96 100644 --- a/src/util.rs +++ b/src/util.rs @@ -71,3 +71,29 @@ pub fn chunks(input: &str) -> (Vec, Vec<&str>) { pub fn three_places(n: f64) -> f64 { (n * 1000.0).round() / 1000.0 } + +// If the value contains a , or " then quote the string, and double every " +pub fn csv_quote(s: &str) -> String { + let mut t = "".to_string(); + let mut must_quote = false; + for c in s.chars() { + match c { + '"' => { + t.push(c); + t.push(c); + must_quote = true; + } + ',' => { + t.push(c); + must_quote = true; + } + _ => { + t.push(c); + } + } + } + if must_quote { + t = "\"".to_string() + &t + "\"" + } + t +}