(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           // The following defines string literals for the numbers 0 through 63. The
157           // first character is the length of the string. The subsequent characters
158           // are the string literal iteslf.
159           static const char* _numberStrings[] =
160           {
161               "\0010",
162               "\0011",
163               "\0012",
164               "\0013",
165               "\0014",
166               "\0015",
167               "\0016",
168               "\0017",
169 mike  1.1     "\0018",
170               "\0019",
171               "\00210",
172               "\00211",
173               "\00212",
174               "\00213",
175               "\00214",
176               "\00215",
177               "\00216",
178               "\00217",
179               "\00218",
180               "\00219",
181               "\00220",
182               "\00221",
183               "\00222",
184               "\00223",
185               "\00224",
186               "\00225",
187               "\00226",
188               "\00227",
189               "\00228",
190 mike  1.1     "\00229",
191               "\00230",
192               "\00231",
193               "\00232",
194               "\00233",
195               "\00234",
196               "\00235",
197               "\00236",
198               "\00237",
199               "\00238",
200               "\00239",
201               "\00240",
202               "\00241",
203               "\00242",
204               "\00243",
205               "\00244",
206               "\00245",
207               "\00246",
208               "\00247",
209               "\00248",
210               "\00249",
211 mike  1.1     "\00250",
212               "\00251",
213               "\00252",
214               "\00253",
215               "\00254",
216               "\00255",
217               "\00256",
218               "\00257",
219               "\00258",
220               "\00259",
221               "\00260",
222               "\00261",
223               "\00262",
224               "\00263",
225           };
226           
227           const char* Uint32ToStr(char buf[11], MI_Uint32 x, size_t* size)
228           {
229               char* p;
230           
231               if (x < 64)
232 mike  1.1     {
233                   *size = _numberStrings[x][0];
234                   return &_numberStrings[x][1];
235               }
236           
237               p = &buf[10];
238               *p = '\0';
239           
240               do
241               {
242                   *--p = '0' + x % 10;
243               }
244               while (x /= 10);
245           
246               *size = &buf[10] - p;
247               return p;
248           }
249           
250           const char* Uint64ToStr(char buf[21], MI_Uint64 x, size_t* size)
251           {
252               char* p;
253 mike  1.1 
254               if (x < 64)
255               {
256                   *size = _numberStrings[x][0];
257                   return &_numberStrings[x][1];
258               }
259           
260               p = &buf[20];
261               *p = '\0';
262           
263               do
264               {
265                   *--p = '0' + x % 10;
266               }
267               while (x /= 10);
268           
269               *size = &buf[20] - p;
270               return p;
271           }
272           
273           #if (MI_CHAR_TYPE != 1)
274 mike  1.1 const MI_Char* Uint32ToZStr(MI_Char buf[11], MI_Uint32 x, size_t* size)
275           {
276               MI_Char* p = &buf[10];
277               *p = '\0';
278           
279               do
280               {
281                   *--p = '0' + x % 10;
282               }
283               while (x /= 10);
284           
285               *size = &buf[10] - p;
286               return p;
287           }
288           #endif
289           
290           #if (MI_CHAR_TYPE != 1)
291           const MI_Char* Uint64ToZStr(MI_Char buf[21], MI_Uint64 x, size_t* size)
292           {
293               MI_Char* p = &buf[20];
294               *p = '\0';
295 mike  1.1 
296               do
297               {
298                   *--p = '0' + x % 10;
299               }
300               while (x /= 10);
301           
302               *size = &buf[20] - p;
303               return p;
304           }
305           #endif

ViewCVS 0.9.2