Port radio_print_config

This commit is contained in:
Greg Shuflin 2021-03-01 21:14:53 -08:00
parent 00570e0e91
commit f7aa60df21
3 changed files with 26 additions and 35 deletions

22
radio.c
View File

@ -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.
//

View File

@ -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);

View File

@ -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);
}
}