微对象的初入(3)

1.1.3.2 方法实现

A. 错误处理

 1 #pragma region internal function (error)
 2 POB_RESULT_T _ob_new_error(POB_RESULT_T* presult, bool result, int code, char* msg)
 3 {
 4     if ((*presult) == NULL)
 5     {
 6         (*presult) = (POB_RESULT_T)malloc(sizeof(OB_RESULT_T));
 7         if ((*presult) == NULL)
 8         {
 9             return NULL;
10         }
11     }
12     (*presult)->code = code;
13     (*presult)->msg = msg;
14     (*presult)->result = result;
15     return (*presult);
16 }
17 
18 void _ob_set_error(POB_RESULT_T presult, bool result, int code, char* msg)
19 {
20     if (presult == NULL)
21     {
22         return;
23     }
24     presult->code = code;
25     presult->msg = msg;
26     presult->result = result;
27     return;
28 }
29 #pragma endregion // internal function (error)

B.KEY => VALUE 对处理

#pragma region internal function Create BASE TYPE
OB_STRING_T _new_name(POB_RESULT_T presult, OB_STRING_T name, OB_UINT_T len)
{
    if (name == NULL || name[0] == 0)
    {
        return NULL;
    }
    else
    {
        OB_STRING_T p = (OB_STRING_T)malloc(len * sizeof(unsigned char) + 1);
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, len * sizeof(unsigned char) + 1);
        memcpy(p, name, len);
        return p;
    }
}
OB_STRING_T _new_value_null(POB_RESULT_T presult)
{
    OB_STRING_T p = (OB_STRING_T)malloc(sizeof(unsigned char));
    if (p == NULL)
    {
        _ob_set_error(presult, false, 1, "malloc failed.");
        return false;
    }
    (*p) = 0;
    return p;
}
OB_STRING_T _new_value_bool(POB_RESULT_T presult)
{
    bool* p = (bool*)malloc(sizeof(bool));
    if (p == NULL)
    {
        _ob_set_error(presult, false, 1, "malloc failed.");
        return false;
    }
    (*p) = true;
    return p;
}
OB_STRING_T _new_value_integer(POB_RESULT_T presult)
{
    int* p = (int*)malloc(sizeof(int));
    if (p == NULL)
    {
        _ob_set_error(presult, false, 1, "malloc failed.");
        return NULL;
    }
    (*p) = 0;
    return (OB_STRING_T)p;
}
OB_STRING_T _new_value_double(POB_RESULT_T presult)
{
    double* p = (double*)malloc(sizeof(double));
    if (p == NULL)
    {
        _ob_set_error(presult, false, 1, "malloc failed.");
        return NULL;
    }
    (*p) = 0.0;
    return (OB_STRING_T)p;
}
OB_STRING_T _new_value_string(POB_RESULT_T presult, OB_UINT_T len)
{
    OB_STRING_T p = (OB_STRING_T)malloc(len * sizeof(unsigned char) + 1);
    if (p == NULL)
    {
        _ob_set_error(presult, false, 1, "malloc failed.");
        return NULL;
    }
    memset(p, 0x00, len * sizeof(unsigned char) + 1);
    return (OB_STRING_T)p;
}

OB_UINT_T _get_array_bytes(enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count)
{
    switch (type)
    {
        case ENUM_OB_ENDOBJ_NULL:
        {
            return count * sizeof(unsigned char);
        }
        break;
        case ENUM_OB_ENDOBJ_BOOL:
        {
            return count * sizeof(bool);
        }
        break;
        case ENUM_OB_ENDOBJ_INTEGER:
        {
            return count * sizeof(int);
        }
        break;
        case ENUM_OB_ENDOBJ_DOUBLE:
        {
            return count * sizeof(double);
        }
        //case ENUM_OB_ENDOBJ_STRING:
        //{
        //    return count * sizeof(unsigned char);
        //}
        default:
        {
            return 0;
        }
    }
}

OB_STRING_T _new_value_array(POB_RESULT_T presult, enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count, OB_UINT_T* len)
{
    (*len) = _get_array_bytes(type, count);
    switch (type)
    {
    case ENUM_OB_ENDOBJ_NULL:
    {
        void* p = (void*)malloc((*len));
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, (*len));
        return (OB_STRING_T)p;
    }
    break;
    case ENUM_OB_ENDOBJ_BOOL:
    {
        bool* p = (bool*)malloc((*len));
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, (*len));
        return (OB_STRING_T)p;
    }
    break;
    case ENUM_OB_ENDOBJ_INTEGER:
    {
        int* p = (int*)malloc((*len));
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, (*len));
        return (OB_STRING_T)p;
    }
    break;
    case ENUM_OB_ENDOBJ_DOUBLE:
    {
        double* p = (double*)malloc((*len));
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, (*len));
        return (OB_STRING_T)p;
    }
    //case ENUM_OB_ENDOBJ_STRING:
    //{
    //    OB_STRING_T p = (OB_STRING_T)malloc((*len));
    //    if (p == NULL)
    //    {
    //        _ob_set_error(presult, false, 1, "malloc failed.");
    //        return NULL;
    //    }
    //    memset(p, 0x00, (*len));
    //    return p;
    //}
    default:
        return NULL;
    }
}

