(file) Return to strings.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 "strings.h"
 26           
 27           size_t Strlcat(char* dest, const char* src, size_t size)
 28           {
 29               const char* start = src;
 30           
 31               if (size)
 32               {
 33                   char* end = dest + size - 1;
 34           
 35                   while (*dest && dest != end)
 36                       dest++;
 37           
 38                   while (*src && dest != end)
 39                       *dest++ = *src++;
 40           
 41                   *dest = '\0';
 42               }
 43 mike  1.1 
 44               while (*src)
 45                   src++;
 46           
 47               return src - start;
 48           }
 49           
 50           size_t Strlcpy(char* dest, const char* src, size_t size)
 51           {
 52               const char* start = src;
 53           
 54               if (size)
 55               {
 56                   char* end = dest + size - 1;
 57           
 58                   while (*src && dest != end)
 59                       *dest++ = *src++;
 60           
 61                   *dest = '\0';
 62               }
 63           
 64 mike  1.1     while (*src)
 65                   src++;
 66           
 67               return src - start;
 68           }
 69           
 70           size_t Zlcat(MI_Char* dest, const MI_Char* src, size_t size)
 71           {
 72               const MI_Char* start = src;
 73           
 74               if (size)
 75               {
 76                   MI_Char* end = dest + size - 1;
 77           
 78                   while (*dest && dest != end)
 79                       dest++;
 80           
 81                   while (*src && dest != end)
 82                       *dest++ = *src++;
 83           
 84                   *dest = '\0';
 85 mike  1.1     }
 86           
 87               while (*src)
 88                   src++;
 89           
 90               return src - start;
 91           }
 92           
 93           size_t Zlcpy(MI_Char* dest, const MI_Char* src, size_t size)
 94           {
 95               const MI_Char* start = src;
 96           
 97               if (size)
 98               {
 99                   MI_Char* end = dest + size - 1;
100           
101                   while (*src && dest != end)
102                       *dest++ = *src++;
103           
104                   *dest = '\0';
105               }
106 mike  1.1 
107               while (*src)
108                   src++;
109           
110               return src - start;
111           }
112           
113           size_t ZStrlcat(MI_Char* dest, const char* src, size_t size)
114           {
115               const char* start = src;
116           
117               if (size)
118               {
119                   MI_Char* end = dest + size - 1;
120           
121                   while (*dest && dest != end)
122                       dest++;
123           
124                   while (*src && dest != end)
125                       *dest++ = *src++;
126           
127 mike  1.1         *dest = '\0';
128               }
129           
130               while (*src)
131                   src++;
132           
133               return src - start;
134           }
135           
136           size_t ZStrlcpy(MI_Char* dest, const char* src, size_t size)
137           {
138               const char* start = src;
139           
140               if (size)
141               {
142                   MI_Char* end = dest + size - 1;
143           
144                   while (*src && dest != end)
145                       *dest++ = *src++;
146           
147                   *dest = '\0';
148 mike  1.1     }
149           
150               while (*src)
151                   src++;
152           
153               return src - start;
154           }
155           
156 mike  1.2 #if !defined(CONFIG_FAVORSIZE)
157           
158 mike  1.1 // The following defines string literals for the numbers 0 through 63. The
159           // first character is the length of the string. The subsequent characters
160           // are the string literal iteslf.
161           static const char* _numberStrings[] =
162           {
163               "\0010",
164               "\0011",
165               "\0012",
166               "\0013",
167               "\0014",
168               "\0015",
169               "\0016",
170               "\0017",
171               "\0018",
172               "\0019",
173               "\00210",
174               "\00211",
175               "\00212",
176               "\00213",
177               "\00214",
178               "\00215",
179 mike  1.1     "\00216",
180               "\00217",
181               "\00218",
182               "\00219",
183               "\00220",
184               "\00221",
185               "\00222",
186               "\00223",
187               "\00224",
188               "\00225",
189               "\00226",
190               "\00227",
191               "\00228",
192               "\00229",
193               "\00230",
194               "\00231",
195               "\00232",
196               "\00233",
197               "\00234",
198               "\00235",
199               "\00236",
200 mike  1.1     "\00237",
201               "\00238",
202               "\00239",
203               "\00240",
204               "\00241",
205               "\00242",
206               "\00243",
207               "\00244",
208               "\00245",
209               "\00246",
210               "\00247",
211               "\00248",
212               "\00249",
213               "\00250",
214               "\00251",
215               "\00252",
216               "\00253",
217               "\00254",
218               "\00255",
219               "\00256",
220               "\00257",
221 mike  1.1     "\00258",
222               "\00259",
223               "\00260",
224               "\00261",
225               "\00262",
226               "\00263",
227           };
228           
229 mike  1.2 #endif /* !defined(CONFIG_FAVORSIZE) */
230           
231 mike  1.1 const char* Uint32ToStr(char buf[11], MI_Uint32 x, size_t* size)
232           {
233               char* p;
234           
235 mike  1.2 #if !defined(CONFIG_FAVORSIZE)
236           
237 mike  1.1     if (x < 64)
238               {
239                   *size = _numberStrings[x][0];
240                   return &_numberStrings[x][1];
241               }
242           
243 mike  1.2 #endif /* !defined(CONFIG_FAVORSIZE)*/
244           
245 mike  1.1     p = &buf[10];
246               *p = '\0';
247           
248               do
249               {
250                   *--p = '0' + x % 10;
251               }
252               while (x /= 10);
253           
254               *size = &buf[10] - p;
255               return p;
256           }
257           
258           const char* Uint64ToStr(char buf[21], MI_Uint64 x, size_t* size)
259           {
260               char* p;
261           
262 mike  1.2 #if !defined(CONFIG_FAVORSIZE)
263           
264 mike  1.1     if (x < 64)
265               {
266                   *size = _numberStrings[x][0];
267                   return &_numberStrings[x][1];
268               }
269           
270 mike  1.2 #endif /* !defined(CONFIG_FAVORSIZE)*/
271           
272 mike  1.1     p = &buf[20];
273               *p = '\0';
274           
275               do
276               {
277                   *--p = '0' + x % 10;
278               }
279               while (x /= 10);
280           
281               *size = &buf[20] - p;
282               return p;
283           }
284           
285           #if (MI_CHAR_TYPE != 1)
286           const MI_Char* Uint32ToZStr(MI_Char buf[11], MI_Uint32 x, size_t* size)
287           {
288               MI_Char* p = &buf[10];
289               *p = '\0';
290           
291               do
292               {
293 mike  1.1         *--p = '0' + x % 10;
294               }
295               while (x /= 10);
296           
297               *size = &buf[10] - p;
298               return p;
299           }
300           #endif
301           
302           #if (MI_CHAR_TYPE != 1)
303           const MI_Char* Uint64ToZStr(MI_Char buf[21], MI_Uint64 x, size_t* size)
304           {
305               MI_Char* p = &buf[20];
306               *p = '\0';
307           
308               do
309               {
310                   *--p = '0' + x % 10;
311               }
312               while (x /= 10);
313           
314 mike  1.1     *size = &buf[20] - p;
315               return p;
316           }
317           #endif

ViewCVS 0.9.2