Esempio n. 1
0
TEST_F(BufferTest, strcat_to_str) {
    EXPECT_EQ(0, mem_buffer_strcat(&m_buffer, "abc"));
    EXPECT_STREQ("abc", (char*)mem_buffer_make_continuous(&m_buffer, 0));

    EXPECT_EQ(0, mem_buffer_strcat(&m_buffer, "def"));
    EXPECT_STREQ("abcdef", (char*)mem_buffer_make_continuous(&m_buffer, 0));

    EXPECT_EQ((size_t)7, mem_buffer_size(&m_buffer));
}
Esempio n. 2
0
TEST_F(BufferTest, make_continuous_single_trunk) {
    char buf[5] = { 0 };

    struct mem_buffer_trunk * told = append_trunk("a");
    void * newDataAddr = mem_buffer_make_continuous(&m_buffer, 0);
    EXPECT_TRUE(told == trunk_at(0));

    memcpy(buf, newDataAddr, mem_buffer_size(&m_buffer));

    EXPECT_STREQ("a", buf);
}
Esempio n. 3
0
TEST_F(BufferTest, make_continuous_multi_trunk) {
    char buf[5] = { 0 };

    EXPECT_TRUE(append_trunk("a"));
    EXPECT_TRUE(append_trunk("b"));
    EXPECT_TRUE(append_trunk("c"));

    memcpy(buf,mem_buffer_make_continuous(&m_buffer, 0), mem_buffer_size(&m_buffer));

    EXPECT_STREQ("abc", buf);
}
Esempio n. 4
0
TEST_F(BufferTest, strcat_to_str_trunk_full) {
    struct mem_buffer_trunk * trunk1 =
        mem_buffer_append_trunk(&m_buffer, 7);

    EXPECT_EQ((size_t)4, mem_trunk_append(&m_buffer, trunk1, "abc", 4));

    EXPECT_EQ(0, mem_buffer_strcat(&m_buffer, "def"));
    EXPECT_STREQ("abcdef", (char*)mem_buffer_make_continuous(&m_buffer, 0));

    EXPECT_EQ((size_t)7, mem_buffer_size(&m_buffer));
    EXPECT_EQ((size_t)1, mem_buffer_trunk_count(&m_buffer));
}
Esempio n. 5
0
int tools_main(error_monitor_t em) {
    struct cpe_dr_generate_ctx ctx;
    struct mem_buffer buffer;
    int rv;

    ctx.m_builder = NULL;
    ctx.m_metalib = NULL;
    ctx.m_em = em;

    rv = 0;

    ctx.m_builder = dr_metalib_builder_create(NULL, em);
    if (ctx.m_builder == NULL) {
        CPE_ERROR(em, "create metalib builder fail!");
        return -1;
    }

    prepare_input(ctx.m_builder, em);

    dr_metalib_builder_analize(ctx.m_builder);

    mem_buffer_init(&buffer, 0);
    if (dr_inbuild_build_lib(
            &buffer,
            dr_metalib_bilder_lib(ctx.m_builder),
            em) == 0)
    {
        ctx.m_metalib = (LPDRMETALIB)mem_buffer_make_continuous(&buffer, 0), mem_buffer_size(&buffer);

        if (ctx.m_metalib) {
            if (do_validate(&ctx) != 0) rv = -1;
            if (do_generate_h(&ctx) != 0) rv = -1;
            if (do_generate_lib_bin(&ctx) != 0) rv = -1;
            if (do_generate_lib_c(&ctx) != 0) rv = -1;
        }
    }
    else {
        rv = -1;
    }

    mem_buffer_clear(&buffer);
    dr_metalib_builder_free(ctx.m_builder);

    return rv;
}
Esempio n. 6
0
static int do_generate_h(cpe_dr_generate_ctx_t ctx) {
    struct dr_metalib_source_it source_it;
    dr_metalib_source_t source;
    struct mem_buffer buffer;
    struct write_stream_file stream;
    int i;
    FILE * fp;
    int rv;

    mem_buffer_init(&buffer, 0);

    rv = 0;
    for(i = 0; i < o_h->count; ++i) {
        dr_metalib_builder_sources(&source_it, ctx->m_builder);
        while((source = dr_metalib_source_next(&source_it))) {
            const char * file_name;

            if (dr_metalib_source_from(source) != dr_metalib_source_from_user) continue;

            file_name = dr_metalib_source_file(source);
            if (file_name == NULL) continue;

            mem_buffer_clear_data(&buffer);
            mem_buffer_strcat(&buffer, o_h->filename[i]);
            mem_buffer_strcat(&buffer, "/");
            file_name_append_base(&buffer, file_name);
            mem_buffer_strcat(&buffer, ".h");

            fp = file_stream_open((char *)mem_buffer_make_continuous(&buffer, 0), "w", ctx->m_em);
            if (fp == NULL) {
                rv = -1;
                continue;
            }

            write_stream_file_init(&stream, fp, ctx->m_em);
            cpe_dr_generate_h((write_stream_t)&stream, source, ctx);
            file_stream_close(fp, ctx->m_em);
        }
    }

    mem_buffer_clear(&buffer);

    return rv;
}
Esempio n. 7
0
void with_dr_store::t_dr_store_reset(const char * name, const char * def) {
    struct mem_buffer buffer;
    mem_buffer_init(&buffer, t_tmp_allocrator());

    EXPECT_EQ(
        0,
        dr_create_lib_from_xml_ex(
            &buffer,
            def,
            strlen(def),
            NULL));

    dr_store_t dr_store = dr_store_find_or_create(m_dr_store_mgr, name);
    ASSERT_TRUE(dr_store) << "find or crate dr_store " << name << " fail!";

    EXPECT_EQ(
        0, 
        dr_store_set_lib(dr_store, (LPDRMETALIB)mem_buffer_make_continuous(&buffer, 0), NULL, NULL));
}
Esempio n. 8
0
TEST_F(BufferTest, make_continuous_no_trunk) {
    EXPECT_TRUE(mem_buffer_make_continuous(&m_buffer, 0) == NULL);
}
Esempio n. 9
0
char *
BufferTest::as_string(void) {
    return (char *)mem_buffer_make_continuous(&m_buffer, 0);
}
Esempio n. 10
0
enum dir_visit_next_op
dir_search_i(
    dir_visitor_t visitor, void * ctx,
    int maxLevel,
    error_monitor_t em,
    mem_buffer_t buffer)
{
    DIR * dirp;
    struct dirent dbuf;
    struct dirent * dp;
    char * path;
    int rv;
    size_t bufSize;
    enum dir_visit_next_op nextOp;

    if (maxLevel == 0) return dir_visit_next_go;

    path = (char *)mem_buffer_make_continuous(buffer, 0);
    bufSize = mem_buffer_size(buffer);

    dirp = dir_open(path, 0, em);
    if (dirp == NULL) return dir_visit_next_go;

    nextOp = dir_visit_next_go;

    while((rv = readdir_r(dirp, &dbuf, &dp)) == 0 && dp) {
        if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue;

        if (
            mem_buffer_strcat(buffer, "/") != 0 || 
            mem_buffer_strcat(buffer, dp->d_name) != 0) break;

        if (S_ISDIR(DTTOIF(dp->d_type))) {
            if (visitor->on_dir_enter) {
                nextOp = visitor->on_dir_enter(
                    (const char *)mem_buffer_make_continuous(buffer, 0),
                    dp->d_name,
                    ctx);

                if (nextOp == dir_visit_next_exit) break;
            }
            else {
                nextOp = dir_visit_next_go;
            }

            if (nextOp == dir_visit_next_go) {
                nextOp = dir_search_i(
                    visitor, ctx,
                    maxLevel > 0 ? maxLevel - 1 : maxLevel,
                    em, buffer);

                if (nextOp == dir_visit_next_exit) break;
            }

            if (visitor->on_dir_leave) {
                nextOp = visitor->on_dir_leave(
                    (const char *)mem_buffer_make_continuous(buffer, 0),
                    dp->d_name,
                    ctx);

                if (nextOp == dir_visit_next_exit) break;
            }
        }
        else if (S_ISREG(DTTOIF(dp->d_type))) {
            if (visitor->on_file) {
                nextOp = visitor->on_file(
                    (const char *)mem_buffer_make_continuous(buffer, 0),
                    dp->d_name,
                    ctx);

                if (nextOp == dir_visit_next_exit) break;
            }
        }

        /*restore curent path*/
        mem_buffer_set_size(buffer, bufSize);
        path = (char *)mem_buffer_make_continuous(buffer, 0);
        if (path == NULL) {
            CPE_ERROR_EX(em, ENOMEM, "no memory for dir search path");
            nextOp = dir_visit_next_exit;
            break;
        }
        path[bufSize - 1] = 0;

    }

    /*clear resources*/
    dir_close(dirp, em);

    return nextOp == dir_visit_next_exit ?  dir_visit_next_exit : dir_visit_next_go;
}