Esempio n. 1
0
sc_result sc_helper_init(sc_memory_context const * ctx)
{
    g_message("Initialize sc-helper");

    _init_keynodes_str();

    sc_keynodes = g_new0(sc_addr, SC_KEYNODE_COUNT);

    if (resolve_nrel_system_identifier(ctx) != SC_RESULT_OK)
    {
        g_message("Can't resovle nrel_system_identifier node. Create the last one");

        sc_addr addr = sc_memory_node_new(ctx, sc_type_const | sc_type_node_norole);
        sc_addr link = sc_memory_link_new(ctx);
        sc_stream *stream = sc_stream_memory_new(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER],
                                                 (sc_uint)(sizeof(sc_uchar) * strlen(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER])),
                                                 SC_STREAM_FLAG_READ, SC_FALSE);
        sc_memory_set_link_content(ctx, link, stream);
        sc_stream_free(stream);

        sc_addr arc = sc_memory_arc_new(ctx, sc_type_arc_common | sc_type_const, addr, link);
        sc_memory_arc_new(ctx, sc_type_arc_pos_const_perm, addr, arc);
        sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER] = addr;
    }

    sc_helper_is_initialized = SC_TRUE;

    return SC_RESULT_OK;
}
Esempio n. 2
0
sc_result resolve_nrel_system_identifier(sc_memory_context const * ctx)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = sc_stream_memory_new(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER],
                                             (sc_uint)(sizeof(sc_uchar) * strlen(keynodes_str[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER])),
                                             SC_STREAM_FLAG_READ, SC_FALSE);
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;
    sc_addr addr1, addr2;

    // try to find nrel_system_identifier strings
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_a_new(ctx,
                                            sc_type_node | sc_type_const | sc_type_node_norole,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_type_const | sc_type_node | sc_type_node_norole);

            while (sc_iterator5_next(it))
            {

                addr1 = sc_iterator5_value(it, 0);
                addr2 = sc_iterator5_value(it, 4);
                // comare begin sc-element and attribute, they must be equivalent
                if (SC_ADDR_IS_EQUAL(addr1, addr2))
                {
                    if (found == SC_FALSE)
                    {
                        sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER] = addr1;
                        found = SC_TRUE;
                    }else
                    {
                        sc_iterator5_free(it);
                        sc_stream_free(stream);
                        g_free(results);
                        g_error("There are more then one sc-elements with system identifier nrel_system_identifier ");
                        return SC_RESULT_ERROR;
                    }
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }else
        return SC_RESULT_ERROR;

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
Esempio n. 3
0
sc_result sc_helper_find_element_by_system_identifier(sc_memory_context const * ctx, const sc_char* data, sc_uint32 len, sc_addr *result_addr)
{
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_iterator5 *it = 0;
    sc_bool found = SC_FALSE;

    g_assert(sc_helper_is_initialized == SC_TRUE);
    g_assert(sc_keynodes != 0);

    // try to find sc-links with that contains system identifier value
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it = sc_iterator5_a_a_f_a_f_new(ctx,
                                            0,
                                            sc_type_arc_common | sc_type_const,
                                            results[i],
                                            sc_type_arc_pos_const_perm,
                                            sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it))
            {
                if (found == SC_FALSE)
                {
                    found = SC_TRUE;
                    *result_addr = sc_iterator5_value(it, 0);
                }else
                {
                    // don't foget to free allocated memory before return error
                    sc_iterator5_free(it);
                    sc_stream_free(stream);
                    g_free(results);
                    return SC_RESULT_ERROR_INVALID_STATE;
                }
            }

            sc_iterator5_free(it);
        }

        g_free(results);
    }

    sc_stream_free(stream);

    return found == SC_TRUE ? SC_RESULT_OK : SC_RESULT_ERROR;
}
Esempio n. 4
0
scp_result write_link_content_number(sc_memory_context *context, double data, sc_addr link)
{
    sc_stream *stream;
    char *content = calloc(NUMBER_PRECISE, sizeof(sc_char));
    g_snprintf(content, NUMBER_PRECISE, "%lf", data);
    stream = sc_stream_memory_new(content, (sc_uint)strlen(content), SC_STREAM_FLAG_READ, SC_FALSE);
    if (SC_RESULT_OK != sc_memory_set_link_content(context, link, stream))
    {
        free(content);
        sc_stream_free(stream);
        return SCP_RESULT_ERROR;
    }
    free(content);
    sc_stream_free(stream);
    return SCP_RESULT_TRUE;
}
Esempio n. 5
0
void test6()
{
    sc_uint32 i, j;
    sc_addr addr;
    sc_stream *stream = 0;
    sc_addr *results = 0;
    sc_uint32 results_count = 0;

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    timer = g_timer_new();

    //g_snprintf(test, 1024, "../CMakeLists.txt");
    g_timer_reset(timer);
    g_timer_start(timer);

    for (i = 0; i < link_append_count; i++)
    {
        //addr = sc_storage_link_new();
        printf("Find addrs for content %d\t", i);

        stream = sc_stream_memory_new((char*)&i, sizeof(i), SC_STREAM_READ, SC_FALSE);

        if (sc_storage_find_links_with_content(stream, &results, &results_count) == SC_RESULT_OK)
        {
            printf("founded: \n");
            for (j = 0; j < results_count; j++)
                printf("seg=%d, offset=%d\n", results[j].seg, results[j].offset);
            g_free(results);
            results = 0;
        }else
            printf("error\n");

        sc_stream_free(stream);
    }

    g_timer_stop(timer);

    printf("Created links: %d\n", link_append_count);
    printf("Links per second: %f\n", link_append_count / g_timer_elapsed(timer, 0));

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_destroy(timer);
}
Esempio n. 6
0
scp_result write_link_content_string(sc_memory_context *context, char* data, sc_addr link)
{
    sc_stream *stream;
    size_t data_len = strlen(data) + 1;
    char *content = calloc(data_len, sizeof(sc_char));
    g_snprintf(content, (gulong)data_len, "%s", data);
    stream = sc_stream_memory_new(content, (sc_uint)strlen(content), SC_STREAM_FLAG_READ, SC_FALSE);
    if (SC_RESULT_OK != sc_memory_set_link_content(context, link, stream))
    {
        free(content);
        sc_stream_free(stream);
        return SCP_RESULT_ERROR;
    }
    free(content);
    sc_stream_free(stream);
    return SCP_RESULT_TRUE;
}
Esempio n. 7
0
void test5()
{
    sc_uint32 i;
    sc_addr addr;
    sc_stream *stream = 0;

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    timer = g_timer_new();

    printf("Create %d links\n", link_append_count);

    //g_snprintf(test, 1024, "../CMakeLists.txt");
    g_timer_reset(timer);
    g_timer_start(timer);
    for (i = 0; i < link_append_count; i++)
    {
        addr = sc_storage_link_new();

        printf("Created sc-link: seg=%d, offset=%d, content=%d\n", addr.seg, addr.offset, i);

        stream = sc_stream_memory_new((char*)&i, sizeof(i), SC_STREAM_READ, SC_FALSE);
        sc_storage_set_link_content(addr, stream);
        sc_stream_free(stream);
    }

    g_timer_stop(timer);

    printf("Created links: %d\n", link_append_count);
    printf("Links per second: %f\n", link_append_count / g_timer_elapsed(timer, 0));

    printf("Segments count: %d\n", sc_storage_get_segments_count());
    print_storage_statistics();

    g_timer_destroy(timer);
}
Esempio n. 8
0
sctpErrorCode sctpCommand::processFindLinks(quint32 cmdFlags, quint32 cmdId, QDataStream *params, QIODevice *outDevice)
{

    sc_addr addr;
    sc_uint32 data_len = 0;
    sc_char *data = 0;

    Q_UNUSED(cmdFlags);

    Q_ASSERT(params != 0);

    // read length of content data
    READ_PARAM(data_len);
    Q_ASSERT(data_len > 0);

    data = new sc_char[data_len];
    if (params->readRawData(data, data_len) != data_len)
        return SCTP_ERROR_CMD_READ_PARAMS;

    sc_stream *stream = sc_stream_memory_new(data, data_len, SC_STREAM_READ, SC_FALSE);
    sc_uint32 result_count = 0;
    sc_addr *result = 0;

    if (sc_memory_find_links_with_content(stream, &result, &result_count) != SC_RESULT_OK)
        writeResultHeader(SCTP_CMD_FIND_LINKS, cmdId, SCTP_RESULT_FAIL, 0, outDevice);
    else
    {
        writeResultHeader(SCTP_CMD_FIND_LINKS, cmdId, SCTP_RESULT_OK, result_count * sizeof(sc_addr) + sizeof(result_count), outDevice);
        outDevice->write((const char*)&result_count, sizeof(result_count));
        outDevice->write((const char*)result, sizeof(sc_addr) * result_count);
    }

    sc_stream_free(stream);

    return SCTP_ERROR_NO;
}
Esempio n. 9
0
bool GwfTranslator::processString(const String &data)
{
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLError error = doc.Parse(data.c_str());

    if (error != tinyxml2::XML_SUCCESS)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     doc.GetErrorStr2(),
                     mParams.fileName,
                     -1);
    }

    tinyxml2::XMLElement *root = doc.FirstChildElement("GWF");
    if (!root)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     "Can't find root element",
                     mParams.fileName,
                     -1);
    }
    root = root->FirstChildElement("staticSector");
    if (!root)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     "Cna't find static sector",
                     mParams.fileName,
                     -1);
    }

    // collect elements
    std::vector<tinyxml2::XMLElement*> nodes;
    std::vector<tinyxml2::XMLElement*> edges;
    std::vector<tinyxml2::XMLElement*> buses;
    std::vector<tinyxml2::XMLElement*> all;

    static std::string s_arc = "arc";
    static std::string s_pair = "pair";
    static std::string s_bus = "bus";

    tinyxml2::XMLElement *el = root->FirstChildElement();
    while (el)
    {
        all.push_back(el);
        if (el->Name() == s_arc || el->Name() == s_pair)
            edges.push_back(el);
        else if (el->Name() == s_bus)
            buses.push_back(el);
        else
            nodes.push_back(el);

        el = el->NextSiblingElement();
    }

    static std::string s_node = "node";
    static std::string s_contour = "contour";

    tStringAddrMap id_map;
    tStringAddrMap::iterator itId;

    // create all nodes
    std::vector<tinyxml2::XMLElement*>::iterator it, itEnd = nodes.end();
    for (it = nodes.begin(); it != itEnd; ++it)
    {
        el = *it;

        String idtf = el->Attribute("idtf");
        String id = el->Attribute("id");
        sc_addr addr;

        itId = id_map.find(id);
        if (itId != id_map.end())
            continue;

        if (idtf.size() > 0)
        {
            if (getScAddr(idtf, addr))
            {
                id_map[id] = addr;
                continue;    // skip elements that already exists
            }
        }

        if (el->Name() == s_contour)
        {
            addr = sc_memory_node_new(sc_type_const | sc_type_node_struct);
            appendScAddr(addr, idtf);
        } else
        {
            tinyxml2::XMLElement *content = el->FirstChildElement("content");
            if (!content)
            {
                THROW_EXCEPT(Exception::ERR_PARSE,
                             "There are no child content for node with id=" + id,
                             mParams.fileName,
                             -1);
            }

            if (content->IntAttribute("type") == 0)
            {
                addr = sc_memory_node_new(convertType(el->Attribute("type")));
                appendScAddr(addr, idtf);
            } else
            {
                // need to create link
                addr = sc_memory_link_new();
                // setup content
                String data = content->GetText();

                if (content->IntAttribute("type") == 4)
                    data = base64_decode(data);

                sc_stream *stream = sc_stream_memory_new(data.c_str(), data.size(), SC_STREAM_READ, SC_FALSE);
                sc_memory_set_link_content(addr, stream);
                sc_stream_free(stream);

                if (mParams.autoFormatInfo)
                {
                    String ext = StringUtil::getFileExtension(content->Attribute("file_name"));
                    if (!ext.empty())
                        generateFormatInfo(addr, ext);
                }
            }
        }

        if (!idtf.empty())
            sc_helper_set_system_identifier(addr, idtf.c_str(), idtf.size());

        id_map[id] = addr;
    }

    // process buses
    itEnd = buses.end();
    for (it = buses.begin(); it != itEnd; ++it)
    {
        el = *it;

        tStringAddrMap::iterator itOwner = id_map.find(el->Attribute("owner"));
        if (itOwner == id_map.end())
            continue;

        id_map[el->Attribute("id")] = itOwner->second;
    }

    // now create edges
    bool created = true;
    while (created)
    {
        created = false;

        itEnd = edges.end();
        for (it = edges.begin(); it != itEnd; ++it)
        {
            el = *it;

            sc_addr addr;
            String id = el->Attribute("id");
            String idtf = el->Attribute("idtf");

            if (id_map.find(id) != id_map.end())
                continue;

            if (getScAddr(idtf, addr))
                continue;

            // get begin and end elements
            tStringAddrMap::iterator itB = id_map.find(el->Attribute("id_b"));
            if (itB == id_map.end())
                continue;

            tStringAddrMap::iterator itE = id_map.find(el->Attribute("id_e"));
            if (itE == id_map.end())
                continue;

            // create arc
            created = true;
            addr = sc_memory_arc_new(convertType(el->Attribute("type")), itB->second, itE->second);
            appendScAddr(addr, idtf);
            id_map[id] = addr;

            if (!idtf.empty())
                sc_helper_set_system_identifier(addr, idtf.c_str(), idtf.size());
        }
    }

    // now append elemnts into contours
    itEnd = all.end();
    for (it = all.begin(); it != itEnd; ++it)
    {
        el = *it;

        tStringAddrMap::iterator itSelf = id_map.find(el->Attribute("id"));
        if (itSelf == id_map.end())
            continue;

        tStringAddrMap::iterator itP = id_map.find(el->Attribute("parent"));
        if (itP == id_map.end())
            continue;

        sc_memory_arc_new(sc_type_arc_pos_const_perm, itP->second, itSelf->second);
    }

    return false;
}
Esempio n. 10
0
sc_addr SCsTranslator::createScAddr(sElement *el)
{
    sc_addr addr;
    SC_ADDR_MAKE_EMPTY(addr);

    if (el->type & sc_type_node)
        addr = sc_memory_node_new(mContext, el->type);
    else if (el->type & sc_type_link)
    {
        addr = sc_memory_link_new(mContext);

        // setup link content
        if (el->link_is_file)
        {
            String file_path;
            if (_getAbsFilePath(el->file_path, file_path))
            {
                sc_stream *stream = sc_stream_file_new(file_path.c_str(), SC_STREAM_FLAG_READ);
                if (stream)
                {
                    sc_memory_set_link_content(mContext, addr, stream);
                    sc_stream_free(stream);
                } else
                {
                    THROW_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
                                 "Can't open file " + el->file_path,
                                 mParams.fileName,
                                 -1);
                }
            } else
            {
                THROW_EXCEPT(Exception::ERR_INVALID_PARAMS,
                             "Unsupported link type  " + el->file_path,
                             mParams.fileName,
                             -1);
            }

        } else
        {           
            sc_stream *stream = sc_stream_memory_new(el->link_data.data.data(), (sc_uint)el->link_data.data.size(), SC_STREAM_FLAG_READ, SC_FALSE);
            sc_memory_set_link_content(mContext, addr, stream);
            sc_stream_free(stream);
        }


        // generate format information
        if (mParams.autoFormatInfo)
        {
            if (el->link_is_file)
            {
				size_t n = el->file_path.find_last_of(".");
                if (n != String::npos)
					generateFormatInfo(addr, el->file_path.substr(n + 1));
            }
        }
    }
    else
    {
        assert(el->arc_src && el->arc_trg);
        if (SC_ADDR_IS_EMPTY(el->arc_src->addr) || SC_ADDR_IS_EMPTY(el->arc_trg->addr))
            return addr;
        addr = sc_memory_arc_new(mContext, el->type, el->arc_src->addr, el->arc_trg->addr);
    }

    el->addr = addr;

    return addr;
}
Esempio n. 11
0
sc_result sc_helper_set_system_identifier(sc_memory_context const * ctx, sc_addr addr, const sc_char* data, sc_uint32 len)
{
    sc_iterator5 *it5 = 0;
    sc_addr *results = 0;
    sc_uint32 results_count = 0;
    sc_stream *stream = 0;
    sc_uint32 i = 0;
    sc_addr idtf_addr, arc_addr;

    SC_ADDR_MAKE_EMPTY(idtf_addr)
    g_assert(sc_keynodes != 0);

    // check if specified system identifier already used
    stream = sc_stream_memory_new(data, sizeof(sc_char) * len, SC_STREAM_FLAG_READ, SC_FALSE);
    if (sc_memory_find_links_with_content(ctx, stream, &results, &results_count) == SC_RESULT_OK)
    {
        for (i = 0; i < results_count; i++)
        {
            it5 = sc_iterator5_a_a_f_a_f_new(ctx,
                                             0,
                                             sc_type_arc_common | sc_type_const,
                                             results[i],
                                             sc_type_arc_pos_const_perm,
                                             sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER]);
            if (sc_iterator5_next(it5))
            {
                // don't foget to free allocated memory before return error
                sc_iterator5_free(it5);
                sc_stream_free(stream);
                g_free(results);
                return SC_RESULT_ERROR_INVALID_PARAMS;
            }

            sc_iterator5_free(it5);
        }

        g_free(results);
    }

    // if there are no elements with specified system identitifier, then we can use it
    idtf_addr = sc_memory_link_new(ctx);
    if (sc_memory_set_link_content(ctx, idtf_addr, stream) != SC_RESULT_OK)
    {
        sc_stream_free(stream);
        return SC_RESULT_ERROR;
    }

    // we doesn't need link data anymore
    sc_stream_free(stream);

    // setup new system identifier
    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_common | sc_type_const, addr, idtf_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    arc_addr = sc_memory_arc_new(ctx, sc_type_arc_pos_const_perm, sc_keynodes[SC_KEYNODE_NREL_SYSTEM_IDENTIFIER], arc_addr);
    if (SC_ADDR_IS_EMPTY(arc_addr))
        return SC_RESULT_ERROR;

    return SC_RESULT_OK;
}
Esempio n. 12
0
ScStream::ScStream(sc_char const * buffer, size_t bufferSize, sc_uint8 flags)
{
  m_stream = sc_stream_memory_new(buffer, (sc_uint)bufferSize, flags, SC_FALSE);
}