~ubuntu-branches/ubuntu/trusty/sblim-sfcb/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * value.c
 *
 * © Copyright IBM Corp. 2005, 2007
 *
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 *
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Author:        Frank Scheffler
 * Contributions: Adrian Schuur <schuur@de.ibm.com>
 *
 * Description:
 *
 * CMPIValue implementation.
 *
*/


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

#include "native.h"
#include "constClass.h"
#include "qualifier.h"
#include "cmpidtx.h"
#include "instance.h"
#include "objectpath.h"

void sfcb_native_release_CMPIValue(CMPIType type, CMPIValue * val)
{
   switch (type) {

   case CMPI_instance:
      CMRelease(val->inst);
      break;

   case CMPI_class:
      CMRelease(val->inst);
      break;
      
   case CMPI_qualifierDecl:
      CMRelease((CMPIQualifierDecl*)val->dataPtr.ptr);
      break;      

   case CMPI_ref:
      CMRelease(val->ref);
      break;

   case CMPI_args:
      CMRelease(val->args);
      break;

   case CMPI_filter:
      CMRelease(val->filter);
      break;

   case CMPI_enumeration:
      CMRelease(val->Enum);
      break;

   case CMPI_string:
      CMRelease(val->string);
      break;

   case CMPI_chars:
      free(val->chars);
      break;

   case CMPI_dateTime:
      CMRelease(val->dateTime);
      break;

   default:
      if (type & CMPI_ARRAY) {
         CMRelease(val->array);
      }
   }
}


CMPIValue sfcb_native_clone_CMPIValue(const CMPIType type,
                                 const CMPIValue * val, CMPIStatus * rc)
{
   CMPIValue v;
   CMPIConstClass *cl;

   if (type & CMPI_ENC) {

      switch (type) {

      case CMPI_instance:
         v.inst = CMClone(val->inst, rc);
         break;

      case CMPI_class:
         cl=(CMPIConstClass*)val->inst;
         v.inst = (CMPIInstance*)CMClone(cl, rc);
         break;
         
      case CMPI_qualifierDecl:
      	v.dataPtr.ptr = CMClone((CMPIQualifierDecl*)val->dataPtr.ptr, rc);
      	break;

      case CMPI_ref:
         v.ref = CMClone(val->ref, rc);
         break;

      case CMPI_args:
         v.args = CMClone(val->args, rc);
         break;

      case CMPI_filter:
         v.filter = CMClone(val->filter, rc);
         break;

      case CMPI_enumeration:
         v.Enum = CMClone(val->Enum, rc);
         break;

      case CMPI_string:
         v.string = CMClone(val->string, rc);
         break;

      case CMPI_chars:
         v.chars = strdup(val->chars);
         CMSetStatus(rc, CMPI_RC_OK);
         break;

      case CMPI_dateTime:
         v.dateTime = CMClone(val->dateTime, rc);
         break;
         
      case CMPI_ptr:
         v = *val;
         break;         
      }

   }
   else if (type & CMPI_ARRAY) {

      v.array = CMClone(val->array, rc);
   }
   else {

      sfcb_setAlignedValue(&v, val, type);
      CMSetStatus(rc, CMPI_RC_OK);
   }

   return v;
}

//extern CMPIString *__oft_toString(CMPIObjectPath * cop, CMPIStatus * rc);

char *sfcb_value2Chars(CMPIType type, CMPIValue * value)
{
   char str[256], *p;
   unsigned int size;
   CMPIString *cStr;
   
   str[0] = 0;
   if (value) {
     if (type & CMPI_ARRAY) {

     }
     else if (type & CMPI_ENC) {
       if (value->chars) {
	 /* non - null encapsulated value */
	 switch (type) {
	 case CMPI_instance:
	   break;

	 case CMPI_ref:
	   cStr = value->ref->ft->toString(value->ref, NULL); //  __oft_toString(value->ref, NULL);
	   return strdup((char *) cStr->hdl);
	   break;

	 case CMPI_args:
	   break;

	 case CMPI_filter:
	   break;

	 case CMPI_string:
	 case CMPI_numericString:
	 case CMPI_booleanString:
	 case CMPI_dateTimeString:
	 case CMPI_classNameString:
	   if (value->string->hdl) {
	     size = strlen((char *) value->string->hdl);
	     p = malloc(size + 8);
	     sprintf(p, "\"%s\"", (char *) value->string->hdl);
	     return p;
	   }
	   break;

	 case CMPI_dateTime:
	   cStr=CMGetStringFormat(value->dateTime,NULL);
	   size = strlen((char *) cStr->hdl);
	   p = malloc(size + 8);
	   sprintf(p, "\"%s\"", (char *) cStr->hdl);
	   return p;
	   break;
	 }
       }
     }
     else if (type & CMPI_SIMPLE) {

       switch (type) {
       case CMPI_boolean:
         return strdup(value->boolean ? "true" : "false");

       case CMPI_char16:
         break;
       }

     }
     else if (type & CMPI_INTEGER) {

       switch (type) {
       case CMPI_uint8:
         sprintf(str, "%u", value->uint8);
         return strdup(str);
       case CMPI_sint8:
         sprintf(str, "%d", value->sint8);
         return strdup(str);
       case CMPI_uint16:
         sprintf(str, "%u", value->uint16);
         return strdup(str);
       case CMPI_sint16:
         sprintf(str, "%d", value->sint16);
         return strdup(str);
       case CMPI_uint32:
         sprintf(str, "%u", value->uint32);
         return strdup(str);
       case CMPI_sint32:
         sprintf(str, "%d", value->sint32);
         return strdup(str);
       case CMPI_uint64:
         sprintf(str, "%llu", value->uint64);
         return strdup(str);
       case CMPI_sint64:
         sprintf(str, "%lld", value->sint64);
         return strdup(str);
       }

     }
     else if (type & CMPI_REAL) {

       switch (type) {
       case CMPI_real32:
         sprintf(str, "%g", value->real32);
         return strdup(str);
       case CMPI_real64:
         sprintf(str, "%g", value->real64);
         return strdup(str);
       }

   }
   }
   return strdup(str);
}

