(file) Return to stringarray.c CVS log (file) (dir) Up to [OMI] / omi / base

 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           #include "strings.h"
27           #include "io.h"
28           
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                   self->data = Batch_Get(batch, sizeof(MI_Char*) * capacity);
41           
42                   if (!self->data)
43 mike  1.1             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               const MI_Char* str)
54           {
55               MI_Char* tmp;
56           
57               /* Enlarge data capacity if necessary */
58               if (self->size == self->capacity)
59                   return MI_RESULT_FAILED;
60           
61               /* Allocate new string */
62               tmp = (MI_Char*)Batch_Zdup(self->batch, str);
63           
64 mike  1.1     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               fputc('{', os);
80           
81               for (i = 0; i < self->size; i++)
82               {
83                   Fzprintf(os, MI_T("%s"), self->data[i]);
84           
85 mike  1.1         if (i + 1 != self->size)
86                       fprintf(os, ", ");
87               }
88           
89               fputc('}', os);
90           }

ViewCVS 0.9.2