00001 /*<std-header orig-src='shore'> 00002 00003 $Id: w_form.cpp,v 1.11 2010/12/08 17:37:37 nhall Exp $ 00004 00005 SHORE -- Scalable Heterogeneous Object REpository 00006 00007 Copyright (c) 1994-99 Computer Sciences Department, University of 00008 Wisconsin -- Madison 00009 All Rights Reserved. 00010 00011 Permission to use, copy, modify and distribute this software and its 00012 documentation is hereby granted, provided that both the copyright 00013 notice and this permission notice appear in all copies of the 00014 software, derivative works or modified versions, and any portions 00015 thereof, and that both notices appear in supporting documentation. 00016 00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY 00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS 00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND 00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 00021 00022 This software was developed with support by the Advanced Research 00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by 00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518. 00025 Further funding for this work was provided by DARPA through 00026 Rome Research Laboratory Contract No. F30602-97-2-0247. 00027 00028 */ 00029 00030 #include "w_defines.h" 00031 00032 /* -- do not edit anything above this line -- </std-header>*/ 00033 00034 00035 /* 00036 * This software is Copyright 1989, 1991, 1992, 1993, 1994, 1998 by: 00037 * 00038 * Josef Burger <bolo@cs.wisc.edu> 00039 * 00040 * All Rights Reserved. 00041 * 00042 * This software may be freely used as long as credit is given 00043 * to the author and this copyright is maintained. 00044 */ 00045 00046 /* 00047 * An implementation of the 'form' function, an IO stream helper 00048 * for c++ that is equivalent to sprintf for stdio. 00049 * 00050 * This is also an improvement over most c++ library implementations 00051 * in that it allows the buffers for multiple formats to exist 00052 * simultaneously, instead of always over-writing one static buffer. 00053 * This also provides some sanity in threaded environments; more than 00054 * one thread can form() at once. 00055 * 00056 * Configuration options: 00057 * MAX_BUFFER Maximum length of each formatted output buffer. 00058 * 00059 * HAVE_VSNPRINTF If defined uses the 'n' format of vsprintf which 00060 * prevents the buffer from being written past its end. 00061 */ 00062 00063 #include <cstdarg> 00064 #include <cstdio> 00065 00066 00067 const int MAX_BUFFER = 1024; 00068 00069 static __thread char default_buffer[MAX_BUFFER]; 00070 00071 const char *form(const char *format, ...) 00072 { 00073 va_list ap; 00074 char *buffer; 00075 00076 buffer = default_buffer; 00077 00078 va_start(ap, format); 00079 #ifdef HAVE_VSNPRINTF 00080 vsnprintf(buffer, MAX_BUFFER, format, ap); 00081 #else 00082 #ifdef HAVE_VPRINTF 00083 vsprintf(buffer, format, ap); 00084 #else 00085 #error need vsprintf 00086 #endif 00087 00088 #endif 00089 va_end(ap); 00090 00091 buffer[MAX_BUFFER-1] = '\0'; /* Paranoia */ 00092 00093 return buffer; 00094 }