tcc-stupidos/libtcc/utils/string.h

47 lines
No EOL
906 B
C

#ifndef LIBTCC_UTILS_STRING_H
# define LIBTCC_UTILS_STRING_H 1
# include <stddef.h>
# include <string.h>
/* copy a string and truncate it. */
static inline char *
pstrcpy(char *buf, size_t buf_size, const char *s)
{
char *q, *q_end;
int c;
if (buf_size > 0) {
q = buf;
q_end = buf + buf_size - 1;
while (q < q_end) {
c = *s++;
if (c == '\0')
break;
*q++ = c;
}
*q = '\0';
}
return buf;
}
/* strcat and truncate. */
static inline char *
pstrcat(char *buf, size_t buf_size, const char *s)
{
size_t len;
len = strlen(buf);
if (len < buf_size)
pstrcpy(buf + len, buf_size - len, s);
return buf;
}
static inline char *
pstrncpy(char *out, const char *in, size_t num)
{
memcpy(out, in, num);
out[num] = '\0';
return out;
}
#endif /* !LIBTCC_UTILS_STRING_H */