naev 0.10.4
nstring.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "nstring.h"
14
15#include "log.h"
16
25#if !HAVE_STRNSTR
26char *strnstr( const char *haystack, const char *needle, size_t size )
27{
28 size_t needlesize;
29 const char *end, *giveup;
30
31 needlesize = strlen(needle);
32 /* We can give up if needle is empty, or haystack can never contain it */
33 if (needlesize == 0 || needlesize > size)
34 return NULL;
35 /* The pointer value that marks the end of haystack */
36 end = haystack + size;
37 /* The maximum value of i, because beyond this haystack cannot contain needle */
38 giveup = end - needlesize + 1;
39
40 /* i is used to iterate over haystack */
41 for (const char *i = haystack; i != giveup; i++) {
42 const char *j, *k;
43 /* j is used to iterate over part of haystack during comparison */
44 /* k is used to iterate over needle during comparison */
45 for (j = i, k = needle; j != end && *k != '\0'; j++, k++) {
46 /* Bail on the first character that doesn't match */
47 if (*j != *k)
48 break;
49 }
50 /* If we've reached the end of needle, we've found a match */
51 /* i contains the start of our match */
52 if (*k == '\0')
53 return (char*) i;
54 }
55 /* Fell through the loops, nothing found */
56 return NULL;
57}
58#endif /* !HAVE_STRNSTR */
59
67#if !HAVE_STRCASESTR
68char *strcasestr( const char *haystack, const char *needle )
69{
70 /* Get lengths. */
71 size_t hay_len = strlen(haystack);
72 size_t needle_len = strlen(needle);
73
74 /* Slow search. */
75 while (hay_len >= needle_len) {
76 if (strncasecmp(haystack, needle, needle_len) == 0)
77 return (char*)haystack;
78
79 haystack++;
80 hay_len--;
81 }
82
83 return NULL;
84}
85#endif /* !HAVE_STRCASESTR */
86
93#if !HAVE_STRNDUP
94char* strndup( const char *s, size_t n )
95{
96 size_t len = MIN( strlen(s), n );
97 char *new = (char *) malloc (len + 1);
98 if (new == NULL)
99 return NULL;
100 new[len] = '\0';
101 return (char *) memcpy (new, s, len);
102}
103#endif /* !HAVE_STRNDUP */
104
108int strsort( const void *p1, const void *p2 )
109{
110 return strcmp(*(const char **) p1, *(const char **) p2);
111}
112
116int strsort_reverse( const void *p1, const void *p2 )
117{
118 return strsort( p2, p1 );
119}
120
131#if !HAVE_VASPRINTF
132int vasprintf( char** strp, const char* fmt, va_list ap )
133{
134 int n;
135 va_list ap1;
136
137 va_copy( ap1, ap );
138 n = vsnprintf( NULL, 0, fmt, ap1 );
139 va_end( ap1 );
140
141 if (n < 0)
142 return -1;
143 *strp = malloc( n+1 );
144 if (strp == NULL )
145 return -1; /* Not that we'll check. We're Linux fans. We've never heard of malloc() failing. */
146
147 return vsnprintf( *strp, n+1, fmt, ap );
148}
149#endif /* !HAVE_VASPRINTF */
150
160#if !HAVE_ASPRINTF
161int asprintf( char** strp, const char* fmt, ... )
162{
163 int n;
164 va_list ap;
165
166 va_start( ap, fmt );
167 n = vasprintf( strp, fmt, ap );
168 va_end( ap );
169 return n;
170}
171#endif /* !HAVE_ASPRINTF */
172
178int scnprintf( char* text, size_t maxlen, const char* fmt, ... )
179{
180 int n;
181 va_list ap;
182
183 if (!maxlen)
184 return 0;
185
186 va_start( ap, fmt );
187 n = vsnprintf( text, maxlen, fmt, ap );
188 va_end( ap );
189 return MIN( maxlen-1, (size_t)n );
190}
191
199int num2str( char dest[NUM2STRLEN], double n, int decimals )
200{
201 if (n >= 1e12)
202 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
203 else if (n >= 1e9)
204 return snprintf( dest, NUM2STRLEN,
205 _("%.0f,%03.0f,%03.0f,%03.*f"),
206 floor(n/1e9),
207 floor(fmod(floor(fabs(n/1e6)),1e3)),
208 floor(fmod(floor(fabs(n/1e3)),1e3)),
209 decimals, fmod(floor(fabs(n)),1e3) );
210 else if (n >= 1e6)
211 return snprintf( dest, NUM2STRLEN,
212 _("%.0f,%03.0f,%03.*f"),
213 floor(n/1e6),
214 floor(fmod(floor(fabs(n/1e3)),1e3)),
215 decimals, fmod(floor(fabs(n)),1e3) );
216 else if (n >= 1e3)
217 return snprintf( dest, NUM2STRLEN,
218 _("%.0f,%03.*f"),
219 floor(n/1e3), decimals, fmod(floor(fabs(n)),1e3) );
220 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
221}
222
230const char* num2strU( double n, int decimals )
231{
232 static char num2strU_buf[NUM2STRLEN];
233 num2str( num2strU_buf, n, decimals );
234 return num2strU_buf;
235}
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition: naev.h:40
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
Definition: nstring.c:161
int num2str(char dest[NUM2STRLEN], double n, int decimals)
Converts an electronic warfare value to a string.
Definition: nstring.c:199
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...
Definition: nstring.c:132
char * strnstr(const char *haystack, const char *needle, size_t size)
A bounded version of strstr. Conforms to BSD semantics.
Definition: nstring.c:26
int strsort(const void *p1, const void *p2)
Sort function for sorting strings with qsort().
Definition: nstring.c:108
char * strcasestr(const char *haystack, const char *needle)
Finds a string inside another string case insensitively.
Definition: nstring.c:68
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
Definition: nstring.c:178
int strsort_reverse(const void *p1, const void *p2)
Order-reversed version of strsort().
Definition: nstring.c:116
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,...
Definition: nstring.c:94
const char * num2strU(double n, int decimals)
Unsafe version of num2str that uses an internal buffer. Every call overwrites the return value.
Definition: nstring.c:230