1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include "stringarray.h"
|
26 krisbash 1.3 #include <pal/strings.h>
27 #include <pal/format.h>
|
28 mike 1.1
29 MI_Result StringArray_Init(
30 StringArray* self,
31 size_t capacity,
32 Batch* batch)
33 {
34 self->batch = batch;
35 self->size = 0;
36 self->capacity = capacity;
37
38 if (capacity)
39 {
|
40 krisbash 1.3 self->data = Batch_Get(batch, sizeof(ZChar*) * capacity);
|
41 mike 1.1
42 if (!self->data)
43 return MI_RESULT_FAILED;
44 }
45 else
46 self->data = NULL;
47
48 return MI_RESULT_OK;
49 }
50
51 MI_Result StringArray_Add(
52 StringArray* self,
|
53 krisbash 1.3 const ZChar* str)
|
54 mike 1.1 {
|
55 krisbash 1.3 ZChar* tmp;
|
56 mike 1.1
57 /* Enlarge data capacity if necessary */
58 if (self->size == self->capacity)
59 return MI_RESULT_FAILED;
60
61 /* Allocate new string */
|
62 krisbash 1.3 tmp = (ZChar*)Batch_Tcsdup(self->batch, str);
|
63 mike 1.1
64 if (!tmp)
65 return MI_RESULT_FAILED;
66
67 /* Add to end of array */
68 self->data[self->size++] = tmp;
69
70 return MI_RESULT_OK;
71 }
72
73 void StringArray_Print(
74 const StringArray* self,
75 FILE* os)
76 {
77 size_t i;
78
|
79 krisbash 1.3 Ftprintf(os, PAL_T("{"));
|
80 mike 1.1
81 for (i = 0; i < self->size; i++)
82 {
|
83 krisbash 1.3 Ftprintf(os, PAL_T("%T"), tcs(self->data[i]));
|
84 mike 1.1
85 if (i + 1 != self->size)
|
86 krisbash 1.3 Ftprintf(os, PAL_T(", "));
|
87 mike 1.1 }
88
|
89 krisbash 1.3 Ftprintf(os, PAL_T("}"));
|
90 mike 1.1 }
|