Skip to content

Instantly share code, notes, and snippets.

@cvaize
Created December 14, 2025 13:57
Show Gist options
  • Select an option

  • Save cvaize/58646c0568019b3c055ee258d0499bff to your computer and use it in GitHub Desktop.

Select an option

Save cvaize/58646c0568019b3c055ee258d0499bff to your computer and use it in GitHub Desktop.
Fix music cue files
#![feature(trim_prefix_suffix)]
use std::borrow::Cow;
use walkdir::WalkDir;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use chardetng::EncodingDetector;
use encoding_rs::WINDOWS_1251;
use encoding_rs::WINDOWS_1252;
fn main() {
let root = std::env::args().nth(1).unwrap_or(String::from("."));
let ext = std::env::args().nth(2).unwrap_or(String::from("cue"));
let ext = ext.as_str();
let ext_suffix = format!(".{}", ext);
let ext_suffix = ext_suffix.as_str();
let mod_ext = format!(".utf8.{}", ext);
let mod_ext = mod_ext.as_str();
for entry in WalkDir::new(root)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
{
let path = entry.path();
if path.extension()
.and_then(|s| s.to_str())
== Some(ext)
{
let filename = path.file_name().unwrap().to_str().unwrap();
if filename.ends_with(mod_ext) {
continue;
}
let new_filename = format!("{}{}", filename.trim_suffix(ext_suffix), mod_ext);
let new_path = Path::new(path.parent().unwrap()).join(new_filename);
if new_path.exists() {
continue;
}
let mut file = File::open(path).unwrap();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
let mut detector = EncodingDetector::new();
detector.feed(&buf, true);
let encoding = detector.guess(None, true);
if encoding.name() == "windows-1251" {
let (text, _, _) = WINDOWS_1251.decode(&buf);
write_file(text, &new_path);
} else if encoding.name() == "windows-1252" {
let (text, _, _) = WINDOWS_1252.decode(&buf);
write_file(text, &new_path);
} else if encoding.name() == "UTF-8" {
// Ignore
} else {
println!("Ignore: {}; Filepath: {}", encoding.name(), path.display());
}
}
}
}
fn write_file(text: Cow<str>, new_path: &Path) {
let mut file = File::create(new_path).unwrap();
file.write_all(text.as_bytes()).unwrap();
// println!("Done: {}", new_path.display());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment