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::raw::{Magic, Reader, Record};
22 use std::io::BufReader;
23 use std::path::{Path, PathBuf};
25 use thiserror::Error as ThisError;
27 /// A utility to dissect SPSS system files.
28 #[derive(Parser, Debug)]
29 #[command(author, version, about, long_about = None)]
31 /// Maximum number of cases to print.
32 #[arg(long = "data", default_value_t = 0)]
36 #[arg(required = true)]
39 /// How to dissect the file.
40 #[arg(short, long, value_enum, default_value_t)]
43 /// The encoding to use.
44 #[arg(long, value_parser = parse_encoding)]
45 encoding: Option<&'static Encoding>,
48 #[derive(ThisError, Debug)]
49 #[error("{0}: unknown encoding")]
50 struct UnknownEncodingError(String);
52 fn parse_encoding(arg: &str) -> Result<&'static Encoding, UnknownEncodingError> {
53 match Encoding::for_label_no_replacement(arg.as_bytes()) {
54 Some(encoding) => Ok(encoding),
55 None => Err(UnknownEncodingError(arg.to_string())),
59 #[derive(Clone, Copy, Debug, Default, ValueEnum)]
67 fn main() -> Result<()> {
76 dissect(&file, max_cases, mode, encoding)?;
85 encoding: Option<&'static Encoding>,
87 let reader = File::open(file_name)?;
88 let reader = BufReader::new(reader);
89 let mut reader = Reader::new(reader, |warning| println!("{warning}"))?;
93 let Record::Header(header) = reader.next().unwrap()? else {
97 Magic::Sav => println!("SPSS System File"),
98 Magic::Zsav => println!("SPSS System File with Zlib compression"),
99 Magic::Ebcdic => println!("EBCDIC-encoded SPSS System File"),
104 for header in reader {
105 let header = header?;
106 println!("{:?}", header);
107 if let Record::Cases(cases) = header {
108 let mut cases = cases.borrow_mut();
109 for _ in 0..max_cases {
110 let Some(Ok(record)) = cases.next() else {
113 println!("{:?}", record);
120 let headers: Vec<Record> = reader.collect::<Result<Vec<_>, _>>()?;
125 let headers: Vec<Record> = reader.collect::<Result<Vec<_>, _>>()?;
126 let encoding = encoding_from_headers(&headers, &|e| eprintln!("{e}"))?;
127 let (headers, _) = decode(headers, encoding, &|e| eprintln!("{e}"))?;
128 for header in headers {
129 println!("{header:?}");