Пример #1
0
ResultList split_by_tokens(const std::basic_string<CharT> &s,
    const std::basic_string<CharT> &tokens, bool keepEmptyParts = true)
{
    if (tokens.length() == 1)
        return split<ResultList>(s, tokens, keepEmptyParts);

    ResultList slist;

    typename std::basic_string<CharT>::size_type start = 0;
    typename std::basic_string<CharT>::size_type pos;

    std::basic_string<CharT> part;

    // If delimiter.empty():
    //   pos = s.find_first_of(delimiter, start);
    // pos will be s.npos.
    while ((pos = s.find_first_of(tokens, start)) != s.npos) { // strtok
        part = s.substr(start, pos - start);

        if (!part.empty() || keepEmptyParts)
            slist.push_back(part);

        start = pos + 1;
    }

    if (start != s.length() || keepEmptyParts)
        slist.push_back(s.substr(start));

    return slist;
}
Пример #2
0
ResultList split(const std::basic_string<CharT> &s,
    const std::basic_string<CharT> &delimiter, bool keepEmptyParts = true)
{
    ResultList slist;

    // If delimiter.empty():
    //   pos = s.find(delimiter, start);
    // pos will be 0.
    if (delimiter.empty()) {
        slist.push_back(s);
        return slist;
    }


    typename std::basic_string<CharT>::size_type start = 0;
    typename std::basic_string<CharT>::size_type pos;

    std::basic_string<CharT> part;

    if (delimiter.length() == 1) {
        CharT ch = delimiter[0];

        // Hope that:
        //   find(std::basic_string<CharT>, CharT ch)
        // will be faster than:
        //   find(std::basic_string<CharT>, std::basic_string<CharT>)
        while ((pos = s.find(ch, start)) != s.npos) { // Use strchr/wcschr instead?
            part = s.substr(start, pos - start);

            if (!part.empty() || keepEmptyParts)
                slist.push_back(part);

            start = pos + delimiter.length();
        }
    } else {
        while ((pos = s.find(delimiter, start)) != s.npos) { // Use strstr/wcsstr instead?
            part = s.substr(start, pos - start);

            if (!part.empty() || keepEmptyParts)
                slist.push_back(part);

            start = pos + delimiter.length();
        }
    }

    if (start != s.length() || keepEmptyParts)
        slist.push_back(s.substr(start));

    return slist;
}
Пример #3
0
inline ResultList split(const std::basic_string<CharT> &s, CharT ch, bool keepEmptyParts = true)
{
#if 0
    ResultList slist;

    std::basic_istringstream<CharT> ss(s);
    std::basic_string<CharT> item;
    while (std::getline(ss, item, ch))
        slist.push_back(item);

    return slist;
#endif

    return split<ResultList>(s, std::basic_string<CharT>(1, ch), keepEmptyParts);
}
SimpleSPARQLQuery::ResultList
SimpleSPARQLQuery::Impl::executeFor(QString modelUri)
{
    ResultList list;
    librdf_query *query;

#ifdef DEBUG_SIMPLE_SPARQL_QUERY
    static std::map<QString, int> counter;
    if (counter.find(m_query) == counter.end()) counter[m_query] = 1;
    else ++counter[m_query];
    std::cerr << "Counter for this query: " << counter[m_query] << std::endl;
    std::cerr << "Base URI is: \"" << modelUri << "\"" << std::endl;
#endif

    {
        Profiler p("SimpleSPARQLQuery: Prepare LIBRDF query");
        query = librdf_new_query
            (m_redland->getWorld(), "sparql", NULL,
             (const unsigned char *)m_query.toUtf8().data(), NULL);
    }
    
    if (!query) {
        m_errorString = "Failed to construct query";
        return list;
    }

    librdf_query_results *results;
    {
        Profiler p("SimpleSPARQLQuery: Execute LIBRDF query");
        results = librdf_query_execute(query, m_redland->getModel(modelUri));
    }

    if (!results) {
        m_errorString = "RDF query failed";
        librdf_free_query(query);
        return list;
    }

    if (!librdf_query_results_is_bindings(results)) {
        m_errorString = "RDF query returned non-bindings results";
        librdf_free_query_results(results);
        librdf_free_query(query);
        return list;
    }
    
    int resultCount = 0;
    int resultTotal = librdf_query_results_get_count(results); // probably wrong
    m_cancelled = false;

    while (!librdf_query_results_finished(results)) {

        int count = librdf_query_results_get_bindings_count(results);

        KeyValueMap resultmap;

        for (int i = 0; i < count; ++i) {

            const char *name =
                librdf_query_results_get_binding_name(results, i);

            if (!name) {
                std::cerr << "WARNING: Result " << i << " of query has no name" << std::endl;
                continue;
            }

            librdf_node *node =
                librdf_query_results_get_binding_value(results, i);

            QString key = (const char *)name;

            if (!node) {
#ifdef DEBUG_SIMPLE_SPARQL_QUERY
                std::cerr << i << ". " << key << " -> (nil)" << std::endl;
#endif
                resultmap[key] = Value();
                continue;
            }

            ValueType type = LiteralValue;
            QString text;

            if (librdf_node_is_resource(node)) {

                type = URIValue;
                librdf_uri *uri = librdf_node_get_uri(node);
                const char *us = (const char *)librdf_uri_as_string(uri);

                if (!us) {
                    std::cerr << "WARNING: Result " << i << " of query claims URI type, but has null URI" << std::endl;
                } else {
                    text = us;
                }

            } else if (librdf_node_is_literal(node)) {

                type = LiteralValue;

                const char *lit = (const char *)librdf_node_get_literal_value(node);
                if (!lit) {
                    std::cerr << "WARNING: Result " << i << " of query claims literal type, but has no literal" << std::endl;
                } else {
                    text = lit;
                }

            } else if (librdf_node_is_blank(node)) {

                type = BlankValue;

                const char *lit = (const char *)librdf_node_get_literal_value(node);
                if (lit) text = lit;

            } else {

                cerr << "SimpleSPARQLQuery: LIBRDF query returned unknown node type (not resource, literal, or blank)" << endl;
            }

#ifdef DEBUG_SIMPLE_SPARQL_QUERY
            cerr << i << ". " << key << " -> " << text << " (type " << type << ")" << endl;
#endif

            resultmap[key] = Value(type, text);

//            librdf_free_node(node);
        }

        list.push_back(resultmap);

        librdf_query_results_next(results);

        resultCount++;

        if (m_reporter) {
            if (resultCount >= resultTotal) {
                if (m_reporter->isDefinite()) m_reporter->setDefinite(false);
                m_reporter->setProgress(resultCount);
            } else {
                m_reporter->setProgress((resultCount * 100) / resultTotal);
            }

            if (m_reporter->wasCancelled()) {
                m_cancelled = true;
                break;
            }
        }
    }

    librdf_free_query_results(results);
    librdf_free_query(query);

#ifdef DEBUG_SIMPLE_SPARQL_QUERY
    SVDEBUG << "SimpleSPARQLQuery::executeDatastore: All results retrieved (" << resultCount << " of them)" << endl;
#endif

    return list;
}
Пример #5
0
    virtual MICommand & execute()
    {
        if (operation.compare("-list-features") == 0)
        {
            ResultList featureList;
            Result result;
            result.value = featureList.toString();
            results.push_back(result);
        }
        else if (operation.compare("-list-thread-groups") == 0)
        {
            long recursionDepth = 0;
            bool available = false;

            for (const Option & option :options)
            {
                if (option.name.compare("--available") == 0)
                {
                    available = true;
                }
                else if (option.name.compare("--recurse") == 0)
                {
                    char * endptr = nullptr;
                    recursionDepth = strtol(option.parameter.c_str(), &endptr, 10);
                    recursionDepth = std::max(recursionDepth, 1L);
                }
            }

            if (parameters.empty() == 1)
            {
                ResultList groups;
                for (const MIInterpreter::MITargets::value_type & ptarget : interpreter.getTargets())
                {
                    ResultTuple group;
                    group.push_back(Result("id", CString(ptarget.second->getThreadGroup())));
                    group.push_back(Result("type", CString("process")));
                    group.push_back(Result("executable", CString(ptarget.second->getExecutable())));
                    if (ptarget.second->getTarget().GetProcess().IsValid())
                    {
                        lldb::SBProcess process = ptarget.second->getTarget().GetProcess();
                        group.push_back(Result("num_children", CString(process.GetNumThreads())));
                    }
                    groups.push_back(group);
                }
                results.push_back(Result("groups", groups));
            }
            else if (parameters.size() == 1)
            {
                ResultList threads;
                //todo
                results.push_back(Result("threads", threads));
            }
            else
            {
                ResultList groups;
                for (const std::string & parameter : parameters)
                {
                    MITarget * ptarget = interpreter.findTarget(parameter);
                    if (ptarget)
                    {
                        ResultTuple group;
                        group.push_back(Result("id", CString(ptarget->getThreadGroup())));
                        group.push_back(Result("type", CString("process")));
                        group.push_back(Result("type", CString(ptarget->getTarget().GetExecutable().GetFilename())));
                        if (ptarget->getTarget().GetProcess().IsValid())
                        {
                            lldb::SBProcess process = ptarget->getTarget().GetProcess();
                            group.push_back(Result("num_children", CString(process.GetNumThreads())));
                        }
                        groups.push_back(group);
                    }
                }
                results.push_back(Result("groups", groups));
            }
        }
        return *this;
    }