(file) Return to strarr.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 <stdlib.h>
 26           #include <string.h>
 27           #include "strarr.h"
 28 krisbash 1.3 #include <pal/strings.h>
 29              #include "alloc.h"
 30 mike     1.1 
 31              char** StrArr()
 32              {
 33 krisbash 1.3     return (char**)PAL_Calloc(1, sizeof(char*));
 34 mike     1.1 }
 35              
 36 krisbash 1.3 _Use_decl_annotations_
 37              void StrArrCat(CharPtrPtr * self_, ConstCharPtr str)
 38 mike     1.1 {
 39                  size_t len;
 40 krisbash 1.3     CharPtrPtr self = *self_;
 41              
 42                  len = StrArrLen(self);
 43                  self = (CharPtrPtr)PAL_Realloc(self, sizeof(char*) * (len + 2));
 44              
 45                  if (self)
 46                  {
 47                      CharPtr s = PAL_Strdup(str);
 48                      self[len] = s;
 49                      self[len+1] = NULL;
 50                  }
 51              
 52              #ifdef _PREFAST_
 53              #pragma prefast (push)
 54              #pragma prefast (disable: 26036)
 55              #endif
 56                  /* 
 57                      OACR does not like NULL-ing buffers by using length above, instead it wants us to walk the entire array manually.
 58                      This isn't necessary here. The logic is correct. 
 59                  */
 60                  *self_ = self;
 61 krisbash 1.3 #ifdef _PREFAST_
 62              #pragma prefast (pop)
 63              #endif
 64              }
 65              
 66              _Use_decl_annotations_
 67              size_t StrArrLen(CharPtrPtr self)
 68              {
 69                  CharPtrPtr p = self;
 70              
 71                  while (*p)
 72                      p++;
 73              
 74                  return p - self;
 75              }
 76              
 77              _Use_decl_annotations_
 78              void StrArrFree(CharPtrPtr self)
 79              {
 80                  CharPtrPtr p = self;
 81                  while (*p)
 82 krisbash 1.3         PAL_Free(*p++);
 83              
 84                  PAL_Free(self);
 85              }
 86              
 87              wchar_t** WcsArr()
 88              {
 89                  return (wchar_t**)PAL_Calloc(1, sizeof(wchar_t*));
 90              }
 91              
 92              _Use_decl_annotations_
 93              void WcsArrCat(WcharPtrPtr * self_, ConstWcharPtr str)
 94              {
 95                  size_t len;
 96                  WcharPtrPtr self = *self_;
 97 mike     1.1     
 98 krisbash 1.3     len = WcsArrLen(self);
 99                  self = (WcharPtrPtr)PAL_Realloc(self, sizeof(wchar_t*) * (len + 2));
100 mike     1.1 
101                  if (self)
102                  {
103 krisbash 1.3         wchar_t* s = PAL_Wcsdup(str);
104 mike     1.1         self[len] = s;
105                      self[len+1] = NULL;
106                  }
107              
108 krisbash 1.3 #ifdef _PREFAST_
109              #pragma prefast (push)
110              #pragma prefast (disable: 26036)
111              #endif
112                  /* 
113                      OACR does not like NULL-ing buffers by using length above, instead it wants us to walk the entire array manually.
114                      This isn't necessary here. The logic is correct. 
115                  */
116 mike     1.1     *self_ = self;
117 krisbash 1.3 #ifdef _PREFAST_
118              #pragma prefast (pop)
119              #endif
120 mike     1.1 }
121              
122 krisbash 1.3 _Use_decl_annotations_
123              size_t WcsArrLen(WcharPtrPtr self)
124 mike     1.1 {
125 krisbash 1.3     WcharPtrPtr p = self;
126 mike     1.1 
127                  while (*p)
128                      p++;
129              
130                  return p - self;
131              }
132              
133 krisbash 1.3 _Use_decl_annotations_
134              void WcsArrFree(WcharPtrPtr self)
135 mike     1.1 {
136 krisbash 1.3     WcharPtrPtr p = self;
137 mike     1.1     while (*p)
138 krisbash 1.3         PAL_Free(*p++);
139 mike     1.1 
140 krisbash 1.3     PAL_Free(self);
141 mike     1.1 }
142 krisbash 1.3 
143              _Use_decl_annotations_
144              ZChar** CloneStringArray(
145                  const ZChar** data, 
146                  MI_Uint32 size,
147                  Batch* batch)
148              {
149                  size_t n;
150                  MI_Uint32 i;
151                  ZChar* ptr;
152                  ZChar** array;
153              
154                  /* Empty arrays are represented by NULL */
155                  if (!data || size == 0)
156                      return NULL;
157              
158                  /* Calculate space for pointer array */
159                  n = size * sizeof(ZChar*);
160              
161                  /* Calculate space for strings (add space for zero terminators) */
162                  for (i = 0; i < size; i++)
163 krisbash 1.3     {
164                      if (!data[i])
165                      {
166                          /* Null elements are illegal */
167                          return NULL;
168                      }
169              
170                      n += (Tcslen(data[i]) + 1) * sizeof(ZChar);
171                  }
172              
173                  /* Allocate memory */
174                  array = (ZChar**)BAlloc(batch, n, CALLSITE);
175              
176                  if (!array)
177                      return NULL;
178              
179                  ptr = (ZChar*)&array[size];
180              
181                  /* Copy the strings */
182                  for (i = 0; i < size; i++)
183                  {
184 krisbash 1.3         size_t count = Tcslen(data[i]) + 1;
185                      memcpy(ptr, data[i], count * sizeof(ZChar));
186                      array[i] = ptr;
187                      ptr += count;
188                  }
189              
190                  return array;
191              }
192              

ViewCVS 0.9.2