naev 0.11.5
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
66#if !HAVE_STRNDUP
67char* strndup( const char *s, size_t n )
68{
69 size_t len = MIN( strlen(s), n );
70 char *new = (char *) malloc (len + 1);
71 if (new == NULL)
72 return NULL;
73 new[len] = '\0';
74 return (char *) memcpy (new, s, len);
75}
76#endif /* !HAVE_STRNDUP */
77
81int strsort( const void *p1, const void *p2 )
82{
83 return strcmp(*(const char **) p1, *(const char **) p2);
84}
85
89int strsort_reverse( const void *p1, const void *p2 )
90{
91 return strsort( p2, p1 );
92}
93
99int scnprintf( char* text, size_t maxlen, const char* fmt, ... )
100{
101 int n;
102 va_list ap;
103
104 if (!maxlen)
105 return 0;
106
107 va_start( ap, fmt );
108 n = vsnprintf( text, maxlen, fmt, ap );
109 va_end( ap );
110 return MIN( maxlen-1, (size_t)n );
111}
112
120int num2str( char dest[NUM2STRLEN], double n, int decimals )
121{
122 /* Don't use decimals if not necessary. */
123 if (fabs(fmod(n,1.)) < 1e-3)
124 decimals = 0;
125
126 if (n >= 1e15)
127 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
128 else if (n >= 1e12)
129 return snprintf( dest, NUM2STRLEN,
130 _("%.0f,%03.0f,%03.0f,%03.0f,%03.*f"),
131 floor(n/1e12),
132 floor(fmod(floor(fabs(n/1e9)),1e3)),
133 floor(fmod(floor(fabs(n/1e6)),1e3)),
134 floor(fmod(floor(fabs(n/1e3)),1e3)),
135 decimals, fmod(floor(fabs(n)),1e3) );
136 else if (n >= 1e9)
137 return snprintf( dest, NUM2STRLEN,
138 _("%.0f,%03.0f,%03.0f,%03.*f"),
139 floor(n/1e9),
140 floor(fmod(floor(fabs(n/1e6)),1e3)),
141 floor(fmod(floor(fabs(n/1e3)),1e3)),
142 decimals, fmod(floor(fabs(n)),1e3) );
143 else if (n >= 1e6)
144 return snprintf( dest, NUM2STRLEN,
145 _("%.0f,%03.0f,%03.*f"),
146 floor(n/1e6),
147 floor(fmod(floor(fabs(n/1e3)),1e3)),
148 decimals, fmod(floor(fabs(n)),1e3) );
149 else if (n >= 1e3)
150 return snprintf( dest, NUM2STRLEN,
151 _("%.0f,%03.*f"),
152 floor(n/1e3), decimals, fmod(floor(fabs(n)),1e3) );
153 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
154}
155
163const char* num2strU( double n, int decimals )
164{
165 static char num2strU_buf[NUM2STRLEN];
166 num2str( num2strU_buf, n, decimals );
167 return num2strU_buf;
168}
169
175void print_with_line_numbers( const char *str )
176{
177 int counter = 0;
178 logprintf( stderr, 0, "%03d: ", ++counter );
179 for (int i=0; str[i] != '\0'; i++) {
180 if (str[i]=='\n')
181 logprintf( stderr, 0, "\n%03d: ", ++counter );
182 else //if (str[i]!='\n')
183 logprintf( stderr, 0, "%c", str[i] );
184 }
185 logprintf( stderr, 0, "\n" );
186}
int logprintf(FILE *stream, int newline, const char *fmt,...)
Like fprintf, but automatically teed to log files (and line-terminated if newline is true).
Definition log.c:56
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:40
void print_with_line_numbers(const char *str)
Prints to stderr with line numbers.
Definition nstring.c:175
int num2str(char dest[NUM2STRLEN], double n, int decimals)
Converts a numeric value to a string.
Definition nstring.c:120
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:81
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:99
int strsort_reverse(const void *p1, const void *p2)
Order-reversed version of strsort().
Definition nstring.c:89
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:67
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:163