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

📄 xmltest.c

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

/* This samples demonstartes export of the eXtremeDB data to XML
 * The following xml-related interfaces are demonstrated:
 *
 * mco_xml_get_policy();
 * mco_xml_set_policy();
 * <class_name>_xml_get();
 * <class_name>_xml_schema();
 */

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

#include "XMLtestDB.h"

#ifdef MCO_PLATFORM_X64
    #define MCO_OID(hi,lo) ((((uint8)hi)<<32)+(uint4)lo)
#else //MCO_PLATFORM_X64
    #ifdef MCO_CFG_QUAD_STRUCT
        #define MCO_OID(hi,lo) {hi,lo}
    #else 
        #define MCO_OID(hi,lo) {(uint8)hi<<32+(uint8)lo}
    #endif 
#endif //MCO_PLATFORM_X64


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];

//int  output_to = 0;
int output_to = 1;

#if defined (_VXWORKS)
    #define OUT_PATH(arg) ("/tgtsvr/"arg)
    char xml_name[] = "/tgtsvr/output.xml";
#elif defined (_WIN32_WCE)
    #define OUT_PATH(arg) (".\\"arg)
    char xml_name[] = "output.xml";
#else 
    #define OUT_PATH(arg) ("./"arg)
    char xml_name[] = "output.xml";
#endif 

const int MAP_ADDRESS = 0x20000000;

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

void _SH_(void)
{

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

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

    printf("\nUsage: xml [-f]\n");
    printf("%s\neXtremeDB runtime version %d.%d, build %d\n%s\n", text, MCO_COMP_VER_MAJOR, MCO_COMP_VER_MINOR,
           MCO_COMP_BUILD_NUM, text1);

    if (output_to)
    {
        printf("\nOutput to file \"%s\"\n\nPress Enter to start", xml_name);
    }
    else
    {
        printf("\nConsole output\n\nPress Enter to start");
    }
    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)
{

    if (!stream_handle)
    {
        unsigned i;
        stream_handle = 0;
        for (i = 0; i < nbytes; i++)
        {
            printf("%c", ((char*)from)[i]);
        }
    }
    else
    {
        FILE* f = (FILE*)stream_handle;
        unsigned i;
        for (i = 0; i < nbytes; i++)
        {
            char c = ((char*)from)[i];
            if (c == 0)
            {
                c = c;
            }
            fprintf(f, "%c", c);
        }
    }
    return 0;
}


static int insert(mco_db_h db)
{

    MCO_RET rc;
    mco_trans_h t;
    Person p_obj;
    Children c_obj;
    Dog d_obj;

    Residence res;
    Address adr;
    Country cnt;
    Date dat;
    Phone pho;
    Office off;

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

    #include "XMLpers1.c"
    #include "XMLpers2.c"
    #include "XMLpers3.c"

    {
         /* Child # 1 */
        XMLtestDB_oid fat = 
        {
            "XXIV-CE", MCO_OID(123456789ul, 87654321ul)
        };
        XMLtestDB_oid mot = 
        {
            "XX-ABCD", MCO_OID(88888888ul, 99999999ul)
        };

        rc = Children_new(t, &c_obj);
        if (rc)
        {
            goto err;
        }
        Children_father_put(&c_obj, &fat);
        Children_mother_put(&c_obj, &mot);
        Children_name_put(&c_obj, "Leny", 4);
        Children_height_put(&c_obj, (float)1.67);
        Children_weight_put(&c_obj, 67.5);
        Children_age_put(&c_obj, 18);
    }
    {
         /* Child # 2 */
        XMLtestDB_oid fat = 
        {
            "12345", MCO_OID(123454321ul, 87654322ul)
        };
        XMLtestDB_oid mot = 
        {
            "XX-ABCD", MCO_OID(88888888ul, 99999999ul)
        };

        rc = Children_new(t, &c_obj);
        if (rc)
        {
            goto err;
        }
        Children_father_put(&c_obj, &fat);
        Children_mother_put(&c_obj, &mot);
        Children_name_put(&c_obj, "Elias", 5);
        Children_height_put(&c_obj, (float)1.82);
        Children_weight_put(&c_obj, 87.5);
        Children_age_put(&c_obj, 27);
    }

    {
        XMLtestDB_oid own = 
        {
            "XX-SX", MCO_OID(123456789ul, 87654323ul)
        };
        {
             /* Dog # 1 */
            rc = Dog_new(t, &d_obj);
            if (rc)
            {
                goto err;
            }
            Dog_name_put(&d_obj, "Misty", 5);
            Dog_owner_put(&d_obj, &own);
        }
        {
             /* Dog # 2 */
            XMLtestDB_oid own = 
            {
                "XX-SX", MCO_OID(123456789ul, 87654323ul)
            };

            rc = Dog_new(t, &d_obj);
            if (rc)
            {
                goto err;
            }
            Dog_name_put(&d_obj, "Grrrr", 5);
            Dog_owner_put(&d_obj, &own);
        }
        {
             /* Dog # 3 */

            XMLtestDB_oid own = 
            {
                "XX-ABCD", MCO_OID(88888888ul, 99999999ul)
            };
            rc = Dog_new(t, &d_obj);
            if (rc)
            {
                goto err;
            }
            Dog_name_put(&d_obj, "Clumzy", 6);
            Dog_owner_put(&d_obj, &own);
        }
        {
             /* Dog # 4 */
            XMLtestDB_oid own = 
            {
                "XXIV-AB", MCO_OID(123456789ul, 87654321ul)
            };
            rc = Dog_new(t, &d_obj);
            if (rc)
            {
                goto err;
            }
            Dog_name_put(&d_obj, "Bibo", 4);
            Dog_owner_put(&d_obj, &own);
        }
        {
             /* Dog # 5 */
            XMLtestDB_oid own = 
            {
                "XXIV-AB", MCO_OID(123456789ul, 87654321ul)
            };
            rc = Dog_new(t, &d_obj);
            if (rc)
            {
                goto err;
            }
            Dog_name_put(&d_obj, "Tank", 4);
            Dog_owner_put(&d_obj, &own);
        }
    }

    rc = mco_trans_commit(t);
    return rc;

    err: rc = mco_trans_rollback(t);
    return rc;
}


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

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

⌨️ 快捷键说明

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