| 145 | } |
| 146 | |
| 147 | bool Epub::parseTocNcxFile() const { |
| 148 | // the ncx file should have been specified in the content.opf file |
| 149 | if (tocNcxItem.empty()) { |
| 150 | LOG_DBG("EBP", "No ncx file specified"); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | LOG_DBG("EBP", "Parsing toc ncx file: %s", tocNcxItem.c_str()); |
| 155 | |
| 156 | const auto tmpNcxPath = getCachePath() + "/toc.ncx"; |
| 157 | HalFile tempNcxFile; |
| 158 | if (!Storage.openFileForWrite("EBP", tmpNcxPath, tempNcxFile)) { |
| 159 | return false; |
| 160 | } |
| 161 | readItemContentsToStream(tocNcxItem, tempNcxFile, 1024); |
| 162 | // Explicitly close() file before reopening for reading |
| 163 | tempNcxFile.close(); |
| 164 | if (!Storage.openFileForRead("EBP", tmpNcxPath, tempNcxFile)) { |
| 165 | return false; |
| 166 | } |
| 167 | const auto ncxSize = tempNcxFile.size(); |
| 168 | |
| 169 | TocNcxParser ncxParser(contentBasePath, ncxSize, bookMetadataCache.get()); |
| 170 | |
| 171 | if (!ncxParser.setup()) { |
| 172 | LOG_ERR("EBP", "Could not setup toc ncx parser"); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | const auto ncxBuffer = static_cast<uint8_t*>(malloc(1024)); |
| 177 | if (!ncxBuffer) { |
| 178 | LOG_ERR("EBP", "Could not allocate memory for toc ncx parser"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | while (tempNcxFile.available()) { |
| 183 | const auto readSize = tempNcxFile.read(ncxBuffer, 1024); |
| 184 | if (readSize == 0) break; |
| 185 | const auto processedSize = ncxParser.write(ncxBuffer, readSize); |
| 186 | |
| 187 | if (processedSize != readSize) { |
| 188 | LOG_ERR("EBP", "Could not process all toc ncx data"); |
| 189 | free(ncxBuffer); |
| 190 | return false; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | free(ncxBuffer); |
| 195 | // Explicitly close() file before calling Storage.remove() |
| 196 | tempNcxFile.close(); |
| 197 | Storage.remove(tmpNcxPath.c_str()); |
| 198 | |
| 199 | LOG_DBG("EBP", "Parsed TOC items"); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | bool Epub::parseTocNavFile() const { |
| 204 | // the nav file should have been specified in the content.opf file (EPUB 3) |
nothing calls this directly
no test coverage detected