Debian Codegraph @

terps/bocfel/iff.cpp

raw symbols SCIP
1 // Copyright 2010-2023 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include <algorithm>
6 #include <utility>
7
8 #include "iff.h"
9 #include "io.h"
10 #include "types.h"
11
12 IFF::IFF(std::shared_ptr<IO> io, TypeID type) : m_io(std::move(io))
13 {
14 try {
15 m_io->seek(0, IO::SeekFrom::Start);
16 uint32_t type_val = m_io->read32();
17 if (type_val != TypeID("FORM").val()) {
18 throw InvalidFile();
19 }
20
21 m_io->seek(4, IO::SeekFrom::Current);
22 type_val = m_io->read32();
23 if (type_val != type.val()) {
24 throw InvalidFile();
25 }
26
27 while (true) {
28 try {
29 type_val = m_io->read32();
30 } catch (const IO::IOError &) {
31 // IOError here will mean EOF, which is not an error; it
32 // just means to stop parsing the file.
33 break;
34 }
35
36 auto size = m_io->read32();
37 auto offset = m_io->tell();
38
39 m_entries.emplace_back(TypeID(type_val), offset, size);
40
41 if ((size & 1) == 1) {
42 size++;
43 }
44
45 m_io->seek(size, IO::SeekFrom::Current);
46 }
47 } catch (const IO::IOError &) {
48 throw InvalidFile();
49 }
50 }
51
52 bool IFF::find(TypeID type_id, uint32_t &size)
53 {
54 auto it = std::find_if(m_entries.begin(), m_entries.end(), [&](const Entry &entry){ return entry.type == type_id; });
55 if (it == m_entries.end()) {
56 return false;
57 } else {
58 try {
59 m_io->seek(it->offset, IO::SeekFrom::Start);
60 } catch (const IO::IOError &) {
61 return false;
62 }
63 size = it->size;
64 return true;
65 }
66 }