bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / ap_snprintf.h
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * The ap_vsnprintf/ap_snprintf functions are based on, and used with the
19  * permission of, the  SIO stdio-replacement strx_* functions by Panos
20  * Tsirigotis <panos@alumni.cs.colorado.edu> for xinetd.
21  */
22
23 #ifndef APACHE_AP_SNPRINTF_H
24 #define APACHE_AP_SNPRINTF_H
25
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <limits.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /* stuff marked API_EXPORT is part of the API, and intended for use
35  * by modules
36  */
37 #ifndef API_EXPORT
38 #define API_EXPORT(type)    type
39 #endif
40
41 /* Stuff marked API_EXPORT_NONSTD is part of the API, and intended for
42  * use by modules.  The difference between API_EXPORT and
43  * API_EXPORT_NONSTD is that the latter is required for any functions
44  * which use varargs or are used via indirect function call.  This
45  * is to accomodate the two calling conventions in windows dlls.
46  */
47 #ifndef API_EXPORT_NONSTD
48 #define API_EXPORT_NONSTD(type)    type
49 #endif
50
51 #if !defined(__GNUC__) || __GNUC__ < 2 || \
52     (__GNUC__ == 2 && __GNUC_MINOR__ < 7) ||\
53     defined(NEXT)
54 #define __attribute__(__x)
55 #endif
56
57 /* These macros allow correct support of 8-bit characters on systems which
58  * support 8-bit characters.  Pretty dumb how the cast is required, but
59  * that's legacy libc for ya.  These new macros do not support EOF like
60  * the standard macros do.  Tough.
61  */
62 #define ap_isalpha(c) (isalpha(((unsigned char)(c))))
63 #define ap_isdigit(c) (isdigit(((unsigned char)(c))))
64 #define ap_islower(c) (islower(((unsigned char)(c))))
65
66
67 /* ap_vformatter() is a generic printf-style formatting routine
68  * with some extensions.  The extensions are:
69  *
70  * %pA  takes a struct in_addr *, and prints it as a.b.c.d
71  * %pI  takes a struct sockaddr_in * and prints it as a.b.c.d:port
72  * %pp  takes a void * and outputs it in hex
73  *
74  * The %p hacks are to force gcc's printf warning code to skip
75  * over a pointer argument without complaining.  This does
76  * mean that the ANSI-style %p (output a void * in hex format) won't
77  * work as expected at all, but that seems to be a fair trade-off
78  * for the increased robustness of having printf-warnings work.
79  *
80  * Additionally, ap_vformatter allows for arbitrary output methods
81  * using the ap_vformatter_buff and flush_func.
82  *
83  * The ap_vformatter_buff has two elements curpos and endpos.
84  * curpos is where ap_vformatter will write the next byte of output.
85  * It proceeds writing output to curpos, and updating curpos, until
86  * either the end of output is reached, or curpos == endpos (i.e. the
87  * buffer is full).
88  *
89  * If the end of output is reached, ap_vformatter returns the
90  * number of bytes written.
91  *
92  * When the buffer is full, the flush_func is called.  The flush_func
93  * can return -1 to indicate that no further output should be attempted,
94  * and ap_vformatter will return immediately with -1.  Otherwise
95  * the flush_func should flush the buffer in whatever manner is
96  * appropriate, re-initialize curpos and endpos, and return 0.
97  *
98  * Note that flush_func is only invoked as a result of attempting to
99  * write another byte at curpos when curpos >= endpos.  So for
100  * example, it's possible when the output exactly matches the buffer
101  * space available that curpos == endpos will be true when
102  * ap_vformatter returns.
103  *
104  * ap_vformatter does not call out to any other code, it is entirely
105  * self-contained.  This allows the callers to do things which are
106  * otherwise "unsafe".  For example, ap_psprintf uses the "scratch"
107  * space at the unallocated end of a block, and doesn't actually
108  * complete the allocation until ap_vformatter returns.  ap_psprintf
109  * would be completely broken if ap_vformatter were to call anything
110  * that used a pool.  Similarly http_bprintf() uses the "scratch"
111  * space at the end of its output buffer, and doesn't actually note
112  * that the space is in use until it either has to flush the buffer
113  * or until ap_vformatter returns.
114  */
115
116 typedef struct {
117     char *curpos;
118     char *endpos;
119 } ap_vformatter_buff;
120
121 API_EXPORT(int) ap_vformatter(int (*flush_func)(ap_vformatter_buff *),
122     ap_vformatter_buff *, const char *fmt, va_list ap);
123
124 /* These are snprintf implementations based on ap_vformatter().
125  *
126  * Note that various standards and implementations disagree on the return
127  * value of snprintf, and side-effects due to %n in the formatting string.
128  * ap_snprintf behaves as follows:
129  *
130  * Process the format string until the entire string is exhausted, or
131  * the buffer fills.  If the buffer fills then stop processing immediately
132  * (so no further %n arguments are processed), and return the buffer
133  * length.  In all cases the buffer is NUL terminated. The return value
134  * is the number of characters placed in the buffer, excluding the
135  * terminating NUL. All this implies that, at most, (len-1) characters
136  * will be copied over; if the return value is >= len, then truncation
137  * occured.
138  *
139  * In no event does ap_snprintf return a negative number.
140  */
141 API_EXPORT_NONSTD(int) ap_snprintf(char *buf, size_t len, const char *format,...)
142           __attribute__((format(printf,3,4)));
143 API_EXPORT(int) ap_vsnprintf(char *buf, size_t len, const char *format,
144            va_list ap);
145
146 #ifdef __cplusplus
147 }
148 #endif
149
150 #endif  /* !APACHE_AP_SNPRINTF_H */