CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
iso_fs.h
Go to the documentation of this file.
1/*
2Copyright (c) 2010 Peter "Corsix" Cawley
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to
7use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8of the Software, and to permit persons to whom the Software is furnished to do
9so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#ifndef CORSIX_TH_ISO_FS_H_
24#define CORSIX_TH_ISO_FS_H_
25
26#include "config.h"
27
28#include <cstdio>
29#include <memory>
30#include <string>
31#include <string_view>
32#include <vector>
33
35
44 public:
47 static constexpr size_t min_sector_size = 2048;
48
50
56 explicit iso_filesystem(const char* path, char pathSeparator = '/');
57 ~iso_filesystem() = default;
58
60
63 std::string_view get_error() const;
64
65 using file_handle = int;
66
68
72 file_handle find_file(const char* sPath) const;
73
75
82 void visit_directory_files(const char* sPath,
83 void (*fnCallback)(void*, const char*,
84 const char*),
85 void* pCallbackData) const;
86
88 static inline bool is_handle_good(file_handle x) { return x != 0; }
89
91
94 uint32_t get_file_start(file_handle iFile) const;
95
97
100 uint32_t get_file_size(file_handle iFile) const;
101
103
108 bool get_file_data(file_handle iFile, uint8_t* pBuffer);
109
110 private:
111 struct file_metadata {
112 std::string path;
113 uint32_t sector;
114 uint32_t size;
115 };
116
117 std::unique_ptr<std::FILE, int (*)(std::FILE*)> raw_file;
118 std::string error{};
119 std::vector<file_metadata> files;
120 long sector_size{min_sector_size};
121 char path_seperator;
122
124 void set_error(const char* sFormat, ...);
125
127 bool seek_to_sector(uint32_t iSector);
128
130 bool read_data(uint32_t iByteCount, uint8_t* pBuffer);
131
133
142 int find_hosp_directory(const uint8_t* pDirEnt, const uint32_t dirEntsSize,
143 int level);
144
146
152 void build_file_lookup_table(uint32_t iSector, uint32_t dirEntsSize,
153 std::string_view prefix);
154
156 static bool file_metadata_less(const file_metadata& lhs,
157 const file_metadata& rhs);
158};
159
160#endif
Layer for reading Theme Hospital files out of an .iso disk image.
Definition iso_fs.h:43
~iso_filesystem()=default
static constexpr size_t min_sector_size
Definition iso_fs.h:47
int file_handle
Definition iso_fs.h:65
bool get_file_data(file_handle iFile, uint8_t *pBuffer)
Get the contents of a file in the loaded .iso disk image.
Definition iso_fs.cpp:552
static bool is_handle_good(file_handle x)
Test if a file handle from find_file() is good or is invalid.
Definition iso_fs.h:88
uint32_t get_file_size(file_handle iFile) const
Get the size (in bytes) of a file in the loaded .iso disk image.
Definition iso_fs.cpp:545
void visit_directory_files(const char *sPath, void(*fnCallback)(void *, const char *, const char *), void *pCallbackData) const
Iterate all files of the .iso disk image within a given directory.
Definition iso_fs.cpp:494
file_handle find_file(const char *sPath) const
Find a file in the loaded .iso disk image.
Definition iso_fs.cpp:518
uint32_t get_file_start(file_handle iFile) const
Get the byte offset of the start of the file in the loaded .iso.
Definition iso_fs.cpp:538
std::string_view get_error() const
Get the reason for the most recent failure.
Definition iso_fs.cpp:562