⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wchar_sort.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
📖 第 1 页 / 共 2 页
字号:

                /* vector of nchar */
                sz = (uint2)((one->line_len / sizeof(wchar_t)) / 3);
                Obj1_vect_a_size(&obj, &j);
                if (j != sz)
                {
                    printf("Invalid vector size detected at %d. LineNo:%d. ", __LINE__, one->line_no);
                    break;
                };
                pw = one->line;
                for (i = 0; i < sz; i++)
                {
                    Obj1_vect_a_at(&obj, i, part, sizeof(part) / sizeof(part[0]));
                    if (0 != memcmp(part, &pw[i* 3], 3))
                    {
                        printf("Invalid array detected at %d. LineNo:%d.\n", __LINE__, one->line_no);
                        DumpShorts("expected", &pw[i* sz], 3);
                        DumpShorts("   found", part, 3);
                        break;
                    };
                };
                if (i < sz)
                {
                    break;
                }

                /* vector of nstring */
                j = 1;
                for (i = 0; i < one->line_len / sizeof(wchar_t); i++)
                {
                    if (((wchar_t*)one->line)[i] == 0x20)
                    {
                        j++;
                    }
                };
                Obj1_vect_s_size(&obj, &sz);
                if (j != sz)
                {
                    printf("Invalid vector size detected at %d. LineNo:%d.\n", __LINE__, one->line_no);
                    break;
                };

                pw = one->line;
                for (i = 0; i < sz; i++)
                {

                    if (MCO_S_OK != Obj1_vect_s_at_len(&obj, i, &j))
                    {
                        printf("Absence of the vector's element detected at %d. LineNo:%d. ", __LINE__, one->line_no);
                        break;
                    };

                    p = malloc((j + 1)* sizeof(wchar_t));
                    Obj1_vect_s_at(&obj, i, p, (uint2)(j + 1), &j);
                    if ((one->line_len / sizeof(wchar_t)) < ((unsigned long)pw - (unsigned long)one->line) / sizeof
                        (wchar_t) + j || 0 != memcmp(pw, p, j))
                    {
                        printf("Invalid string detected at %d. LineNo:%d.\n", __LINE__, one->line_no);
                        printf("expected:%d found:%d\n", (one->line_len / sizeof(wchar_t)), j);
                        DumpShorts("expected", pw, (uint2)(one->line_len / sizeof(wchar_t)));
                        DumpShorts("   found", p, j);
                        break;
                    };
                    if (((wchar_t*)p)[j] != 0)
                    {
                        printf("No zero-terminator at the end of the string at %d. LineNo:%d. ", __LINE__, one->line_no)
                               ;
                        break;
                    };
                    free(p);

                    pw += j + 1;

                };
                if (i < sz)
                {
                    break;
                }

            };
            mco_trans_commit(t);
        }
        else
        {
            printf("Can't open a transaction. Error code: %d\n", rc);
        };

        one = one->next;
    };

    return one ? 1 : 0;
};

int cmpr(const void* p1, const void* p2)
{

    one_h h1 = *((one_h*)p1);
    one_h h2 = *((one_h*)p2);

    wchar_t* s1 = h1->line;
    wchar_t* s2 = h2->line;

	return WSCMP(s1, s2);
    /*return wcscmp(s1, s2);*/
    /*	do {
    if (!(*s1)) {
    if (!(*s2)) return 0;
    else return -1;
    } else if (!(*s2)) 
    return 1;
    if (*s1 != *s2)
    return (*s1) - (*s2);
    s1++; s2++;
    } while (*s1 || *s2);
    return 0;*/
};

