chessbaseLinux/crates/chess-import/examples/convert.rs
vrubel bb7d3b3fa6
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
Open ChessBase databases (.cbh, .cbv) and zip/7z archives
New crate chess-import converts non-PGN sources to PGN in memory and returns
a PgnDatabase, so the library, notation and analysis work unchanged:
- .cbv archives (container rewritten after uncbv), .cbh with companion files
  (layout after the public Morphy format description): games, variations,
  comments with per-database code page detection, NAGs, squares/arrows, set-up
  positions, time spent, book chapters (guiding texts) as move-less records;
- ChessBase figurines become piece letters, diagram/object markers handled,
  Western names in Cyrillic databases read correctly;
- .zip/.7z with PGN (own encoding each), .cbh and .cbv; duplicates skipped;
- .cbz/encrypted archives and 2CBH rejected with clear messages.
Library: '+ Add database', format shown, import notes; comments keep
paragraph breaks. Tests on the user's sample books (skipped when absent).

Also: AUR PKGBUILD for 0.2.0 with the bundled Stockfish.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 13:18:27 +03:00

32 lines
1 KiB
Rust

//! Converts a database to PGN on stdout; notes go to stderr.
//!
//! cargo run -p chess-import --example convert -- "Some Book.cbv" > book.pgn
use std::io::Write;
use std::sync::atomic::AtomicBool;
fn main() {
let Some(path) = std::env::args_os().nth(1) else {
eprintln!("usage: convert <file.pgn|.cbh|.cbv|.zip|.7z>");
std::process::exit(2);
};
let never = AtomicBool::new(false);
match chess_import::open(std::path::Path::new(&path), &mut |_| {}, &never) {
Ok(o) => {
eprintln!("{}: {} games", o.format.label(), o.db.len());
for n in &o.notes {
eprintln!("note: {n}");
}
let mut out = std::io::stdout().lock();
for i in 0..o.db.len() {
if let Some(text) = o.db.raw_text(i) {
let _ = writeln!(out, "{}\n", text.trim_end());
}
}
}
Err(e) => {
eprintln!("error: {e}");
std::process::exit(1);
}
}
}