mirror of
https://git.openldap.org/openldap/openldap.git
synced 2025-12-21 15:19:34 -05:00
add support for error-handling number/time parsing functions; need to replace ato{il}/strto[u]l throughout the code
This commit is contained in:
parent
dcdd2a567a
commit
25fe62efde
2 changed files with 180 additions and 0 deletions
|
|
@ -273,6 +273,24 @@ lutil_LogStoppedEvent( char *svc );
|
||||||
#define putc(c,fp) do { char x=(c); __atoe_l(&x,1); putc(x,fp); } while(0)
|
#define putc(c,fp) do { char x=(c); __atoe_l(&x,1); putc(x,fp); } while(0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_atoi( int *v, const char *s );
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_atou( unsigned *v, const char *s );
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_atol( long *v, const char *s );
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_atoul( unsigned long *v, const char *s );
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_parse_time( const char *in, unsigned long *tp );
|
||||||
|
|
||||||
|
LDAP_LUTIL_F (int)
|
||||||
|
lutil_unparse_time( char *buf, size_t buflen, unsigned long t );
|
||||||
|
|
||||||
LDAP_END_DECL
|
LDAP_END_DECL
|
||||||
|
|
||||||
#endif /* _LUTIL_H */
|
#endif /* _LUTIL_H */
|
||||||
|
|
|
||||||
|
|
@ -329,3 +329,165 @@ lutil_memrchr(const void *b, int c, size_t n)
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_atoi( int *v, const char *s )
|
||||||
|
{
|
||||||
|
char *next;
|
||||||
|
long i;
|
||||||
|
|
||||||
|
assert( s != NULL );
|
||||||
|
assert( v != NULL );
|
||||||
|
|
||||||
|
i = strtol( s, &next, 10 );
|
||||||
|
if ( next == s || next[ 0 ] != '\0' ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (long)(int)i != i ) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = (int)i;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_atou( unsigned *v, const char *s )
|
||||||
|
{
|
||||||
|
char *next;
|
||||||
|
unsigned long u;
|
||||||
|
|
||||||
|
assert( s != NULL );
|
||||||
|
assert( v != NULL );
|
||||||
|
|
||||||
|
u = strtoul( s, &next, 10 );
|
||||||
|
if ( next == s || next[ 0 ] != '\0' ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( (unsigned long)(unsigned)u != u ) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = u;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_atol( long *v, const char *s )
|
||||||
|
{
|
||||||
|
char *next;
|
||||||
|
long l;
|
||||||
|
|
||||||
|
assert( s != NULL );
|
||||||
|
assert( v != NULL );
|
||||||
|
|
||||||
|
l = strtol( s, &next, 10 );
|
||||||
|
if ( next == s || next[ 0 ] != '\0' ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = l;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_atoul( unsigned long *v, const char *s )
|
||||||
|
{
|
||||||
|
char *next;
|
||||||
|
unsigned long ul;
|
||||||
|
|
||||||
|
assert( s != NULL );
|
||||||
|
assert( v != NULL );
|
||||||
|
|
||||||
|
ul = strtoul( s, &next, 10 );
|
||||||
|
if ( next == s || next[ 0 ] != '\0' ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = ul;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char time_unit[] = "dhms";
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_parse_time(
|
||||||
|
const char *in,
|
||||||
|
unsigned long *tp )
|
||||||
|
{
|
||||||
|
unsigned long t = 0;
|
||||||
|
char *s,
|
||||||
|
*next;
|
||||||
|
int sofar = -1,
|
||||||
|
scale[] = { 86400, 3600, 60, 1 };
|
||||||
|
|
||||||
|
*tp = 0;
|
||||||
|
|
||||||
|
for ( s = (char *)in; s[ 0 ] != '\0'; ) {
|
||||||
|
unsigned long u;
|
||||||
|
char *what;
|
||||||
|
|
||||||
|
u = strtoul( s, &next, 10 );
|
||||||
|
if ( next == s ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( next[ 0 ] == '\0' ) {
|
||||||
|
/* assume seconds */
|
||||||
|
t += u;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
what = strchr( time_unit, next[ 0 ] );
|
||||||
|
if ( what == NULL ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( what - time_unit <= sofar ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sofar = what - time_unit;
|
||||||
|
t += u * scale[ sofar ];
|
||||||
|
|
||||||
|
s = &next[ 1 ];
|
||||||
|
}
|
||||||
|
|
||||||
|
*tp = t;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
lutil_unparse_time(
|
||||||
|
char *buf,
|
||||||
|
size_t buflen,
|
||||||
|
unsigned long t )
|
||||||
|
{
|
||||||
|
int len, i;
|
||||||
|
unsigned long v[ 4 ];
|
||||||
|
|
||||||
|
v[ 0 ] = t/86400;
|
||||||
|
v[ 1 ] = (t%86400)/3600;
|
||||||
|
v[ 2 ] = (t%3600)/60;
|
||||||
|
v[ 3 ] = t%60;
|
||||||
|
|
||||||
|
for ( i = 0; i < 4; i++ ) {
|
||||||
|
if ( v[i] > 0 || i == 3 ) {
|
||||||
|
len = snprintf( buf, buflen, "%lu%c", v[ i ], time_unit[ i ] );
|
||||||
|
if ( len < 0 || (unsigned)len >= buflen ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
buflen -= len;
|
||||||
|
buf += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue