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;
39 giveup = end - needlesize + 1;
42 for (
const char *i = haystack; i != giveup; i++ ) {
46 for ( j = i, k = needle; j != end && *k !=
'\0'; j++, k++ ) {
123int num2str(
char dest[NUM2STRLEN],
double n,
int decimals )
126 if ( fabs( fmod( n, 1. ) ) < 1e-3 )
130 return snprintf( dest, NUM2STRLEN,
"%.*f", decimals, n );
131 else if ( n >= 1e12 )
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 ) );
139 return snprintf( dest, NUM2STRLEN, _(
"%.0f,%03.0f,%03.0f,%03.*f" ),
141 floor( fmod( floor( fabs( n / 1e6 ) ), 1e3 ) ),
142 floor( fmod( floor( fabs( n / 1e3 ) ), 1e3 ) ), decimals,
143 fmod( floor( fabs( n ) ), 1e3 ) );
145 return snprintf( dest, NUM2STRLEN, _(
"%.0f,%03.0f,%03.*f" ),
147 floor( fmod( floor( fabs( n / 1e3 ) ), 1e3 ) ), decimals,
148 fmod( floor( fabs( 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 );
int logprintf(FILE *stream, int newline, const char *fmt,...)
Like fprintf, but automatically teed to log files (and line-terminated if newline is true).
int num2str(char dest[NUM2STRLEN], double n, int decimals)
Converts a numeric value to a string.
char * strnstr(const char *haystack, const char *needle, size_t size)
A bounded version of strstr. Conforms to BSD semantics.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
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.