| 148 | constexpr std::ios::off_type kMpqHashEntryOffset = kMpqBlockEntryOffset + kBlockEntrySize; |
| 149 | |
| 150 | struct Archive { |
| 151 | FStreamWrapper stream; |
| 152 | std::string name; |
| 153 | std::uintmax_t size; |
| 154 | bool modified; |
| 155 | bool exists; |
| 156 | |
| 157 | #ifndef CAN_SEEKP_BEYOND_EOF |
| 158 | std::streampos stream_begin; |
| 159 | #endif |
| 160 | |
| 161 | _HASHENTRY *sgpHashTbl; |
| 162 | _BLOCKENTRY *sgpBlockTbl; |
| 163 | |
| 164 | bool Open(const char *name) |
| 165 | { |
| 166 | Close(); |
| 167 | #ifdef _DEBUG |
| 168 | SDL_Log("Opening %s", name); |
| 169 | #endif |
| 170 | exists = FileExists(name); |
| 171 | std::ios::openmode mode = std::ios::in | std::ios::out | std::ios::binary; |
| 172 | if (exists) { |
| 173 | if (GetFileSize(name, &size) == 0) { |
| 174 | SDL_Log("GetFileSize(\"%s\") failed with \"%s\"", name, std::strerror(errno)); |
| 175 | return false; |
| 176 | #ifdef _DEBUG |
| 177 | } else { |
| 178 | SDL_Log("GetFileSize(\"%s\") = %" PRIuMAX, name, size); |
| 179 | #endif |
| 180 | } |
| 181 | } else { |
| 182 | mode |= std::ios::trunc; |
| 183 | } |
| 184 | if (!stream.Open(name, mode)) { |
| 185 | stream.Close(); |
| 186 | return false; |
| 187 | } |
| 188 | modified = !exists; |
| 189 | |
| 190 | this->name = name; |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool Close(bool clear_tables = true) |
| 195 | { |
| 196 | if (!stream.IsOpen()) |
| 197 | return true; |
| 198 | #ifdef _DEBUG |
| 199 | SDL_Log("Closing %s", name.c_str()); |
| 200 | #endif |
| 201 | |
| 202 | bool result = true; |
| 203 | if (modified && !(stream.seekp(0, std::ios::beg) && WriteHeaderAndTables())) |
| 204 | result = false; |
| 205 | stream.Close(); |
| 206 | if (modified && result && size != 0) { |
| 207 | #ifdef _DEBUG |
nothing calls this directly
no outgoing calls
no test coverage detected