Compare commits
2 commits
45f99ed4f6
...
dd433cfa4e
Author | SHA1 | Date | |
---|---|---|---|
lemon-sh | dd433cfa4e | ||
lemon-sh | 240f21e313 |
33
src/main.rs
33
src/main.rs
|
@ -58,6 +58,8 @@ impl From<EncodingError> for CifError {
|
|||
|
||||
type Result<T> = std::result::Result<T, CifError>;
|
||||
|
||||
// we should decide whether these two should be inline or not
|
||||
|
||||
#[inline]
|
||||
fn join_to_arraystring<const C: usize>(elems: &[&str], str: &mut ArrayString<C>) -> Result<()> {
|
||||
for (i, item) in elems.iter().enumerate() {
|
||||
|
@ -69,6 +71,7 @@ fn join_to_arraystring<const C: usize>(elems: &[&str], str: &mut ArrayString<C>)
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn lines_read_noempty<T: BufRead>(lines: &mut Lines<T>) -> Result<Option<String>> {
|
||||
while let Some(s) = lines.next().transpose()? {
|
||||
if !s.is_empty() {
|
||||
|
@ -84,7 +87,7 @@ fn unfuck_cif_number(number: &str) -> Result<u32> {
|
|||
if number.is_empty() || number.as_bytes()[0] == 32 || number.as_bytes()[number.len()-1] == 32 {
|
||||
return Err(GrammarError(number.to_string()));
|
||||
}
|
||||
let split = number.split_whitespace().collect::<ArrayVec<_, 64>>();
|
||||
let split = number.split_ascii_whitespace().collect::<ArrayVec<_, 64>>();
|
||||
let splitlen = split.len();
|
||||
for (i, word) in split.iter().enumerate() {
|
||||
if !word.starts_with("ty") {
|
||||
|
@ -120,9 +123,11 @@ fn unfuck_cif_number(number: &str) -> Result<u32> {
|
|||
Ok(u32::from(*nummap::NUMMAP.get(number).ok_or_else(|| GrammarError(number.to_string()))?))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unfuck_cif_number_fast(number: &str) -> Result<u8> {
|
||||
let number = *(nummap::NUMMAP.get(number).ok_or_else(|| GrammarError(number.to_string()))?);
|
||||
let mut buf = ArrayString::<2048>::new();
|
||||
let split = number.split_ascii_whitespace().take(8).collect::<ArrayVec<_, 8>>();
|
||||
join_to_arraystring(&split, &mut buf)?;
|
||||
let number = *(nummap::NUMMAP.get(&buf).ok_or_else(|| GrammarError(number.to_string()))?);
|
||||
if number < 256 {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
Ok(number as u8)
|
||||
|
@ -141,11 +146,13 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
|
|||
return Err(InvalidHeaderError)
|
||||
}
|
||||
|
||||
if lines.next().transpose()?.ok_or(InvalidHeaderError)? != "WERSJA jeden" {
|
||||
let version_str = lines_read_noempty(&mut lines)?.ok_or(InvalidHeaderError)?;
|
||||
let version_split = version_str.split_ascii_whitespace().take(2).collect::<ArrayVec<_, 2>>();
|
||||
if version_split.len() != 2 || version_split[0] != "WERSJA" || version_split[1] != "jeden" {
|
||||
return Err(InvalidHeaderError)
|
||||
}
|
||||
|
||||
let size_string = lines.next().transpose()?.ok_or(InvalidHeaderError)?;
|
||||
let size_string = lines_read_noempty(&mut lines)?.ok_or(InvalidHeaderError)?;
|
||||
if size_string.len() < 8 || !size_string.starts_with("ROZMIAR") {
|
||||
return Err(InvalidSizeError(size_string))
|
||||
}
|
||||
|
@ -177,13 +184,17 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
|
|||
outpng.set_depth(BitDepth::Eight);
|
||||
|
||||
let first = loop {
|
||||
if let Some(line) = lines.next() {
|
||||
let line = line?;
|
||||
if let Some(line) = lines_read_noempty(&mut lines)? {
|
||||
// TODO: THIS IS A BAD IDEA TO PARSE METADATA, CHANGE THIS ASAP
|
||||
if line.starts_with("METADANE ") {
|
||||
let md_split = line.splitn(3, ' ').collect::<ArrayVec<_, 3>>();
|
||||
let mut buf = ArrayString::<2048>::new();
|
||||
let split = line.split_ascii_whitespace().collect::<ArrayVec<_, 256>>();
|
||||
join_to_arraystring(&split, &mut buf)?;
|
||||
let md_split = buf.splitn(3, ' ').collect::<ArrayVec<_, 3>>();
|
||||
if md_split.len() != 3 {
|
||||
return Err(InvalidMetadataError(line.to_string()))
|
||||
}
|
||||
println!("{:?}", md_split);
|
||||
outpng.add_itxt_chunk(md_split[1].to_string(), md_split[2].to_string())?;
|
||||
continue
|
||||
}
|
||||
|
@ -207,11 +218,7 @@ fn decoder(inputfile: &str, outputfile: &str) -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
for line in lines {
|
||||
let line = line?;
|
||||
if line.is_empty() {
|
||||
continue
|
||||
}
|
||||
while let Some(line) = lines_read_noempty(&mut lines)? {
|
||||
let px_split = line.split(';').collect::<ArrayVec<_, 4>>();
|
||||
if px_split.len() != bytes_per_pixel as usize {
|
||||
return Err(InvalidPixelError(line.to_string()))
|
||||
|
|
Loading…
Reference in a new issue