1 /* PSPP - a program for statistical analysis.
2 * Copyright (C) 2023 Free Software Foundation, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 use clap::{Parser, ValueEnum};
19 use encoding_rs::Encoding;
20 use pspp::cooked::decode;
21 use pspp::raw::{Reader, Record};
23 use std::io::BufReader;
24 use std::path::{Path, PathBuf};
26 use thiserror::Error as ThisError;
28 /// A utility to dissect SPSS system files.
29 #[derive(Parser, Debug)]
30 #[command(author, version, about, long_about = None)]
32 /// Maximum number of cases to print.
33 #[arg(long = "data", default_value_t = 0)]
37 #[arg(required = true)]
40 /// How to dissect the file.
41 #[arg(short, long, value_enum, default_value_t)]
44 /// The encoding to use.
45 #[arg(long, value_parser = parse_encoding)]
46 encoding: Option<&'static Encoding>,
49 #[derive(ThisError, Debug)]
50 #[error("{0}: unknown encoding")]
51 struct UnknownEncodingError(String);
53 fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> {
54 match Encoding::for_label_no_replacement(arg.as_bytes()) {
55 Some(encoding) => Ok(encoding),
56 None => Err(UnknownEncodingError(arg.to_string())),
60 #[derive(Clone, Copy, Debug, Default, ValueEnum)]
67 fn main() -> Result<()> {
76 dissect(&file, max_cases, mode, encoding)?;
81 fn dissect(file_name: &Path, max_cases: u64, mode: Mode, encoding: Option<&'static Encoding>) -> Result<()> {
82 let reader = File::open(file_name)?;
83 let reader = BufReader::new(reader);
84 let mut reader = Reader::new(reader)?;
86 let headers: Vec<Record> = reader.collect_headers()?;
89 for header in headers {
90 println!("{header:?}");
94 let headers = decode(headers, encoding, &|e| panic!("{e}"))?;
95 for header in headers {
96 println!("{header:?}");
101 for _ in 0..max_cases {
102 let Some(Ok(Record::Case(data))) = reader.next() else {
105 println!("{:?}", data);