-
Notifications
You must be signed in to change notification settings - Fork 150
/
strings.h
99 lines (85 loc) · 3.21 KB
/
strings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/** @file src/os/strings.h OS-independent inclusion of string routines. */
#ifndef OS_STRINGS_H
#define OS_STRINGS_H
/*
* This header should define the following functions:
*
* strcasecmp - strcmp, but case insensitive
* strncasecmp - strncmp, but case insensitive
* snprintf
*
*/
#if defined(_MSC_VER)
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#if _MSC_VER < 1900
static int snprintf(char *str, size_t size, const char *format, ...)
{
va_list ap;
int ret;
va_start(ap, format);
ret = vsnprintf(str, size, format, ap);
va_end(ap);
return ret;
}
/**
* Almost POSIX compliant implementation of \c vsnprintf for VC compiler.
* The difference is in the value returned on output truncation. This
* implementation returns size whereas a POSIX implementation returns
* size or more (the number of bytes that would be written to str
* had size been sufficiently large excluding the terminating null byte).
*/
static int vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
int ret;
if (size == 0) return 0;
errno = 0;
ret = _vsnprintf(str, size, format, ap);
if (ret < 0) {
if (errno != ERANGE) {
/* There's a formatting error, better get that looked
* at properly instead of ignoring it. */
assert(0);
}
} else if ((size_t)ret < size) {
/* The buffer is big enough for the number of
* characers stored (excluding null), i.e.
* the string has been null-terminated. */
return ret;
}
/* The buffer is too small for _vsnprintf to write the
* null-terminator at its end and return size. */
str[size - 1] = '\0';
return (int)size;
}
#endif /* _MSC_VER < 1900 */
#elif !defined(__TINYC__)
#include <stdio.h>
#include <strings.h>
#include <stdarg.h>
#if defined(__MINGW32__) && defined(__STRICT_ANSI__)
#if 0 /* NOTE : disabled because it generates warnings when cross compiling
* for MinGW32 under linux */
int __cdecl __MINGW_NOTHROW strcasecmp (const char *, const char *);
int __cdecl __MINGW_NOTHROW strncasecmp (const char *, const char *, size_t);
char* __cdecl __MINGW_NOTHROW strdup (const char*) __MINGW_ATTRIB_MALLOC;
#endif
#endif /* __MINGW32__ && __STRICT_ANSI__ */
#if !defined(__MINGW32__) && defined(__GNUC__) && !defined(snprintf)
/* (v)snprintf is in fact C99, but we like to use it over (v)sprintf for the obvious reasons */
#if !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__)
extern int snprintf (char *__restrict __s, size_t __maxlen, __const char *__restrict __format, ...) __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
extern int vsnprintf (char *__restrict __s, size_t __maxlen, __const char *__restrict __format, va_list __arg) __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
#endif /* __APPLE__ */
#endif /* __GCC__ */
#if !defined(__MINGW32__) && defined(__GNUC__) && !defined(strdup) && !defined(__APPLE__) && !defined(TOS) && !defined(__FreeBSD__)
/* strdup is not ANSI-C, but our own implemention would only be slower */
extern char *strdup (__const char *__s);
#endif /* __GCC__ */
#endif /* _MSC_VER && !__TINYC__ */
#endif /* OS_STRINGS_H */