int CheckSortOrder()
{

    int result = 0;
    one_h* arr;
    uint4 i = 0, j = 0;
    one_h one = root;
    MCO_RET rc = MCO_S_OK;
    mco_trans_h t;
    Obj2 obj2;
    Obj3 obj3;
    Obj4 obj4;
    Obj5 obj5;
    mco_cursor_t c;

    arr = (one_h*)malloc(sizeof(one_h)* line_cnt);

    while (one)
    {

        if ((one->flags &OBJ1) != 0)
        {
            arr[i] = one;
            i++;
        };

        one = one->next;
    };

    qsort(arr, i, sizeof(one_h), cmpr);
    /*
    for ( i=0; i< line_cnt; i++ ) {
    DumpShorts("sort", arr[i]->line, arr[i]->line_len / 2);
    };
     */
    /* hash */
    if ((rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK)
    {

        one = root;
        while (one)
        {

            if ((one->flags &OBJ2) != 0 && one->line_len != 0)
            {

                if (MCO_S_OK != (rc = Obj2_by_hash_find(t, one->line, (uint2)(one->line_len / sizeof(wchar_t)), &obj2)))
                {
                    printf("An expected item wasn't found at %d. RC=%d. ", __LINE__, rc);
                    DumpShorts("expected", one->line, (uint2)(one->line_len / 2));
                    result = 1;
                }
                else
                {
                    wchar_t buf[1024];

                    Obj2_line_get(&obj2, buf, sizeof(buf) / sizeof(buf[0]));
                    for (i = 0; i < sizeof(buf) / sizeof(buf[0]) / 2; i++)
                    {
                        wchar_t us = buf[i* 2];
                        buf[i* 2] = buf[i* 2+1];
                        if (buf[i* 2] != us)
                        {
                            buf[i* 2+1] = us;
                        }
                        else
                        {
                            buf[i* 2+1] = us + 1;
                        }
                    };

                    if (MCO_S_OK == Obj2_by_hash_find(t, buf, sizeof(buf) / sizeof(buf[0]), &obj2))
                    {
                        printf("An unexpected item was found at %d.", __LINE__);
                        DumpShorts("unexpected", buf, sizeof(buf) / sizeof(buf[0]));
                        result = 1;
                    };
                };
            };

            if ((one->flags &OBJ4) != 0 && one->line_len != 0)
            {

                if (MCO_S_OK != (rc = Obj4_by_hash_find(t, one->line, (uint2)(one->line_len / sizeof(wchar_t)), &obj4)))
                {
                    printf("An expected item wasn't found at %d. RC=%d. ", __LINE__, rc);
                    DumpShorts("expected", one->line, (uint2)(one->line_len / 2));
                    result = 1;
                }
                else
                {
                    uint2 sz;
                    wchar_t* p, * v;

                    Obj4_line_size(&obj4, &sz);

                    if (sz > 1)
                    {

                        p = (wchar_t*)malloc(sizeof(wchar_t)*(sz + 1));
                        v = (wchar_t*)malloc(sizeof(wchar_t)*(sz + 1));

                        Obj4_line_get(&obj4, p, (uint2)(sz + 1), &sz);

                        for (i = 0; i < (uint2)(sz / 2); i++)
                        {
                            v[i* 2] = p[i* 2+1];
                            v[i* 2+1] = p[i* 2];
                            if (v[i* 2] == v[i* 2+1])
                            {
                                v[i* 2+1] += 1;
                            }
                        };

                        if (0 != memcmp(v, p, sizeof(wchar_t)*(sz + 1)))
                        {

                            if (MCO_S_OK == Obj4_by_hash_find(t, v, sz, &obj4))
                            {
                                printf("An unexpected item was found at %d.", __LINE__);
                                DumpShorts("unexpected", p, sz);
                                result = 1;
                            };
                        };

                        free(p);
                        free(v);
                    };
                };
            };

            one = one->next;
        };

        mco_trans_commit(t);
    };

    /* btree */
    if ((rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK)
    {

        Obj3_by_tree_index_cursor(t, &c);
        mco_cursor_first(t, &c);
        i = 0;

        do
        {
            wchar_t buf[1024];

            while ((arr[i]->flags &OBJ3) == 0 && i < line_cnt)i++;
            if (i >= line_cnt)
            {
                printf("The btree list is too long at %d.", __LINE__);
                result = 1;
                break;
            };

            if (arr[i]->line_len == 0)
            {
                i++;
                continue;
            };

            Obj3_from_cursor(t, &c, &obj3);

            Obj3_line_get(&obj3, buf, sizeof(buf) / sizeof(buf[0]));

            if (0 != memcmp(buf, arr[i]->line, sizeof(buf) > arr[i]->line_len ? arr[i]->line_len: sizeof(buf)))
            {

                printf("The items are not equal in a btree index at %d. No:%d(%d).", __LINE__, i, arr[i]->line_no);
                DumpShorts("expected", arr[i]->line, (uint2)((sizeof(buf) > arr[i]->line_len ? arr[i]->line_len: sizeof
                           (buf)) / sizeof(wchar_t)));
                DumpShorts("   found", buf, (uint2)((sizeof(buf) > arr[i]->line_len ? arr[i]->line_len: sizeof(buf)) /
                           sizeof(wchar_t)));
                result = 1;
            };

            i++;

        }
        while (MCO_S_OK == (rc = mco_cursor_next(t, &c)))
            ;

        for (; i < line_cnt; i++)
        {
            if ((arr[i]->flags &OBJ3) != 0)
            {
                printf("The btree list is too short at %d.", __LINE__);
                result = 1;
                break;
            };
        };

        Obj5_by_tree_index_cursor(t, &c);
        mco_cursor_first(t, &c);
        i = 0;

        do
        {
            uint2 sz;
            wchar_t* p;

            while ((arr[i]->flags &OBJ5) == 0 && i < line_cnt)i++;
            if (i >= line_cnt)
            {
                printf("The btree list is too long at %d.", __LINE__);
                result = 1;
                break;
            };

            Obj5_from_cursor(t, &c, &obj5);
            Obj5_line_size(&obj5, &sz);

            if (sz == 0 && arr[i]->line_len == 0)
            {
                i++;
                continue;
            };

            p = (wchar_t*)malloc(sizeof(wchar_t)*(sz + 1));
            Obj5_line_get(&obj5, p, (uint2)(sz + 1), &sz);

            if ((arr[i]->line_len != sz* sizeof(wchar_t)) || (0 != memcmp(p, arr[i]->line, sz* sizeof(wchar_t))))
            {

                printf("The items are not equal in a btree index at %d. No:%d(%d).", __LINE__, i, arr[i]->line_no);
                DumpShorts("expected", arr[i]->line, (uint2)(arr[i]->line_len / sizeof(wchar_t)));
                DumpShorts("   found", p, sz);
                result = 1;
            };

            free(p);

            i++;

        }
        while (MCO_S_OK == (rc = mco_cursor_next(t, &c)))
            ;

        for (; i < line_cnt; i++)
        {
            if ((arr[i]->flags &OBJ3) != 0)
            {
                printf("The btree list is too short at %d.", __LINE__);
                result = 1;
                break;
            };
        };

        mco_trans_commit(t);
    }
    else
    {
        printf("Can't open a transaction. Error code: %d\n", rc);
    };

    free(arr);

    return result;
};

int main(int argc, char** argv)
{

    MCO_RET rc;
    int res = 0;
    mco_runtime_info_t info;
    void* start_mem;

    if (argc < 2)
    {
        fprintf(stdout, "\nUsage: %s <unicode_file.txt>\n", argv[0]);
        return 1;
    };

    mco_get_runtime_info(&info);
    if (info.mco_shm_supported)
    {
        start_mem = (void*)ADDRESS;
        printf("Shared memory version.\n");
        fflush(stdout);
    }
    else
    {
        start_mem = malloc(DATABASE_SIZE);
        if (!start_mem)
        {
            printf("Couldn't allocated memory\n");
            exit(1);
        }
        printf("Non-shared memory version.\n");
        fflush(stdout);
    };

    /* initialize eXtremeDB runtime */
    mco_runtime_start();

    /* setup fatal error handler */
    mco_error_set_handler(&errhandler);

    /* create the database */
    printf("Initializing a database: ");
    fflush(stdout);

    if (MCO_S_OK != (rc = mco_db_open(dbname, prjdb_get_dictionary(), start_mem, DATABASE_SIZE, PAGE_SIZE)))
    {
        printf("Unable to open a database. Error code: %d\n", rc);
        fflush(stdout);
        return 1;
    };
    printf("Done\n");
    fflush(stdout);

    /* Connect to the database */
    printf("Connecting to the database: ");
    fflush(stdout);
    if (MCO_S_OK != (rc = mco_db_connect(dbname, &db)))
    {

        printf("Unable to connect to the database. Error code: %d\n", rc);
        fflush(stdout);
        return 1;
    };
    printf("Done\n");
    fflush(stdout);

    printf("Loading file: ");
    fflush(stdout);
    if (LoadFile(argv[1]))
    {
        printf("Failed\n");
        fflush(stdout);
        res =  - 1;
    }
    else
    {
        printf("Done\n");
        fflush(stdout);

        printf("Checking content: ");
        fflush(stdout);
        if (CheckContent())
        {
            printf("Failed\n");
            res =  - 1;
        }
        else
        {
            printf("Done\n");
        }
        fflush(stdout);

        printf("Checking sort order: ");
        fflush(stdout);
        if (CheckSortOrder())
        {
            printf("Failed\n");
            res =  - 1;
        }
        else
        {
            printf("Done\n");
        }
        fflush(stdout);
    };

    printf("Stopping the database:");
    fflush(stdout);

    /* disconnect from the database */
    if (MCO_S_OK != (rc = mco_db_disconnect(db)))
    {
        printf("mco_db_disconnect( db ) = %d\n", rc);
    };

    /* Close and shutdown the database */
    mco_db_close(dbname);

    /* Shutdown eXtremeDB engine */
    mco_runtime_stop();
    free(start_mem);

    printf("Done\n\n");
    fflush(stdout);
	getchar();
    return res;

};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -