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

📄 xmlload.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 C
字号:
/***************************************************************
 *                                                             *
 * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.   *
 *                                                             *
 ***************************************************************/

/* This samples demonstartes eXtremeDB XML input capabilities
 * The insert() function parses the xml file, creating the
 * database objects.
 *
 * The output() function writes all database in XML format to
 * standard output
 *
 */

#include <platform.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mco.h>

#include "XMLtestDB.h"

static const char* dbname = "XMLtestDB";
static const int SEGSZ = 1024 * 1024 * 1;
#ifndef MCO_PLATFORM_X64
    static const int PAGESIZE = 96;
#else 
    static const int PAGESIZE = 192;
#endif 


static int num_obj[3];

static FILE* file;
#define XML_TEXT_SIZE 10000						  /* size in bytes for xml buffer for 1 xml-object */
static char xml[XML_TEXT_SIZE];
static int ptr;

#if defined (_VXWORKS)
    char xml_name[] = "/tgtsvr/input.xml";
#elif defined (_WIN32_WCE)
    char xml_name[] = "input.xml";
#else 
    char xml_name[] = "input.xml";
#endif 

const int MAP_ADDRESS = 0x20000000;

/************************************************************/

void _SH_(void)
{

    char text[] = 
    {
        "\nThis samples demonstartes eXtremeDB XML load\n"
    };

    char text1[] = 
    {
        "Copyright (c) 2001-2007 McObject LLC. All Right Reserved.\n\n"
    };

    printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n\nPress Enter to start", text, MCO_COMP_VER_MAJOR,
           MCO_COMP_VER_MINOR, MCO_COMP_BUILD_NUM, text1);

    getchar();

}


/************************************************************/

void showStat(mco_db_h db, uint2 class_code)
{
    MCO_RET rc = 0;
    mco_trans_h t;
    mco_class_stat_t stat;

    mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    rc = mco_class_stat_get(t, class_code, &stat);
    mco_trans_commit(t);

    if (rc == MCO_S_OK)
    {
        printf("\n\tStatistics for the class with code %d:\n""\tNumber of objects:\t%ld\n"
               "\tTotal core pages used:\t%ld\n""\tTotal blob pages used:\t%ld\n""\tTotal core space used:\t%ld\n",
               class_code, stat.objects_num, stat.core_pages, stat.blob_pages, stat.core_space);
        num_obj[class_code - 1] = (int)stat.objects_num;
    }
}


void showMem(mco_db_h db)
{
    mco_puint totalpg, freepg;

    mco_db_free_pages(db, &freepg);
    mco_db_total_pages(db, &totalpg);

    printf("\n\tMemory Report:""\n\ttotal pages=%d (%dK)""\n\t free pages=%d (%dK)""\n\t used %dK\n", totalpg, totalpg*
           PAGESIZE / 1024, freepg, freepg* PAGESIZE / 1024, (totalpg - freepg)* PAGESIZE / 1024);
}


/************************************************************/

static int erase(mco_db_h db)
{
    mco_cursor_t csr;
    mco_trans_h t;
    Person p_obj;
    Children c_obj;
    Dog d_obj;
    int n;

    for (n = 0; n < num_obj[Dog_code - 1]; n++)
    {
        mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
        Dog_list_cursor(t, &csr);
        mco_cursor_first(t, &csr);
        Dog_from_cursor(t, &csr, &d_obj);
        Dog_delete(&d_obj);
        mco_trans_commit(t);
    }
    for (n = 0; n < num_obj[Children_code - 1]; n++)
    {
        mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
        Children_list_cursor(t, &csr);
        mco_cursor_first(t, &csr);
        Children_from_cursor(t, &csr, &c_obj);
        Children_delete(&c_obj);
        mco_trans_commit(t);
    }
    for (n = 0; n < num_obj[Person_code - 1]; n++)
    {
        mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
        Person_list_cursor(t, &csr);
        mco_cursor_first(t, &csr);
        Person_from_cursor(t, &csr, &p_obj);
        Person_delete(&p_obj);
        mco_trans_commit(t);
    }
    return 0;
}


/************************************************************/

int do_print(void* stream_handle, const void* from, unsigned nbytes)
{
    unsigned i;
    stream_handle = 0;
    for (i = 0; i < nbytes; i++)
    {
        printf("%c", ((char*)from)[i]);
    }
    return 0;
}


/************************************************************/

static const char xml_header1[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
static const char xml_header2[] = "<data>";
static const char xml_footer[] = "</data>";

static int insert(mco_db_h db)
{

    MCO_RET rc;
    mco_trans_h t;
    Person p_obj;
    Children c_obj;
    Dog d_obj;
    int class_code;
    int c;

    rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    if (rc)
    {
        return rc;
    }

    for (;;)
    {
         /* loop on xml-objects */
        /* skip all before class tag */
        do
        {
            c = getc(file);
        }
        while (c != '<' && c != EOF);
        if (c == EOF)
        {
            break;
        }
         /* finished */
        ptr = 1;
        xml[0] = '<';
        /* read class name */
        do
        {
            c = getc(file);
            xml[ptr++] = c;
        }
        while (c != '>' && c != EOF);
        if (c == EOF)
        {
            break;
        }
         /* finished */
        xml[ptr] = 0;
        if (strcmp(xml, "<Person>") == 0)
        {
            class_code = Person_code;
        }
        else if (strcmp(xml, "<Children>") == 0)
        {
            class_code = Children_code;
        }
        else if (strcmp(xml, "<Dog>") == 0)
        {
            class_code = Dog_code;
        }
        // skip the XML header

        else if (strcmp(xml, xml_header1) == 0)
        {
            continue;
        }
        else if (strcmp(xml, xml_header2) == 0)
        {
            continue;
        }
        else if (strcmp(xml, xml_footer) == 0)
        {
            continue;
        }

        else
        {
            printf("\nError - undefined class '%s'\n", xml);
            exit(3);
        }

        /* read xml-object */
        for (;;)
        {
            c = getc(file);
            if (c == EOF)
            {
                xml[ptr] = 0;
                printf("\nError - unexpected end of file: %s\n",  &xml[(ptr > 50) ? ptr - 50: 0]);
                exit(4);
            }
            xml[ptr++] = c;
            if (c == '>')
            {
                xml[ptr] = 0;
                if (class_code == Children_code)
                {
                    if (strcmp("</Children>", &xml[ptr - 11]) == 0)
                    {
                        break;
                    }
                }
                else if (class_code == Person_code)
                {
                    if (strcmp("</Person>", &xml[ptr - 9]) == 0)
                    {
                        break;
                    }
                }
                else if (class_code == Dog_code)
                {
                    if (strcmp("</Dog>", &xml[ptr - 6]) == 0)
                    {
                        break;
                    }
                }
            }
        }
        switch (class_code)
        {
            case Children_code:
                rc = Children_xml_create(t, xml, &c_obj);
                break;
            case Person_code:
                rc = Person_xml_create(t, xml, &p_obj);
                break;
            case Dog_code:
                rc = Dog_xml_create(t, xml, &d_obj);
                break;
        }
        if (rc != MCO_S_OK)
        {
            printf("ERR CREATE %d\n", rc);
            exit(0);
        }
        ptr = 0;
    } /* loop on xml-objects */

    rc = mco_trans_commit(t);
    return rc;
}


/************************************************************/

/* fatal error handler */
static void errhandler(int n)
{
    printf("\neXtremeDB runtime fatal error: %d", n);
    getchar();
    exit( - 1);
}


/************************************************************/

#include "mcoxml.h"

MCO_RET output(mco_db_h db)
{
    mco_trans_h t;
    Person p_obj;
    Children c_obj;
    Dog d_obj;
    MCO_RET rc;
    mco_cursor_t c;

    rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t);
    if (rc)
    {
        return rc;
    }

    rc = Person_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Person_from_cursor(t, &c, &p_obj);
        if (rc)
        {
            return rc;
        }
        rc = Person_xml_get(&p_obj, 0, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    rc = Children_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Children_from_cursor(t, &c, &c_obj);
        if (rc)
        {
            return rc;
        }
        rc = Children_xml_get(&c_obj, 0, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    rc = Dog_list_cursor(t, &c);
    if (rc)
    {
        return rc;
    }
    rc = mco_cursor_first(t, &c);
    if (rc)
    {
        return rc;
    }
    for (;;)
    {
        rc = Dog_from_cursor(t, &c, &d_obj);
        if (rc)
        {
            return rc;
        }
        rc = Dog_xml_get(&d_obj, 0, &do_print);
        if (rc)
        {
            return rc;
        }
        if (mco_cursor_next(t, &c) != MCO_S_OK)
        {
            break;
        }
    }

    rc = mco_trans_commit(t);
    return rc;
}


/************************************************************/

int main(int argc, char** argv)
{
    MCO_RET rc;
    mco_db_h db = 0;
    char* start_mem;
    mco_trans_h t;
    mco_xml_policy_t policy;

    mco_runtime_info_t info;

    _SH_();
    mco_get_runtime_info(&info);
    if (info.mco_shm_supported)
    {
        start_mem = (char*)MAP_ADDRESS;
    }
    else
    {
        start_mem = (char*)malloc(SEGSZ);
        if (!start_mem)
        {
            printf("Couldn't allocated memory\n");
            exit(1);
        }
    };

    file = fopen(xml_name, "r");

    if (file == 0)
    {
        printf("Cannot open file\n");
        exit(2);
    }

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

    rc = mco_runtime_start();
    rc = mco_db_open(dbname, XMLtestDB_get_dictionary(), start_mem, SEGSZ, (uint2)PAGESIZE);
    if (rc)
    {
        printf("\nerror creating database");
        if (!info.mco_shm_supported)
        {
            free(start_mem);
        }
        exit(1);
    }

    /* connect to the database, obtain a database handle */
    mco_db_connect(dbname, &db);
    printf(
           "\nDatabase created successfully, db handle = %d\n%dK allocated for the database\nDatabase pagesize is %d bytes\n\n", (int)db, SEGSZ / 1024, PAGESIZE);

    rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
    if (rc)
    {
        goto end;
    }

    if (MCO_E_UNSUPPORTED == (rc = mco_xml_get_policy(t, &policy)))
    {
        printf("\nXML is not supported!\n\n");
        goto end;
    }

    policy.text_coding = MCO_TEXT_ASCII; //BASE64;
    policy.blob_coding = MCO_TEXT_ASCII; //BASE64;
    policy.ignore_field = MCO_YES;

    mco_xml_set_policy(t, &policy);

    rc = mco_trans_commit(t);
    if (rc)
    {
        goto end;
    }

    printf("\n\nLoading XML from file...");
    rc = insert(db);
    printf("done\n\n");
    if (rc)
    {
        goto end;
    }

    printf("\n\nReading XML from the database to the standard output\n\n");
    rc = output(db);

    end: 
    /* disconnect from the database, db is no longer valid */
    mco_db_disconnect(db);
    /* destroy the db */
    mco_db_close(dbname);
    mco_runtime_stop();

    if (!info.mco_shm_supported)
    {
        free(start_mem);
    }

    printf("\nPress Enter key to exit");
    getchar();
    PROG_EXIT(0);

}

⌨️ 快捷键说明

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