26char *
strnstr(
const char *haystack,
const char *needle,
size_t size )
29 const char *end, *giveup;
31 needlesize = strlen(needle);
33 if (needlesize == 0 || needlesize > size)
36 end = haystack + size;
38 giveup = end - needlesize + 1;
41 for (
const char *i = haystack; i != giveup; i++) {
45 for (j = i, k = needle; j != end && *k !=
'\0'; j++, k++) {
68char *
strcasestr(
const char *haystack,
const char *needle )
71 size_t hay_len = strlen(haystack);
72 size_t needle_len = strlen(needle);
75 while (hay_len >= needle_len) {
76 if (strncasecmp(haystack, needle, needle_len) == 0)
77 return (
char*)haystack;
96 size_t len =
MIN( strlen(s), n );
97 char *
new = (
char *) malloc (len + 1);
101 return (
char *) memcpy (
new, s, len);
110 return strcmp(*(
const char **) p1, *(
const char **) p2);
132int vasprintf(
char** strp,
const char* fmt, va_list ap )
138 n = vsnprintf( NULL, 0, fmt, ap1 );
143 *strp = malloc( n+1 );
147 return vsnprintf( *strp, n+1, fmt, ap );
178int scnprintf(
char* text,
size_t maxlen,
const char* fmt, ... )
187 n = vsnprintf( text, maxlen, fmt, ap );
189 return MIN( maxlen-1, (
size_t)n );
199int num2str(
char dest[NUM2STRLEN],
double n,
int decimals )
202 return snprintf( dest, NUM2STRLEN,
"%.*f", decimals, n );
204 return snprintf( dest, NUM2STRLEN,
205 _(
"%.0f,%03.0f,%03.0f,%03.*f"),
207 floor(fmod(floor(fabs(n/1e6)),1e3)),
208 floor(fmod(floor(fabs(n/1e3)),1e3)),
209 decimals, fmod(floor(fabs(n)),1e3) );
211 return snprintf( dest, NUM2STRLEN,
212 _(
"%.0f,%03.0f,%03.*f"),
214 floor(fmod(floor(fabs(n/1e3)),1e3)),
215 decimals, fmod(floor(fabs(n)),1e3) );
217 return snprintf( dest, NUM2STRLEN,
219 floor(n/1e3), decimals, fmod(floor(fabs(n)),1e3) );
220 return snprintf( dest, NUM2STRLEN,
"%.*f", decimals, n );
232 static char num2strU_buf[NUM2STRLEN];
233 num2str( num2strU_buf, n, decimals );
Header file with generic functions and naev-specifics.
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
int num2str(char dest[NUM2STRLEN], double n, int decimals)
Converts an electronic warfare value to a string.
int vasprintf(char **strp, const char *fmt, va_list ap)
Like vsprintf(), but it allocates a large-enough string and returns the pointer in the first argument...
char * strnstr(const char *haystack, const char *needle, size_t size)
A bounded version of strstr. Conforms to BSD semantics.
int strsort(const void *p1, const void *p2)
Sort function for sorting strings with qsort().
char * strcasestr(const char *haystack, const char *needle)
Finds a string inside another string case insensitively.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
int strsort_reverse(const void *p1, const void *p2)
Order-reversed version of strsort().
char * strndup(const char *s, size_t n)
Return a pointer to a new string, which is a duplicate of the string s (or, if necessary,...
const char * num2strU(double n, int decimals)
Unsafe version of num2str that uses an internal buffer. Every call overwrites the return value.