微对象的初入(4)

E. 对象处理

  1 bool __stdcall ob_new_obj_null(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_NULL_T data)
  2 {
  3     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
  4     if (presult == NULL)
  5     {
  6         return false;
  7     }
  8     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
  9     if (pobj == NULL)
 10     {
 11         _ob_set_error(presult, false, 1, "malloc failed.");
 12         return false;
 13     }
 14     pobj->type = ENUM_OB_ENDOBJ_NULL;
 15     pobj->len = 0;
 16     pobj->name = _new_name(presult, name, len);
 17     pobj->value = _new_value_null(presult);
 18     pobj->value[0] = data;
 19     (*ppobj) = pobj;
 20     (*ppresult) = presult;
 21     return true;
 22 }
 23 
 24 bool __stdcall ob_new_obj_bool(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_BOOL_T data)
 25 {
 26     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
 27     if (presult == NULL)
 28     {
 29         return false;
 30     }
 31     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
 32     if (pobj == NULL)
 33     {
 34         _ob_set_error(presult, false, 1, "malloc failed.");
 35         return false;
 36     }
 37     pobj->type = ENUM_OB_ENDOBJ_BOOL;
 38     pobj->len = 1;
 39     pobj->name = _new_name(presult, name, len);
 40     pobj->value = _new_value_bool(presult);
 41     pobj->value[0] = data;
 42     (*ppobj) = pobj;
 43     (*ppresult) = presult;
 44     return true;
 45 }
 46 
 47 bool __stdcall ob_new_obj_integer(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_INT_T data)
 48 {
 49     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
 50     if (presult == NULL)
 51     {
 52         return false;
 53     }
 54     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
 55     if (pobj == NULL)
 56     {
 57         _ob_set_error(presult, false, 1, "malloc failed.");
 58         return false;
 59     }
 60     pobj->type = ENUM_OB_ENDOBJ_INTEGER;
 61     pobj->len = sizeof(int);
 62     pobj->name = _new_name(presult, name, len);
 63     pobj->value = _new_value_integer(presult);
 64     memcpy(pobj->value, &data, sizeof(OB_INT_T));
 65     (*ppobj) = pobj;
 66     (*ppresult) = presult;
 67     return true;
 68 }
 69 
 70 bool __stdcall ob_new_obj_double(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_DOUBLE_T data)
 71 {
 72     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
 73     if (presult == NULL)
 74     {
 75         return false;
 76     }
 77     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
 78     if (pobj == NULL)
 79     {
 80         _ob_set_error(presult, false, 1, "malloc failed.");
 81         return false;
 82     }
 83     pobj->type = ENUM_OB_ENDOBJ_DOUBLE;
 84     pobj->len = sizeof(double);
 85     pobj->name = _new_name(presult, name, len);
 86     pobj->value = _new_value_double(presult);
 87     memcpy(pobj->value, &data, sizeof(OB_DOUBLE_T));
 88     (*ppobj) = pobj;
 89     (*ppresult) = presult;
 90     return true;
 91 }
 92 
 93 bool __stdcall ob_new_obj_string(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_STRING_T data, OB_UINT_T datalen)
 94 {
 95     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
 96     if (presult == NULL)
 97     {
 98         return false;
 99     }
100     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
101     if (pobj == NULL)
102     {
103         _ob_set_error(presult, false, 1, "malloc failed.");
104         return false;
105     }
106     pobj->type = ENUM_OB_ENDOBJ_STRING;
107     pobj->len = len+1;
108     pobj->name = _new_name(presult, name, len);
109     pobj->value = _new_value_string(presult, datalen);
110     memcpy(pobj->value, data, datalen);
111     (*ppobj) = pobj;
112     (*ppresult) = presult;
113     return true;
114 }
115 
116 bool __stdcall ob_new_obj_array(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_ARRAY_T data, enum ENUM_OB_ENDOBJ_TYPE type, OB_UINT_T count)
117 {
118     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
119     if (presult == NULL)
120     {
121         return false;
122     }
123     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
124     if (pobj == NULL)
125     {
126         _ob_set_error(presult, false, 1, "malloc failed.");
127         return false;
128     }
129     OB_UINT_T arraylen_bytes = 0;
130     pobj->type = ENUM_OB_ENDOBJ_ARRAY;
131     pobj->value = _new_value_array(presult, type, count, &arraylen_bytes);
132     pobj->len = arraylen_bytes;
133     pobj->name = _new_name(presult, name, len);
134     if (pobj->value == NULL)
135     {
136         _ob_set_error(presult, false, 1, "malloc failed.");
137         return false;
138     }
139     memcpy(pobj->value, data, ob_type_size_array(type, count));
140     (*ppobj) = pobj;
141     (*ppresult) = presult;
142     return true;
143 }
144 
145 bool __stdcall ob_new_obj_array_string(POB_ENDOBJ_T* ppobj, POB_RESULT_T* ppresult, OB_STRING_T name, OB_UINT_T len, OB_ARRAY_STRING_T data, OB_UINT_T count)
146 {
147     POB_RESULT_T presult = _ob_new_error(ppresult, true, 0, NULL);
148     if (presult == NULL)
149     {
150         return false;
151     }
152     POB_ENDOBJ_T pobj = (POB_ENDOBJ_T)malloc(sizeof(OB_ENDOBJ_T));
153     if (pobj == NULL)
154     {
155         _ob_set_error(presult, false, 1, "malloc failed.");
156         return false;
157     }
158     pobj->type = ENUM_OB_ENDOBJ_ARRAY;
159     pobj->value = (void*)_new_value_array_string(presult, data, count);
160     pobj->len = count;
161     pobj->name = _new_name(presult, name, len);
162     if (pobj->value == NULL)
163     {
164         _ob_set_error(presult, false, 1, "malloc failed.");
165         return false;
166     }
167     (*ppobj) = pobj;
168     (*ppresult) = presult;
169     return true;
170 }

猜你喜欢

转载自www.cnblogs.com/ousokou/p/10750658.html