Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/examples/jdat_check.rs

3.4 KiB, 1 run

created by r1870400018:67303, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Says whether each file named on the command line reads as jdat, and where it does not.
2//!
3//! ```text
4//! jdat_check <path>...
5//! jdat_check --logs DIR
6//! ```
7//!
8//! For each path one line is printed: `path: OK`, or `path: REFUSED line L col C: kind`, or
9//! `path: UNREADABLE: kind`. The text of a file is never printed, nor any part of it, and the
10//! kind is taken from the error's tags and not from its message. No key is printed, since a key
11//! may be personal data. The exit status is 0 when every file reads, 1 when one is refused or
12//! unreadable, and 2 with no paths.
13//!
14//! With `--logs DIR` the directory is an Oxegen data directory: each `.log` and `.tbl` file in it
15//! is a run of records, each a 4-byte big-endian length and the jdat text of one entry. Every
16//! record is read through the decoder and the output is one `REFUSED` line for each refusal (the
17//! file, the record, its byte offset, a tag to find it by, the line, the column and the class), a
18//! `TRUNCATED` line for a file that ends in a broken record, the counts, and the counts by class
19//! (`oxedyne_fe2o3_jdat::string::scan`). Nothing a record holds is printed. The exit status is 0
20//! when every record reads, 1 when one is refused or a file is truncated, and 2 on a usage error
21//! or a directory that holds no table file. Run it on a `cp -a` COPY of the data directory.
22//!
23//! The decoder is the one `Dat::decode_string` uses, so a file that reads here reads for any
24//! program that loads it that way. Built static, this is run on a host to learn which of its
25//! configuration files a stricter decoder would stop at, before the decoder is deployed.
26
27use oxedyne_fe2o3_jdat::string::scan::{
28 self,
29 TextScan,
30};
31
32use std::{
33 env,
34 fs,
35 path::Path,
36 process,
37};
38
39// Reads every record of every table file in a directory, and reports the refusals by class.
40fn logs(dir: &str) -> ! {
41 let mut out = TextScan::default();
42 if let Err(e) = scan::scan_dir(Path::new(dir), &mut out) {
43 eprintln!("jdat_check FAILED: {}", e);
44 process::exit(2);
45 }
46 if out.files == 0 {
47 eprintln!("No .log or .tbl file in {:?}; this is not an Oxegen data directory.", dir);
48 process::exit(2);
49 }
50 print!("{}", out.report());
51 if out.is_clean() {
52 println!("CLEAN: every record read.");
53 process::exit(0);
54 }
55 println!("NOT CLEAN: see the REFUSED and TRUNCATED lines.");
56 process::exit(1);
57}
58
59fn main() {
60 let paths = env::args().skip(1).collect::<Vec<_>>();
61 if paths.len() == 2 && paths[0] == "--logs" {
62 logs(&paths[1]);
63 }
64 if paths.len() == 0 || paths[0] == "--logs" {
65 eprintln!("Usage: jdat_check <path>... or jdat_check --logs DIR");
66 process::exit(2);
67 }
68 let mut bad = 0;
69 for path in &paths {
70 let bytes = match fs::read(path) {
71 Ok(bytes) => bytes,
72 Err(e) => {
73 println!("{}: UNREADABLE: {:?}", path, e.kind());
74 bad += 1;
75 continue;
76 },
77 };
78 let text = match String::from_utf8(bytes) {
79 Ok(text) => text,
80 Err(_) => {
81 println!("{}: UNREADABLE: NotUtf8", path);
82 bad += 1;
83 continue;
84 },
85 };
86 let (read, line) = scan::check_line(path, &text);
87 println!("{}", line);
88 if !read {
89 bad += 1;
90 }
91 }
92 process::exit(if bad == 0 { 0 } else { 1 });
93}