Skip to content

Commit

Permalink
Add %pT support to apr_snprintf() for printing an apr_os_thread_t.
Browse files Browse the repository at this point in the history
(from a series of suggestions on #apr)


git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@64485 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
trawick committed Apr 17, 2003
1 parent ad3a9f9 commit 558f9ca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Changes with APR 0.9.4

*) Add %pT support to apr_snprintf() for printing an apr_os_thread_t.
[Jeff Trawick]

*) Add APR_TCP_NODELAY_INHERITED & APR_O_NONBLOCK_INHERITED to apr.hw
[Allan Edwards]

Expand Down
2 changes: 2 additions & 0 deletions include/apr_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ APR_DECLARE(const char *) apr_filename_of_pathname(const char *pathname);
* %%pA takes a struct in_addr *, and prints it as a.b.c.d
* %%pI takes an apr_sockaddr_t * and prints it as a.b.c.d:port or
* [ipv6-address]:port
* %%pT takes an apr_os_thread_t * and prints it in decimal
* ('0' is printed if !APR_HAS_THREADS)
* %%pp takes a void * and outputs it in hex
*
* The %%p hacks are to force gcc's printf warning code to skip
Expand Down
50 changes: 50 additions & 0 deletions strings/apr_snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_network_io.h"
#include "apr_portable.h"
#include <math.h>
#if APR_HAVE_CTYPE_H
#include <ctype.h>
Expand Down Expand Up @@ -534,6 +535,30 @@ static char *conv_apr_sockaddr(apr_sockaddr_t *sa, char *buf_end, int *len)



#if APR_HAS_THREADS
static char *conv_os_thread_t(apr_os_thread_t *tid, char *buf_end, int *len)
{
union {
apr_os_thread_t tid;
apr_uint64_t alignme;
} u;
int is_negative;

u.tid = *tid;
switch(sizeof(u.tid)) {
case sizeof(apr_int32_t):
return conv_10(*(apr_uint32_t *)&u.tid, TRUE, &is_negative, buf_end, len);
case sizeof(apr_int64_t):
return conv_10_quad(*(apr_uint64_t *)&u.tid, TRUE, &is_negative, buf_end, len);
default:
/* not implemented; stick 0 in the buffer */
return conv_10(0, TRUE, &is_negative, buf_end, len);
}
}
#endif



/*
* Convert a floating point number to a string formats 'f', 'e' or 'E'.
* The result is placed in buf, and len denotes the length of the string
Expand Down Expand Up @@ -1160,6 +1185,31 @@ APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
}
break;

case 'T':
#if APR_HAS_THREADS
{
apr_os_thread_t *tid;

tid = va_arg(ap, apr_os_thread_t *);
if (tid != NULL) {
s = conv_os_thread_t(tid, &num_buf[NUM_BUF_SIZE], &s_len);
if (adjust_precision && precision < s_len)
s_len = precision;
}
else {
s = S_NULL;
s_len = S_NULL_LEN;
}
pad_char = ' ';
}
#else
char_buf[0] = '0';
s = &char_buf[0];
s_len = 1;
pad_char = ' ';
#endif
break;

case NUL:
/* if %p ends the string, oh well ignore it */
continue;
Expand Down

0 comments on commit 558f9ca

Please sign in to comment.