Ejemplo n.º 1
0
void normalizeInput(SWKey &key, std::string &text) {
    int utf8State = detectUTF8(text.c_str());
    if (normalize) {
        // Don't need to normalize text that is ASCII
        // But assume other non-UTF-8 text is Latin1 (cp1252) and convert it to UTF-8
        if (!utf8State) {
            cout << "Warning: " << key.getText() << ": Converting to UTF-8 (" << text << ")" << endl;
            converter.processText(text, (SWKey *)2);  // note the hack of 2 to mimic a real key. TODO: remove all hacks
            converted++;

            // Prepare for double check. This probably can be removed.
            // But for now we are running the check again.
            // This is to determine whether we need to normalize output of the conversion.
            utf8State = detectUTF8(text.c_str());
        }

        // Double check. This probably can be removed.
        if (!utf8State) {
            cout << "Error: " << key.getText() << ": Converting to UTF-8 (" << text << ")" << endl;
        }

        if (utf8State > 0) {
            std::string before = text;
            normalizer->processText(text, (SWKey *)2);  // note the hack of 2 to mimic a real key. TODO: remove all hacks
            if (before != text) {
                normalized++;
            }
        }
    }
}
Ejemplo n.º 2
0
void ListKey::add(const SWKey &ikey) {
	if (++arraycnt > arraymax) {
		array = (SWKey **) ((array) ? realloc(array, (arraycnt + 32) * sizeof(SWKey *)) : calloc(arraycnt + 32, sizeof(SWKey *)));
		arraymax = arraycnt + 32;
	}
	array[arraycnt-1] = ikey.clone();
	setToElement(arraycnt-1);
}
Ejemplo n.º 3
0
void writeEntry(SWKey &key, std::string &text) {
#ifdef DEBUG
    cout << "(" << entryCount << ") " << key.getText() << endl;
#endif

    module->setKey(key);

    normalizeInput(key, text);

    module->setEntry(text.c_str());
}
Ejemplo n.º 4
0
void ListKey::setText(const char *ikey) {
	// at least try to set the current element to this text
	for (arraypos = 0; arraypos < arraycnt; arraypos++) {
		SWKey *key = array[arraypos];
		if (key) {
			if (key->isTraversable() && key->isBoundSet()) {
				key->setText(ikey);
				if (!key->popError())
					break;
			}
			else {
				if (!strcmp(key->getText(), ikey))
					break;
			}
		}
	}
	if (arraypos >= arraycnt) {
		error = 1;
		arraypos = arraycnt-1;
	}
	
	SWKey::setText(ikey);
}
const QString CInfoDisplay::decodeCrossReference( const QString& data ) {
	Q_ASSERT(!data.isEmpty());
	if (data.isEmpty()) {
		return QString("<div class=\"crossrefinfo\"><h3>%1</h3></div>")
				.arg(tr("Cross references"));
	}

	//  qWarning("setting crossref %s", data.latin1());

	CSwordBackend::DisplayOptions dispOpts;
	dispOpts.lineBreaks  = false;
	dispOpts.verseNumbers = true;

	CSwordBackend::FilterOptions filterOpts;
	filterOpts.headings    = false;
	filterOpts.strongNumbers  = false;
	filterOpts.morphTags    = false;
	filterOpts.lemmas     = false;
	filterOpts.footnotes   = false;
	filterOpts.scriptureReferences = false;

	CrossRefRendering renderer(dispOpts, filterOpts);
	CTextRendering::KeyTree tree;

	//  const bool isBible = true;
	CSwordModuleInfo* module = CBTConfig::get
									(CBTConfig::standardBible);

	//a prefixed module gives the module to look into
	QRegExp re("^[^ ]+:");
	//  re.setMinimal(true);
	int pos = re.indexIn(data);
	if (pos != -1) {
		pos += re.matchedLength()-1;
	}

	if (pos > 0) {
		const QString moduleName = data.left(pos);
		//     qWarning("found module %s", moduleName.latin1());
		module = CPointers::backend()->findModuleByName(moduleName);
		if (!module) {
			module = CBTConfig::get
							(CBTConfig::standardBible);
		}
		//   Q_ASSERT(module);
	}

	//Q_ASSERT(module); //why? the existense of the module is tested later
	CTextRendering::KeyTreeItem::Settings settings (
		false,
		CTextRendering::KeyTreeItem::Settings::CompleteShort
	);

	if (module && (module->type() == CSwordModuleInfo::Bible)) {
		VerseKey vk;
		sword::ListKey refs = vk.ParseVerseList((const char*)data.mid((pos == -1) ? 0 : pos+1).toUtf8(), "Gen 1:1", true);

		for (int i = 0; i < refs.Count(); ++i) {
			SWKey* key = refs.getElement(i);
			Q_ASSERT(key);
			VerseKey* vk = dynamic_cast<VerseKey*>(key);

			CTextRendering::KeyTreeItem* itm = (CTextRendering::KeyTreeItem*)0; //explicit conversion for MS VS
			if (vk && vk->isBoundSet()) { //render a range of keys
				itm = new CTextRendering::KeyTreeItem(
						QString::fromUtf8(vk->LowerBound().getText()),
						QString::fromUtf8(vk->UpperBound().getText()),
						module,
						settings
					);
			}
			else {
				itm = new CTextRendering::KeyTreeItem(
						QString::fromUtf8(key->getText()),
						QString::fromUtf8(key->getText()),
						module,
						settings
					);
			}

			Q_ASSERT(itm);

			tree.append( itm );
		}
	}
	else if (module) {
		CTextRendering::KeyTreeItem* itm = new CTextRendering::KeyTreeItem(
												data.mid((pos == -1) ? 0 : pos+1),
												module,
												settings
											);
		tree.append( itm );
	}

	//  qWarning("rendered the tree: %s", renderer.renderKeyTree(tree).latin1());
	//spanns containing rtl text need dir=rtl on their parent tag to be aligned properly
    QString lang = "en";  // default english
    if (module)
        lang = module->language()->abbrev();
	return QString("<div class=\"crossrefinfo\" lang=\"%1\"><h3>%2</h3><div class=\"para\" dir=\"%3\">%4</div></div>")
            .arg(lang)
			.arg(tr("Cross references"))
	        .arg(module ? ((module->textDirection() == CSwordModuleInfo::LeftToRight) ? "ltr" : "rtl") : "")
			.arg(renderer.renderKeyTree(tree));
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
    BlockType iType = BOOKBLOCKS;
    int compType = 1;
    string cipherKey = "";
    std::unique_ptr<SWCompress> compressor;

    int compLevel = 0;

    if ((argc < 3) || (argc > 7)) {
        errorOutHelp(argv[0]);
    }

    if (argc > 3) {
        switch (std::atoi(argv[3])) {
        case 2: iType = VERSEBLOCKS; break;
        case 3: iType = CHAPTERBLOCKS; break;
        case 4: iType = BOOKBLOCKS; break;
        default:
            std::cerr << "Argument 3 must be one of <2|3|4>\n";
            errorOutHelp(*argv);
        }
        if (argc > 4) {
            compType = std::atoi(argv[4]);
            if (argc > 5) {
                compLevel = std::atoi(argv[5]);
                if (argc > 6) {
                    cipherKey = argv[6];
                }
            }
        }
    }

    if ((iType < 2) || (compType < 1) || (compType > 4) || compLevel < 0 || compLevel > 9 || (!std::strcmp(argv[1], "-h")) || (!std::strcmp(argv[1], "--help")) || (!std::strcmp(argv[1], "/?")) || (!std::strcmp(argv[1], "-?")) || (!std::strcmp(argv[1], "-help"))) {
        errorOutHelp(argv[0]);
    }

    SWMgr mgr;

    auto const it = mgr.modules().find(argv[1]);
    if (it == mgr.modules().end()) {
        fprintf(stderr, "error: %s: couldn't find module: %s\n", argv[0], argv[1]);
        std::exit(-2);
    }

    SWModule & inModule = *it->second;

    // Try to initialize a default set of datafiles and indicies at our
    // datapath location passed to us from the user.

#define BIBLE 1
#define LEX 2
#define COM 3

    int modType = 0;
    if (inModule.getType() == "Biblical Texts") {
        modType = BIBLE;
    } else if (inModule.getType() == "Lexicons / Dictionaries") {
        modType = LEX;
    } else if (inModule.getType() == "Commentaries") {
        modType = COM;
    }

    switch (compType) {    // these are deleted by zText
    case 1: compressor = std::make_unique<LZSSCompress>(); break;
    case 2: compressor = std::make_unique<ZipCompress>(); break;
    case 3: compressor = std::make_unique<Bzip2Compress>(); break;
    case 4: compressor = std::make_unique<XzCompress>(); break;
    }
    if (compressor && compLevel > 0) {
        compressor->setLevel(compLevel);
    }

    int result = 0;
    switch (modType) {
    case BIBLE:
    case COM: {
        SWKey *k = inModule.getKey();
        VerseKey *vk = dynamic_cast<VerseKey *>(k);
        result = zText::createModule(argv[2], iType, vk->getVersificationSystem().c_str());
        break;
    }
    case LEX:
        result = zLD::createModule(argv[2]);
        break;
    }

    if (result) {
        fprintf(stderr, "error: %s: couldn't create module at path: %s\n", argv[0], argv[2]);
        std::exit(-3);
    }

    std::unique_ptr<SWModule> outModule;
    switch (modType) {
    case BIBLE:
    case COM: {
        SWKey *k = inModule.getKey();
        VerseKey *vk = dynamic_cast<VerseKey *>(k);
        outModule = std::make_unique<zText>(argv[2], nullptr, nullptr, iType, std::move(compressor),
            ENC_UNKNOWN, DIRECTION_LTR, FMT_UNKNOWN, nullptr,
            vk->getVersificationSystem().c_str());    // open our datapath with our RawText driver.
        static_cast<VerseKey *>(inModule.getKey())->setIntros(true);
        break;
    }
    case LEX:
        outModule = std::make_unique<zLD>(argv[2], nullptr, nullptr, iType, std::move(compressor));        // open our datapath with our RawText driver.
        break;
    }

    if (!cipherKey.empty())
        outModule->addRawFilter(
                    std::make_shared<CipherFilter>(cipherKey.c_str()));

    string lastBuffer = "Something that would never be first module entry";
    SWKey bufferKey;
    SWKey lastBufferKey;
    auto const outModuleKey(outModule->createKey());
    VerseKey *vkey = dynamic_cast<VerseKey *>(outModuleKey.get());
    outModuleKey->setPersist(true);
    if (vkey) {
        vkey->setIntros(true);
        vkey->setAutoNormalize(false);
    }
    outModule->setKey(*outModuleKey);

    inModule.setSkipConsecutiveLinks(false);
    inModule.positionToTop();
    while (!inModule.popError()) {
        bufferKey.positionFrom(*inModule.getKey());
        // pseudo-check for link.  Will get most common links.
        if ((lastBuffer == inModule.getRawEntry()) &&(lastBuffer.length() > 0)) {
            outModuleKey->positionFrom(bufferKey);
            outModule->linkEntry(lastBufferKey);    // link to last key
        cout << "Adding [" << bufferKey.getText() << "] link to: [" << lastBufferKey.getText() << "]\n";
        }
        else {
            lastBuffer = inModule.getRawEntry();
            lastBufferKey.setText(inModule.getKeyText());
            if (lastBuffer.length() > 0) {
                cout << "Adding [" << bufferKey.getText() << "] new text.\n";
                outModuleKey->positionFrom(bufferKey);
//                outModule->getRawEntry();    // snap
//                outModule->setKey(bufferKey);
                outModule->setEntry(lastBuffer.c_str());    // save new text;
            }
            else {
                    cout << "Skipping [" << bufferKey.getText() << "] no entry in Module.\n";
            }
        }
        inModule.increment();
    }
}
Ejemplo n.º 7
0
const char *ListKey::getShortText() const {
	int pos = arraypos;
	SWKey *key = (pos >= arraycnt || !arraycnt) ? 0:array[pos];
	return (key) ? key->getShortText() : keytext;
}