Exemple #1
0
static inline void swap(struct heap *h, size_t i, size_t j) {
    char tmp[h->item_size];

    void *pi = pitem(h, i);
    void *pj = pitem(h, j);

    h->cpy(tmp, pi);
    h->cpy(pi, pj);
    h->cpy(pj, tmp);
}
Exemple #2
0
void *heap_pop(struct heap *h) {

    if( !h->n ) {
        return 0;
    }

    if( h->n == 1 ) {
        h->n--;
        return pitem(h, 0);
    }

    size_t l = --h->n;
    swap(h, 0, l);
    sift_down(h, 0);

    return pitem(h, l);
}
Exemple #3
0
void *heap_get(struct heap *h) {

    if( !h->n ) {
        return 0;
    }

    return pitem(h, 0);
}
Exemple #4
0
bool heap_add(struct heap *h, void *v) {
    if( h->n >= h->size ) {
        return false;
    }

    size_t i = h->n++;

    h->cpy(pitem(h,i), v);
    sift_up(h, i);

    return true;
}
Exemple #5
0
void VSearch::searchFirstPhaseFile(const QString &p_basePath,
                                   const QString &p_filePath,
                                   const QSharedPointer<VSearchResult> &p_result)
{
    Q_ASSERT(testTarget(VSearchConfig::Note));

    QString name = VUtils::fileNameFromPath(p_filePath);
    if (!matchPattern(name)) {
        return;
    }

    if (testObject(VSearchConfig::Name)) {
        if (matchNonContent(name)) {
            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Note,
                                                            VSearchResultItem::LineNumber,
                                                            name,
                                                            p_filePath);
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Path)) {
        QString normFilePath(QDir(p_basePath).relativeFilePath(p_filePath));
        removeSlashFromPath(normFilePath);
        if (matchNonContent(normFilePath)) {
            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Note,
                                                            VSearchResultItem::LineNumber,
                                                            name,
                                                            p_filePath);
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Content)) {
        // Add an item for second phase process.
        p_result->addSecondPhaseItem(p_filePath);
    }
}
Exemple #6
0
// appends _tr's state to _chunkOut
//JFB TODO: save envs with beat position info (like it is done for items)
void SaveSingleTrackTemplateChunk(MediaTrack* _tr, WDL_FastString* _chunkOut, bool _delItems, bool _delEnvs)
{
	if (_tr && _chunkOut)
	{
		SNM_TrackEnvParserPatcher p(_tr, false); // no auto-commit!
		if (_delEnvs)
			p.RemoveEnvelopes();

		// systematically remove items whatever is _delItems, then
		// replace them with items + optional beat pos info, if !_delItems (i.e. mimic native templates)
		p.RemoveSubChunk("ITEM", 2, -1);

		_chunkOut->Append(p.GetChunk()->Get());

		if (!_delItems)
		{
			double d;
			WDL_FastString beatInfo;
			for (int i=0; i<GetTrackNumMediaItems(_tr); i++)
			{
				if (MediaItem* item = GetTrackMediaItem(_tr, i))
				{
					SNM_ChunkParserPatcher pitem(item, false); // no auto-commit!
					int posChunk = pitem.GetLinePos(1, "ITEM", "POSITION", 1, 0); // look for the *next* line
					if (--posChunk>=0) { // --posChunk to zap "\n"
						TimeMap2_timeToBeats(NULL, *(double*)GetSetMediaItemInfo(item, "D_POSITION", NULL), NULL, NULL, &d, NULL);
						beatInfo.SetFormatted(32, " %.14f", d);
						pitem.GetChunk()->Insert(beatInfo.Get(), posChunk);
					}
					posChunk = pitem.GetLinePos(1, "ITEM", "SNAPOFFS", 1, 0);
					if (--posChunk>=0) {
						TimeMap2_timeToBeats(NULL, *(double*)GetSetMediaItemInfo(item, "D_SNAPOFFSET", NULL), NULL, NULL, &d, NULL);
						beatInfo.SetFormatted(32, " %.14f", d);
						pitem.GetChunk()->Insert(beatInfo.Get(), posChunk);
					}
					posChunk = pitem.GetLinePos(1, "ITEM", "LENGTH", 1, 0);
					if (--posChunk>=0) {
						TimeMap2_timeToBeats(NULL, *(double*)GetSetMediaItemInfo(item, "D_LENGTH", NULL), NULL, NULL, &d, NULL);
						beatInfo.SetFormatted(32, " %.14f", d);
						pitem.GetChunk()->Insert(beatInfo.Get(), posChunk);
					}
					_chunkOut->Insert(pitem.GetChunk(), _chunkOut->GetLength()-2); // -2: before ">\n"
				}
			}
		}
	}
}
Exemple #7
0
void VSearch::searchFirstPhase(VNotebook *p_notebook,
                               const QSharedPointer<VSearchResult> &p_result)
{
    bool opened = p_notebook->isOpened();
    if (!opened && !p_notebook->open()) {
        p_result->logError(QString("Fail to open notebook %1.").arg(p_notebook->getName()));
        p_result->m_state = VSearchState::Fail;
        return;
    }

    if (testTarget(VSearchConfig::Notebook)
        && testObject(VSearchConfig::Name)) {
        QString text = p_notebook->getName();
        if (matchNonContent(text)) {
            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Notebook,
                                                            VSearchResultItem::LineNumber,
                                                            text,
                                                            p_notebook->getPath());
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (!testTarget(VSearchConfig::Note)
        && !testTarget(VSearchConfig::Folder)) {
        goto exit;
    }

    // Search for subfolders.
    for (auto const & dir : p_notebook->getRootDir()->getSubDirs()) {
        if (askedToStop()) {
            qDebug() << "asked to cancel the search";
            p_result->m_state = VSearchState::Cancelled;
            goto exit;
        }

        searchFirstPhase(dir, p_result);
    }

exit:
    if (!opened) {
        p_notebook->close();
    }
}
Exemple #8
0
void VSearch::searchFirstPhase(const QString &p_basePath,
                               const QString &p_directoryPath,
                               const QSharedPointer<VSearchResult> &p_result)
{
    Q_ASSERT(testTarget(VSearchConfig::Note) || testTarget(VSearchConfig::Folder));
    Q_ASSERT(!p_directoryPath.isEmpty());

    QDir dir(p_directoryPath);
    if (!dir.exists()) {
        p_result->logError(QString("Directory %1 does not exist.").arg(p_directoryPath));
        p_result->m_state = VSearchState::Fail;
        return;
    }

    Q_ASSERT(dir.isAbsolute());

    if (testTarget(VSearchConfig::Folder)) {
        QString name = dir.dirName();
        if (testObject(VSearchConfig::Name)) {
            if (matchNonContent(name)) {
                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
                                                                VSearchResultItem::LineNumber,
                                                                name,
                                                                p_directoryPath);
                QSharedPointer<VSearchResultItem> pitem(item);
                emit resultItemAdded(pitem);
            }
        }

        if (testObject(VSearchConfig::Path)) {
            QString normPath(QDir(p_basePath).relativeFilePath(p_directoryPath));
            removeSlashFromPath(normPath);
            if (matchNonContent(normPath)) {
                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
                                                                VSearchResultItem::LineNumber,
                                                                name,
                                                                p_directoryPath);
                QSharedPointer<VSearchResultItem> pitem(item);
                emit resultItemAdded(pitem);
            }
        }
    }

    if (testTarget(VSearchConfig::Note)) {
        QStringList files = dir.entryList(QDir::Files);
        for (auto const & file : files) {
            if (askedToStop()) {
                qDebug() << "asked to cancel the search";
                p_result->m_state = VSearchState::Cancelled;
                return;
            }

            searchFirstPhaseFile(p_basePath, dir.absoluteFilePath(file), p_result);
        }
    }

    // Search subfolders.
    QStringList subdirs = dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
    for (auto const & sub : subdirs) {
        if (askedToStop()) {
            qDebug() << "asked to cancel the search";
            p_result->m_state = VSearchState::Cancelled;
            return;
        }

        searchFirstPhase(p_basePath, dir.absoluteFilePath(sub), p_result);
    }
}
Exemple #9
0
void VSearch::searchFirstPhase(VDirectory *p_directory,
                               const QSharedPointer<VSearchResult> &p_result)
{
    Q_ASSERT(testTarget(VSearchConfig::Note) || testTarget(VSearchConfig::Folder));

    bool opened = p_directory->isOpened();
    if (!opened && !p_directory->open()) {
        p_result->logError(QString("Fail to open folder %1.").arg(p_directory->fetchRelativePath()));
        p_result->m_state = VSearchState::Fail;
        return;
    }

    if (testTarget(VSearchConfig::Folder)) {
        QString name = p_directory->getName();
        QString dirPath = p_directory->fetchPath();
        if (testObject(VSearchConfig::Name)) {
            if (matchNonContent(name)) {
                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
                                                                VSearchResultItem::LineNumber,
                                                                name,
                                                                dirPath);
                QSharedPointer<VSearchResultItem> pitem(item);
                emit resultItemAdded(pitem);
            }
        }

        if (testObject(VSearchConfig::Path)) {
            QString normPath(p_directory->fetchRelativePath());
            removeSlashFromPath(normPath);
            if (matchNonContent(normPath)) {
                VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Folder,
                                                                VSearchResultItem::LineNumber,
                                                                name,
                                                                dirPath);
                QSharedPointer<VSearchResultItem> pitem(item);
                emit resultItemAdded(pitem);
            }
        }
    }

    // Search files.
    if (testTarget(VSearchConfig::Note)) {
        for (auto const & file : p_directory->getFiles()) {
            if (askedToStop()) {
                qDebug() << "asked to cancel the search";
                p_result->m_state = VSearchState::Cancelled;
                goto exit;
            }

            searchFirstPhase(file, p_result);
        }
    }

    // Search subfolders.
    for (auto const & dir : p_directory->getSubDirs()) {
        if (askedToStop()) {
            qDebug() << "asked to cancel the search";
            p_result->m_state = VSearchState::Cancelled;
            goto exit;
        }

        searchFirstPhase(dir, p_result);
    }

