From f7aa60df212b8ed1e26eea851a19a87cc84c5826 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 1 Mar 2021 21:14:53 -0800 Subject: [PATCH] Port radio_print_config --- radio.c | 22 ---------------------- src/lib.rs | 6 +++++- src/radio.rs | 33 +++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/radio.c b/radio.c index e5b7e00..4a1cb02 100644 --- a/radio.c +++ b/radio.c @@ -154,28 +154,6 @@ badline: fprintf(stderr, "Invalid line: '%s'\n", line); device->update_timestamp(device); } -// -// Print full information about the device configuration. -// -void radio_print_config(radio_device_t* device, FILE *out, int verbose) -{ - if (verbose) { - char buf [40]; - time_t t; - struct tm *tmp; - - t = time(NULL); - tmp = localtime(&t); - if (! tmp || ! strftime(buf, sizeof(buf), "%Y/%m/%d ", tmp)) - buf[0] = 0; - fprintf(out, "#\n"); - fprintf(out, "# Configuration generated %sby dmrconfig, version %s\n", - buf, version); - fprintf(out, "#\n"); - } - device->print_config(device, out, verbose); -} - // // Update contacts database on the device. // diff --git a/src/lib.rs b/src/lib.rs index 704528f..a59e139 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,9 +12,13 @@ mod radio; const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ"; const VERSION: Option<&'static str> = option_env!("VERSION"); +fn version() -> &'static str { + VERSION.unwrap_or("-------") +} + fn print_usage() { - let version = VERSION.unwrap_or("-----"); + let version = version(); print!("DMR Config, Version {}, {}", version, COPYRIGHT); diff --git a/src/radio.rs b/src/radio.rs index 537d287..29eaf36 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -36,7 +36,6 @@ extern { fn get_active_device() -> *const radio_device_t; fn set_active_device(device: *const radio_device_t); - fn radio_print_config(device: *const radio_device_t, file: *const libc::FILE, verbose: c_int); fn radio_parse_config(device: *const radio_device_t, filename: *const c_char); fn radio_write_csv(device: *const radio_device_t, filename: *const c_char); @@ -313,31 +312,41 @@ pub fn parse_config(radio: &Radio, filename: &str) { /// If `filename` is `None`, write to stdout. pub fn print_config(radio: &Radio, filename: Option<&str>) { - let file; + use std::io::Write; - let (fd, verbosity) = match filename { + fn make_config_string() -> String { + use chrono::Datelike; + let date = chrono::offset::Local::today(); + let y = date.year(); let m = date.month(); let d = date.day(); + format!("#\n# Configuration generated {}/{}/{} by dmrconfig, version {}\n#\n", y, m, d, crate::version()) + } + + let mut file; + + let (fd, verbose) = match filename { None => { unsafe { - let verbosity = if libc::isatty(libc::STDOUT_FILENO) == 1 { - 0 - } else { - 1 - }; - (libc::STDOUT_FILENO, verbosity) + let verbose = libc::isatty(libc::STDOUT_FILENO) != 1; + if verbose { + print!("{}", make_config_string()); + } + (libc::STDOUT_FILENO, verbose) } }, Some(filename) => { file = std::fs::File::create(filename).unwrap(); - (file.as_raw_fd(), 1) + file.write_all(make_config_string().as_bytes()).unwrap(); + (file.as_raw_fd(), true) } }; - let device = radio.ptr; let mode = CString::new("w").unwrap(); unsafe { + let device = radio.ptr as *mut radio_device_t; + let print_config_fn = (*device).print_config.unwrap(); let file = libc::fdopen(fd, mode.as_ptr()); - radio_print_config(device, file, verbosity); + print_config_fn(device, file, if verbose { 1 } else { 0 } ); libc::fclose(file); } }