Example #1
0
void CHMTextStream::resetToStart() {
	myIndex = 0;

	if (!myEntryNames.empty()) {
		return;
	}

	CHMFileInfo::FileNames names = myCHMFile.sectionNames(myBase);
	if (names.empty()) {
		return;
	}

	CHMReferenceCollection referenceCollection;

	referenceCollection.addReference(names.Start, false);
	referenceCollection.addReference(names.Home, false);

	shared_ptr<ZLInputStream> tocStream = myCHMFile.entryStream(myBase, names.TOC);
	if (!tocStream.isNull() && tocStream->open()) {
		referenceCollection.setPrefix(names.TOC);
		HHCReferenceCollector(referenceCollection).readDocument(*tocStream);
	}

	while (referenceCollection.containsNonProcessedReferences()) {
		myEntryNames.push_back(referenceCollection.nextReference());
	}
}
Example #2
0
bool CHMPlugin::readDescription(const std::string &path, BookDescription &description) const {
	ZLFile file(path);
	shared_ptr<ZLInputStream> stream = file.inputStream();
	if (stream.isNull() || !stream->open()) {
		return false;
	}

	CHMFileInfo chmFile(path);
	if (!chmFile.init(*stream)) {
		return false;
	}

	CHMFileInfo::FileNames names = chmFile.sectionNames(stream);
	if (names.empty()) {
		return false;
	}

	/*
	shared_ptr<ZLInputStream> entryStream = chmFile.entryStream(stream, names.Start);
	if (entryStream.isNull()) {
		entryStream = chmFile.entryStream(stream, names.Home);
	}
	if (entryStream.isNull()) {
		entryStream = chmFile.entryStream(stream, names.TOC);
	}
	/ *
	if (entryStream.isNull()) {
		chmFile.entryStream(stream, names.Index);
	}
	* /
	if (entryStream.isNull()) {
		return false;
	}
	*/

	CHMTextStream textStream(chmFile, stream);
	detectEncodingAndLanguage(description, textStream);
	if (description.encoding().empty()) {
		return false;
	}

	return true;
}
Example #3
0
bool CHMPlugin::readMetaInfo(Book &book) const {
    const ZLFile &file = book.file();
    shared_ptr<ZLInputStream> stream = file.inputStream();
    if (stream.isNull() || !stream->open()) {
        return false;
    }

    CHMFileInfo chmFile(file);
    if (!chmFile.init(*stream)) {
        return false;
    }

    CHMFileInfo::FileNames names = chmFile.sectionNames(stream);
    if (names.empty()) {
        return false;
    }

    /*
    shared_ptr<ZLInputStream> entryStream = chmFile.entryStream(stream, names.Start);
    if (entryStream.isNull()) {
    	entryStream = chmFile.entryStream(stream, names.Home);
    }
    if (entryStream.isNull()) {
    	entryStream = chmFile.entryStream(stream, names.TOC);
    }
    / *
    if (entryStream.isNull()) {
    	chmFile.entryStream(stream, names.Index);
    }
    * /
    if (entryStream.isNull()) {
    	return false;
    }
    */

    CHMTextStream textStream(chmFile, stream);
    detectEncodingAndLanguage(book, textStream);
    if (book.encoding().empty()) {
        return false;
    }

    return true;
}
Example #4
0
bool CHMPlugin::readModel(const BookDescription &description, BookModel &model) const {
	shared_ptr<ZLInputStream> stream = ZLFile(description.fileName()).inputStream();
	if (stream.isNull() || !stream->open()) {
		return false;
	}

	shared_ptr<CHMFileInfo> info = new CHMFileInfo(description.fileName());
	if (!info->init(*stream)) {
		return false;
	}

	CHMFileInfo::FileNames names = info->sectionNames(stream);
	if (names.empty()) {
		return false;
	}

	CHMReferenceCollection referenceCollection;

	referenceCollection.addReference(names.Start, false);
	referenceCollection.addReference(names.Home, false);

	shared_ptr<ZLInputStream> tocStream = info->entryStream(stream, names.TOC);
	HHCReader hhcReader(referenceCollection, model, description.encoding());
	if (!tocStream.isNull() && tocStream->open()) {
		referenceCollection.setPrefix(names.TOC);
		hhcReader.readDocument(*tocStream);
	}

	/*
	if (!tocStream.isNull() && tocStream->open()) {
		std::string buf;
		buf.append(tocStream->sizeOfOpened(), '\0');
		tocStream->read((char*)buf.data(), buf.length());
		std::cerr << "[ " << names.TOC << " ]\n" << buf << "\n";
	}
	*/

	int contentCounter = 0;
	PlainTextFormat format(description.fileName());
	HtmlSectionReader reader(model, format, description.encoding(), info, referenceCollection);
	while (referenceCollection.containsNonProcessedReferences()) {
		const std::string fileName = referenceCollection.nextReference();
		if (ZLStringUtil::stringEndsWith(fileName, ".jpg") ||
		    ZLStringUtil::stringEndsWith(fileName, ".gif")) {
			std::string lowerCasedFileName = ZLUnicodeUtil::toLower(fileName);
			BookReader bookReader(model);
			bookReader.setMainTextModel();
			bookReader.addHyperlinkLabel(lowerCasedFileName);
			bookReader.pushKind(REGULAR);
			bookReader.beginParagraph();
			bookReader.addImageReference(lowerCasedFileName);
			bookReader.addImage(fileName, new CHMFileImage(info, fileName));
			bookReader.endParagraph();
			bookReader.insertEndOfTextParagraph();
		} else {
			shared_ptr<ZLInputStream> entryStream = info->entryStream(stream, fileName);
			if (!entryStream.isNull() && entryStream->open()) {
				/*
				std::string buf;
				buf.append(entryStream->sizeOfOpened(), '\0');
				entryStream->read((char*)buf.data(), buf.length());
				std::cerr << "[ " << fileName << " ]\n" << buf << "\n";
				entryStream->open();
				*/
				reader.setSectionName(fileName);
				reader.readDocument(*entryStream);
				++contentCounter;
			}
		}
	}
	if (contentCounter == 0) {
		return false;
	}

	hhcReader.setReferences();

	return true;
}