Example #1
0
END_TEST

START_TEST (document_type)
{
    reset_errno();
    size_t c = model_size(model);
    assert_noerr();
    assert_uint_eq(1, c);
    
    reset_errno();
    Document *d = model_document(model, 0);
    assert_noerr();
    assert_not_null(d);
    
    reset_errno();
    Node *bogus = model_document(model, 1);
    assert_errno(EINVAL);
    assert_null(bogus);
    
    reset_errno();
    Node *r1 = model_document_root(model, 0);
    assert_noerr();
    assert_not_null(r1);
    assert_node_kind(r1, MAPPING);

    reset_errno();
    Node *r2 = document_root(d);
    assert_noerr();
    assert_not_null(r2);
    assert_ptr_eq(r1, r2);
}
Example #2
0
END_TEST

START_TEST (null_document)
{
    reset_errno();
    assert_null(document_root(NULL));
    assert_errno(EINVAL);
}
Example #3
0
node *model_document_root(const document_model *model, size_t index)
{
    node *document = model_document(model, index);
    node *result = NULL;

    if(NULL != document)
    {
        result = document_root(document);
    }

    return result;
}
Example #4
0
static bool emit_document(node *document, void *context)
{
    log_trace(component, "emitting document");
    yaml_emitter_t *emitter = (yaml_emitter_t *)context;
    yaml_event_t event;

    yaml_document_start_event_initialize(&event, &(yaml_version_directive_t){1, 1}, NULL, NULL, 0);
    if (!yaml_emitter_emit(emitter, &event))
        return false;

    if(!emit_node(document_root(document), context))
    {
        return false;
    }

    yaml_document_end_event_initialize(&event, 0);
    if (!yaml_emitter_emit(emitter, &event))
        return false;

    return true;
}
Example #5
0
bool emit_node(node *each, void *argument)
{
    emit_context *context = (emit_context *)argument;

    bool result = true;
    switch(node_kind(each))
    {
        case DOCUMENT:
            log_trace("shell", "emitting document");
            result = emit_node(document_root(each), NULL);
            break;
        case SCALAR:
            result = emit_scalar(each);
            EMIT("\n");
            break;
        case SEQUENCE:
            log_trace("shell", "emitting seqence");
            MAYBE_EMIT("(");
            result = sequence_iterate(each, emit_sequence_item, NULL);
            MAYBE_EMIT(")");
            EMIT("\n");
            break;
        case MAPPING:
            log_trace("shell", "emitting mapping");
            MAYBE_EMIT("(");
            result = mapping_iterate(each, context->emit_mapping_item, NULL);
            MAYBE_EMIT(")");
            EMIT("\n");
            break;
        case ALIAS:
            log_trace("shell", "resolving alias");
            result = emit_node(alias_target(each), NULL);
            break;

    }
    fflush(stdout);

    return result;
}
Example #6
0
wxThread::ExitCode WebServerThread::Entry()
{
    // Get user setting
    int webserverPort = Model_Setting::instance().GetIntSetting("WEBSERVERPORT", 8080);
    const wxString& strPort = wxString::Format("%d", webserverPort);

    // Create and configure the server
    struct mg_mgr mgr;

    mg_mgr_init(&mgr, NULL);
    struct mg_connection* nc = mg_bind(&mgr, strPort.c_str(), ev_handler);
    if (nc == nullptr)
    {
        wxLogDebug(wxString::Format("mg_bind(%s) failed", strPort));
        mg_mgr_free(&mgr);
        return (wxThread::ExitCode)-1;
    }
    
    mg_set_protocol_http_websocket(nc);
    std::string document_root(wxFileName(mmex::getReportIndex()).GetPath().c_str());
    s_http_server_opts.document_root = document_root.c_str();
    s_http_server_opts.enable_directory_listing = "yes";

    wxSetWorkingDirectory(wxString(s_http_server_opts.document_root));

    // Serve requests 
    while (IsAlive())
    {
        mg_mgr_poll(&mgr, 1000);
    }

    // Cleanup, and free server instance
    mg_mgr_free(&mgr);

    return nullptr;
}
Example #7
0
bool mapping_iterate(const node *mapping, mapping_iterator iterator, void *context)
{
    PRECOND_NONNULL_ELSE_FALSE(mapping, iterator);
    PRECOND_ELSE_FALSE(MAPPING == node_kind(mapping));

    context_adapter adapter = {.iterator.mapping=iterator, .context=context};
    return hashtable_iterate(mapping->content.mapping, mapping_iterator_adpater, &adapter);
}

bool node_equals(const node *one, const node *two)
{
    if(one == two)
    {
        return true;
    }

    if((NULL == one && NULL != two) || (NULL != one && NULL == two))
    {
        return false;
    }

    bool result = node_kind(one) == node_kind(two) &&
        tag_equals(node_name(one), node_name(two)) &&
        node_size(one) == node_size(two);

    if(!result)
    {
        return result;
    }
    switch(node_kind(one))
    {
        case DOCUMENT:
            result &= node_equals(document_root(one), document_root(two));
            break;
        case SCALAR:
            result &= scalar_equals(one, two);
            break;
        case SEQUENCE:
            result &= sequence_equals(one, two);
            break;
        case MAPPING:
            result &= mapping_equals(one, two);
            break;
        case ALIAS:
            result &= node_equals(alias_target(one), alias_target(two));
            break;
    }
    return result;
}

static bool scalar_equals(const node *one, const node *two)
{
    size_t n1 = node_size(one);
    size_t n2 = node_size(two);

    if(n1 != n2)
    {
        return false;
    }
    return memcmp(scalar_value(one), scalar_value(two), n1) == 0;
}