Skip to content

Commit 26bc17f

Browse files
committed
PlatformLinux: refactor of str*_s functions
1 parent 9a79f6f commit 26bc17f

File tree

1 file changed

+113
-18
lines changed

1 file changed

+113
-18
lines changed

src/Common/PlatformLinux.inl

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,127 @@ typedef dirent DirEntryType;
276276
#define lstrcpy strcpy
277277
#define stricmp strcasecmp
278278
#define strupr SDL_strupr
279-
inline bool strncpy_s(char * dest, size_t dst_size, const char * source, size_t num) {
280-
bool result = false;
281-
size_t len = 0;
282279

283-
if((dst_size - 1) >= num)
284-
len = num;
280+
inline int strcpy_s(char *dest, size_t num, const char *source)
281+
{
282+
if(!num)
283+
return 22;
284+
if(!dest)
285+
return 22;
286+
287+
if(!source)
288+
{
289+
dest[0] = '\0';
290+
return 22;
291+
}
292+
293+
size_t i;
294+
for(i = 0; i < num; i++)
295+
{
296+
if((dest[i] = source[i]) == '\0')
297+
return 0;
298+
}
299+
dest[0] = '\0';
300+
return 34;
301+
}
302+
303+
template <std::size_t num>
304+
inline int strcpy_s(char (&dest)[num], const char *source) { return strcpy_s(dest, num, source); }
305+
306+
inline int strncpy_s(char * dest, size_t dst_size, const char * source, size_t num)
307+
{
308+
if(!num)
309+
{
310+
if(dest && dst_size)
311+
*dest = 0;
312+
313+
return 0;
314+
}
315+
316+
if (!dest || !source || (0 == dst_size))
317+
return 22;
318+
319+
size_t i, end;
320+
if(num < dst_size)
321+
end = num;
285322
else
286-
len = dst_size - 1;
323+
end = dst_size - 1;
324+
325+
for(i = 0; i < end && source[i]; i++)
326+
dest[i] = source[i];
327+
328+
if(!source[i] || end == num)
329+
{
330+
dest[i] = '\0';
331+
return 0;
332+
}
333+
334+
dest[0] = '\0';
287335

288-
result = (NULL == strncpy(dest, source, len));
289-
dest[len] = 0;
336+
return 22;
337+
}
338+
339+
template <std::size_t dst_sz>
340+
inline int strncpy_s(char (&dest)[dst_sz], const char * source, size_t num) { return strncpy_s(dest, dst_sz, source, num); }
341+
342+
inline int strcat_s(char * dest, size_t num, const char * source)
343+
{
344+
if(!dest || (0 == num))
345+
return 22;
290346

291-
return result;
347+
if(!source)
348+
{
349+
dest[0] = '\0';
350+
return 22;
351+
}
352+
353+
size_t i, j;
354+
for(i = 0; i < num; i++)
355+
{
356+
if(dest[i] == '\0')
357+
{
358+
for(j = 0; (j + i) < num; j++)
359+
{
360+
if((dest[j + i] = source[j]) == '\0')
361+
return 0;
362+
}
363+
}
364+
}
365+
366+
dest[0] = '\0';
367+
return 34;
292368
}
293-
inline bool strncpy_s(char * dest, const char * source, size_t num) {
294-
bool result = false;
295369

296-
result = (NULL == strncpy(dest, source, num));
297-
dest[num] = 0;
370+
inline int strncat_s(char * dest, size_t num, const char * source, size_t count)
371+
{
372+
if (!dest || (0 == num))
373+
return 22;
374+
375+
if (!source)
376+
{
377+
dest[0] = '\0';
378+
return 22;
379+
}
380+
381+
size_t i, j;
382+
for(i = 0; i < num; i++)
383+
{
384+
if(dest[i] == '\0')
385+
{
386+
for(j = 0; (j + i) < num; j++)
387+
{
388+
if(j == count || (dest[j + i] = source[j]) == '\0')
389+
{
390+
dest[j + i] = '\0';
391+
return 0;
392+
}
393+
}
394+
}
395+
}
298396

299-
return result;
397+
dest[0] = '\0';
398+
return 34;
300399
}
301-
inline int strcpy_s(char *dest, const char *source) { return (int)(NULL == strcpy(dest, source)); }
302-
inline int strcpy_s(char *dest, size_t num, const char *source) { return (int)(NULL == strcpy(dest, source)); }
303-
inline int strcat_s(char * dest, size_t size, const char * source) { return (NULL == strcat(dest, source)); }
304-
inline int strncat_s(char * dest, size_t size, const char * source, size_t count) { return (NULL == strncat(dest, source, count)); }
305400

306401
#define _vsnprintf vsnprintf
307402
#define vsprintf_s(dest, size, format, args) vsprintf(dest, format, args)

0 commit comments

Comments
 (0)