forked from sans-py/sfnt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7249b3f
commit 16ac436
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,44 @@ | ||
use crate::types::FileType; | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
pub fn run() -> Result<(), String> { | ||
let Some(second_arg) = std::env::args().nth(1) else { | ||
return Err(format!("Usage: {} [string]", file!())) | ||
}; | ||
let Ok(mut file_handle) = File::open(&second_arg) else { | ||
return Err(format!("Error opening file: {}", second_arg)) | ||
}; | ||
|
||
let Some(file_type) = guess_file_type(&mut file_handle) else { | ||
return Err(format!("Error guessing file type: {}", second_arg)) | ||
}; | ||
println!("File type: {}", file_type); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn guess_file_type(file: &mut File) -> Option<FileType> { | ||
let mut bytes = [0u8; 256]; | ||
let Ok(_) = file.read_exact(&mut bytes) else { | ||
return None; | ||
}; | ||
// Windows Bom Bytes 라고 해서, \xef\xbb\xbf 가 붙어 있는 경우가 있음. 대응해야 함. | ||
let mut start_ind = 0; | ||
let bom_bytes = b"\xef\xbb\xbf"; | ||
if bytes.starts_with(bom_bytes) { | ||
start_ind = 3; | ||
} | ||
|
||
let head = &bytes[start_ind..(start_ind 4)]; | ||
match head { | ||
b"OTTO" => Some(FileType::OTF), | ||
b"ttcf" => Some(FileType::TTF), | ||
b"wOFF" => Some(FileType::WOFF), | ||
b"wOF2" => Some(FileType::WOFF2), | ||
b"<?xm" => { | ||
unimplemented!() | ||
} | ||
_ => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 1,8 @@ | ||
mod cli; | ||
mod types; | ||
fn main() { | ||
println!("Hello, world!"); | ||
match cli::run() { | ||
Ok(_) => (), | ||
Err(e) => println!("{}", e), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,23 @@ | ||
pub enum FileType { | ||
OTF, | ||
TTC, | ||
TTF, | ||
WOFF, | ||
WOFF2, | ||
OTX, | ||
TTX, | ||
} | ||
|
||
impl std::fmt::Display for FileType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
FileType::OTF => write!(f, "otf"), | ||
FileType::TTC => write!(f, "ttc"), | ||
FileType::TTF => write!(f, "ttf"), | ||
FileType::WOFF => write!(f, "woff"), | ||
FileType::WOFF2 => write!(f, "woff2"), | ||
FileType::OTX => write!(f, "otx"), | ||
FileType::TTX => write!(f, "ttx"), | ||
} | ||
} | ||
} |