OB_ARRAY_STRING_T _new_value_array_string(POB_RESULT_T presult, OB_ARRAY_STRING_T data, OB_UINT_T count)
{
    OB_ARRAY_STRING_T p = NULL;
    if (count > 0)
    {
        p = (OB_STRING_T*)malloc(sizeof(OB_STRING_T)*count);
        if (p == NULL)
        {
            _ob_set_error(presult, false, 1, "malloc failed.");
            return NULL;
        }
        memset(p, 0x00, sizeof(OB_STRING_T)*count);
        for (OB_UINT_T i = 0; i < count; i++)
        {
            OB_STRING_T one = ((OB_STRING_T*)data)[i];
            OB_UINT_T len = (OB_UINT_T)strlen((char*)one);
            if (len > 0)
            {
                memset(((OB_STRING_T*)p)[i], 0x00, len + 1);
                memcpy(((OB_STRING_T*)p)[i], ((OB_STRING_T*)data)[i], len);
            }
        }
    }
    return p;
}

#pragma endregion internal function Create BASE TYPE

C.内存释放

#pragma region internal ob common function
void ob_endobj_free(POB_ENDOBJ_T* ppobj)
{
    if (ppobj == NULL || (*ppobj) == NULL)
    {
        return ;
    }
    if ((*ppobj)->name != NULL)
    {
        free((*ppobj)->name);
        (*ppobj)->name = NULL;
    }
    if ((*ppobj)->value != NULL)
    {
        free((*ppobj)->value);
        (*ppobj)->value = NULL;
    }
    free((*ppobj));
    (*ppobj) = NULL;
}

void ob_node_free(POB_TREE_NODE_T* ppnode)
{
    if (ppnode == NULL || (*ppnode) == NULL)
    {
        return ;
    }
    POB_TREE_NODE_T pnode = (*ppnode)->child;

    while (pnode != NULL)
    {
        POB_TREE_NODE_T pchild = pnode->child;
        if (pchild != NULL)
        {
            ob_node_free(&pchild);
        }
        else
        {
            ob_endobj_free(&pnode->obj);
            pnode = pnode->next;
        }
    }
}
#pragma endregion internal ob free function

D. 通用方法

 1 #pragma region // internal common function
 2 void create_rand_time(char* rand_time, int len)
 3 {
 4     struct __timeb64 tb;
 5     _ftime64_s(&tb); // C4996
 6 
 7     char strtime[16];
 8     memset(strtime, 0x00, sizeof(strtime));
 9 
10     struct tm t;
11     localtime_s(&t, &tb.time);
12     strftime(strtime, 14, "%Y%m%d%H%M%S", &t);
13     sprintf_s(rand_time, 18, "%s.%03d", strtime, tb.millitm);
14 }
15 
16 OB_UINT_T ob_type_size(enum ENUM_OB_ENDOBJ_TYPE type)
17 {
18     switch (type)
19     {
20     case ENUM_OB_ENDOBJ_NULL:
21         return sizeof(OB_NULL_T);
22     case ENUM_OB_ENDOBJ_BOOL:
23         return sizeof(OB_BOOL_T);
24     case ENUM_OB_ENDOBJ_INTEGER:
25         return sizeof(OB_INT_T);
26     case ENUM_OB_ENDOBJ_DOUBLE:
27         return sizeof(OB_DOUBLE_T);
28     case ENUM_OB_ENDOBJ_STRING:
29         return sizeof(OB_STRING_T);
30     }
31     return 0;
32 }
33 
34 OB_UINT_T ob_type_size_array(enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count )
35 {
36     switch (type)
37     {
38     case ENUM_OB_ENDOBJ_NULL:
39         return sizeof(OB_NULL_T)*count;
40     case ENUM_OB_ENDOBJ_BOOL:
41         return sizeof(OB_BOOL_T)*count;
42     case ENUM_OB_ENDOBJ_INTEGER:
43         return sizeof(OB_INT_T)*count;
44     case ENUM_OB_ENDOBJ_DOUBLE:
45         return sizeof(OB_DOUBLE_T)*count;
46     case ENUM_OB_ENDOBJ_STRING:
47         return sizeof(OB_STRING_T)*count;
48     }
49     return 0;
50 }
51 #pragma endregion // internal common function

 

posted on 2019-04-22 15:51  公羽随笔  阅读(76)  评论(0编辑  收藏  举报

导航