* along with this program. If not, see <http://www.gnu.org/licenses/>. */
use anyhow::Result;
-use clap::Parser;
-use pspp::{raw::{Reader, Record}, locale_charset::locale_charset};
+use clap::{Parser, ValueEnum};
+use encoding_rs::Encoding;
+use pspp::cooked::decode;
+use pspp::raw::{Reader, Record};
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::str;
+use thiserror::Error as ThisError;
/// A utility to dissect SPSS system files.
#[derive(Parser, Debug)]
/// Files to dissect.
#[arg(required = true)]
files: Vec<PathBuf>,
+
+ /// How to dissect the file.
+ #[arg(short, long, value_enum, default_value_t)]
+ mode: Mode,
+
+ /// The encoding to use.
+ #[arg(long, value_parser = parse_encoding)]
+ encoding: Option<&'static Encoding>,
+}
+
+#[derive(ThisError, Debug)]
+#[error("{0}: unknown encoding")]
+struct UnknownEncodingError(String);
+
+fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> {
+ match Encoding::for_label_no_replacement(arg.as_bytes()) {
+ Some(encoding) => Ok(encoding),
+ None => Err(UnknownEncodingError(arg.to_string())),
+ }
+}
+
+#[derive(Clone, Copy, Debug, Default, ValueEnum)]
+enum Mode {
+ Raw,
+ #[default]
+ Cooked,
}
fn main() -> Result<()> {
- println!("locale_charset={}", locale_charset());
- let Args { max_cases, files } = Args::parse();
+ let Args {
+ max_cases,
+ files,
+ mode,
+ encoding,
+ } = Args::parse();
for file in files {
- dissect(&file, max_cases)?;
+ dissect(&file, max_cases, mode, encoding)?;
}
Ok(())
}
-fn dissect(file_name: &Path, max_cases: u64) -> Result<()> {
+fn dissect(file_name: &Path, max_cases: u64, mode: Mode, encoding: Option<&'static Encoding>) -> Result<()> {
let reader = File::open(file_name)?;
let reader = BufReader::new(reader);
let mut reader = Reader::new(reader)?;
- let records: Vec<Record> = reader.collect_headers()?;
- for record in records {
- println!("{record:?}");
- if let Record::EndOfHeaders(_) = record {
- break;
- };
+ let headers: Vec<Record> = reader.collect_headers()?;
+ match mode {
+ Mode::Raw => {
+ for header in headers {
+ println!("{header:?}");
+ }
+ }
+ Mode::Cooked => {
+ let headers = decode(headers, encoding, &|e| panic!("{e}"))?;
+ for header in headers {
+ println!("{header:?}");
+ }
+ }
}
for _ in 0..max_cases {