void sfcb_setAlignedValue(CMPIValue * target, const CMPIValue *source, 
			  CMPIType type)
{
  /* copies a value making sure the alignments are right */
  if (target && source) {
    memset(target,0,sizeof(CMPIValue));
    switch (type) {
    case CMPI_boolean:
    case CMPI_uint8:
    case CMPI_sint8:
      target->Byte = source->Byte;
      break;
    case CMPI_char16:
    case CMPI_uint16:
    case CMPI_sint16:
      target->Short = source->Short;
      break;
    case CMPI_uint32:
    case CMPI_sint32:
      target->Int = source->Int;
      break;
    case CMPI_real32:
      target->Float = source->Float;
      break;
    case CMPI_uint64:
    case CMPI_sint64:
      target->Long = source->Long;
      break;
    case CMPI_real64:
      target->Double = source->Double;
      break;
    default:
      if (type == CMPI_ptr) {
	*target = *source;
      } else {
	target->inst = source->inst;
      }
    }  
  }
}

static int extended_strcmp(char *s1, char *s2)
{
    if(s1 == NULL && s2 == NULL) return 0;
    if(s1 == NULL) return -1;
    if(s2 == NULL) return 1;
    
    return strcmp(s1, s2);
}

int sfcb_comp_CMPIValue(CMPIValue *val1, CMPIValue *val2, CMPIType type)
{
   int c;
   CMPIValue tempVal1, tempVal2;
   CMPIString *s1, *s2;
   
   /* check if we have null pointers for our ENC data types, the pointers */
   if(val1->array == NULL && val2->array == NULL) return 0; /*identical*/
   if(val1->array == NULL) return -1; /* val1 is less than val2 */
   if(val2->array == NULL) return 1;  /* val2 is less than val1 */
   
   if(type & CMPI_ARRAY) {
      c = (val1->array)->ft->getSize(val1->array, NULL);
      if(c != (val2->array)->ft->getSize(val2->array, NULL)) {
      	 /* member count not equal -> CMPIValue cannot be equal */
         return 1;
      }
      while(c--) {
         tempVal1 = ((val1->array)->ft->getElementAt(val1->array, c-1, NULL)).value;
         tempVal2 = ((val2->array)->ft->getElementAt(val2->array, c-1, NULL)).value;
         
         if(sfcb_comp_CMPIValue(&tempVal1, &tempVal2, type & ~CMPI_ARRAY)) {
            /* one element does not seem to be identical*/
            return 1;
         }
      }
      /* all tests passed - arrays identical */
      return 0;
   }
   if(val1 && val2) {
      switch(type) {
         case CMPI_boolean:
         case CMPI_uint8:
         case CMPI_sint8:
            return(val1->Byte - val2->Byte);
         case CMPI_char16:
         case CMPI_uint16:
         case CMPI_sint16:
            return(val1->Short - val2->Short);
         case CMPI_uint32:
         case CMPI_sint32:
            return(val1->Int - val2->Int);
         case CMPI_real32:
            return(val1->Float - val2->Float);
         case CMPI_uint64:
         case CMPI_sint64:
            return(val1->Long - val2->Long);
         case CMPI_real64:
            return(val1->Double - val2->Double);
         case CMPI_instance:
            return (instanceCompare(val1->inst, val2->inst));
         case CMPI_ref:
            return (objectpathCompare(val1->ref, val2->ref));
         case CMPI_string:
            return extended_strcmp(val1->string->ft->getCharPtr(val1->string, NULL),
                          val2->string->ft->getCharPtr(val2->string, NULL));
         case CMPI_dateTime:
            s1 = val1->dateTime->ft->getStringFormat(val1->dateTime, NULL);
            s2 = val2->dateTime->ft->getStringFormat(val2->dateTime, NULL);
            return extended_strcmp(s1->ft->getCharPtr(s1, NULL),
                          s2->ft->getCharPtr(s2, NULL));
         default:
            return 0;
      }
   }
   return 0;
}