naev 0.12.6
nstring.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
12
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 */
39 giveup = end - needlesize + 1;
40
41 /* i is used to iterate over haystack */
42 for ( const char *i = haystack; i != giveup; i++ ) {
43 const char *j, *k;
44 /* j is used to iterate over part of haystack during comparison */
45 /* k is used to iterate over needle during comparison */
46 for ( j = i, k = needle; j != end && *k != '\0'; j++, k++ ) {
47 /* Bail on the first character that doesn't match */
48 if ( *j != *k )
49 break;
50 }
51 /* If we've reached the end of needle, we've found a match */
52 /* i contains the start of our match */
53 if ( *k == '\0' )
54 return (char *)i;
55 }
56 /* Fell through the loops, nothing found */
57 return NULL;
58}
59#endif /* !HAVE_STRNSTR */
60
68#if !HAVE_STRNDUP
69char *strndup( const char *s, size_t n )
70{
71 size_t len = MIN( strlen( s ), n );
72 char *new = (char *)malloc( len + 1 );
73 if ( new == NULL )
74 return NULL;
75 new[len] = '\0';
76 return (char *)memcpy( new, s, len );
77}
78#endif /* !HAVE_STRNDUP */
79
83int strsort( const void *p1, const void *p2 )
84{
85 return strcmp( *(const char **)p1, *(const char **)p2 );
86}
87
91int strsort_reverse( const void *p1, const void *p2 )
92{
93 return strsort( p2, p1 );
94}
95
102int scnprintf( char *text, size_t maxlen, const char *fmt, ... )
103{
104 int n;
105 va_list ap;
106
107 if ( !maxlen )
108 return 0;
109
110 va_start( ap, fmt );
111 n = vsnprintf( text, maxlen, fmt, ap );
112 va_end( ap );
113 return MIN( maxlen - 1, (size_t)n );
114}
115
123int num2str( char dest[NUM2STRLEN], double n, int decimals )
124{
125 /* Don't use decimals if not necessary. */
126 if ( fabs( fmod( n, 1. ) ) < 1e-3 )
127 decimals = 0;
128
129 if ( n >= 1e15 )
130 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
131 else if ( n >= 1e12 )
132 return snprintf(
133 dest, NUM2STRLEN, _( "%.0f,%03.0f,%03.0f,%03.0f,%03.*f" ),
134 floor( n / 1e12 ), floor( fmod( floor( fabs( n / 1e9 ) ), 1e3 ) ),
135 floor( fmod( floor( fabs( n / 1e6 ) ), 1e3 ) ),
136 floor( fmod( floor( fabs( n / 1e3 ) ), 1e3 ) ), decimals,
137 fmod( floor( fabs( n ) ), 1e3 ) );
138 else if ( n >= 1e9 )
139 return snprintf( dest, NUM2STRLEN, _( "%.0f,%03.0f,%03.0f,%03.*f" ),
140 floor( n / 1e9 ),
141 floor( fmod( floor( fabs( n / 1e6 ) ), 1e3 ) ),
142 floor( fmod( floor( fabs( n / 1e3 ) ), 1e3 ) ), decimals,
143 fmod( floor( fabs( n ) ), 1e3 ) );
144 else if ( n >= 1e6 )
145 return snprintf( dest, NUM2STRLEN, _( "%.0f,%03.0f,%03.*f" ),
146 floor( n / 1e6 ),
147 floor( fmod( floor( fabs( n / 1e3 ) ), 1e3 ) ), decimals,
148 fmod( floor( fabs( n ) ), 1e3 ) );
149 else if ( n >= 1e3 )
150 return snprintf( dest, NUM2STRLEN, _( "%.0f,%03.*f" ), floor( n / 1e3 ),
151 decimals, fmod( floor( fabs( n ) ), 1e3 ) );
152 return snprintf( dest, NUM2STRLEN, "%.*f", decimals, n );
153}
154
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:116
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition naev.h:39
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:123
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:83
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:102
int strsort_reverse(const void *p1, const void *p2)
Order-reversed version of strsort().
Definition nstring.c:91
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:69
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