| 201 | } |
| 202 | |
| 203 | bool Epub::parseTocNavFile() const { |
| 204 | // the nav file should have been specified in the content.opf file (EPUB 3) |
| 205 | if (tocNavItem.empty()) { |
| 206 | LOG_DBG("EBP", "No nav file specified"); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | LOG_DBG("EBP", "Parsing toc nav file: %s", tocNavItem.c_str()); |
| 211 | |
| 212 | const auto tmpNavPath = getCachePath() + "/toc.nav"; |
| 213 | HalFile tempNavFile; |
| 214 | if (!Storage.openFileForWrite("EBP", tmpNavPath, tempNavFile)) { |
| 215 | return false; |
| 216 | } |
| 217 | readItemContentsToStream(tocNavItem, tempNavFile, 1024); |
| 218 | // Explicitly close() file before reopening for reading |
| 219 | tempNavFile.close(); |
| 220 | if (!Storage.openFileForRead("EBP", tmpNavPath, tempNavFile)) { |
| 221 | return false; |
| 222 | } |
| 223 | const auto navSize = tempNavFile.size(); |
| 224 | |
| 225 | // Note: We can't use `contentBasePath` here as the nav file may be in a different folder to the content.opf |
| 226 | // and the HTMLX nav file will have hrefs relative to itself |
| 227 | const std::string navContentBasePath = tocNavItem.substr(0, tocNavItem.find_last_of('/') + 1); |
| 228 | TocNavParser navParser(navContentBasePath, navSize, bookMetadataCache.get()); |
| 229 | |
| 230 | if (!navParser.setup()) { |
| 231 | LOG_ERR("EBP", "Could not setup toc nav parser"); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | const auto navBuffer = static_cast<uint8_t*>(malloc(1024)); |
| 236 | if (!navBuffer) { |
| 237 | LOG_ERR("EBP", "Could not allocate memory for toc nav parser"); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | while (tempNavFile.available()) { |
| 242 | const auto readSize = tempNavFile.read(navBuffer, 1024); |
| 243 | const auto processedSize = navParser.write(navBuffer, readSize); |
| 244 | |
| 245 | if (processedSize != readSize) { |
| 246 | LOG_ERR("EBP", "Could not process all toc nav data"); |
| 247 | free(navBuffer); |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | free(navBuffer); |
| 253 | // Explicitly close() file before calling Storage.remove() |
| 254 | tempNavFile.close(); |
| 255 | Storage.remove(tmpNavPath.c_str()); |
| 256 | |
| 257 | LOG_DBG("EBP", "Parsed TOC nav items"); |
| 258 | return true; |
| 259 | } |
| 260 |
nothing calls this directly
no test coverage detected