exit:
    if (!opened) {
        p_directory->close();
    }
}
Exemple #10
0
void VSearch::searchFirstPhase(VFile *p_file,
                               const QSharedPointer<VSearchResult> &p_result,
                               bool p_searchContent)
{
    Q_ASSERT(testTarget(VSearchConfig::Note));

    QString name = p_file->getName();
    if (!matchPattern(name)) {
        return;
    }

    QString filePath = p_file->fetchPath();
    if (testObject(VSearchConfig::Name)) {
        if (matchNonContent(name)) {
            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Note,
                                                            VSearchResultItem::LineNumber,
                                                            name,
                                                            filePath);
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Path)) {
        QString normFilePath;
        if (p_file->getType() == FileType::Note) {
            normFilePath = static_cast<VNoteFile *>(p_file)->fetchRelativePath();
        } else {
            normFilePath = filePath;
        }

        removeSlashFromPath(normFilePath);
        if (matchNonContent(normFilePath)) {
            VSearchResultItem *item = new VSearchResultItem(VSearchResultItem::Note,
                                                            VSearchResultItem::LineNumber,
                                                            name,
                                                            filePath);
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Outline)) {
        VSearchResultItem *item = searchForOutline(p_file);
        if (item) {
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Tag)) {
        VSearchResultItem *item = searchForTag(p_file);
        if (item) {
            QSharedPointer<VSearchResultItem> pitem(item);
            emit resultItemAdded(pitem);
        }
    }

    if (testObject(VSearchConfig::Content)) {
        // Search content in first phase.
        if (p_searchContent) {
            VSearchResultItem *item = searchForContent(p_file);
            if (item) {
                QSharedPointer<VSearchResultItem> pitem(item);
                emit resultItemAdded(pitem);
            }
        } else {
            // Add an item for second phase process.
            p_result->addSecondPhaseItem(filePath);
        }
    }
}
Exemple #11
0
static inline bool leq(struct heap *h, size_t i, size_t j) {
    return h->leq(pitem(h,i), pitem(h,j));
}