/* aprintf.c librock_CHISEL _summary printf into a librock astring. Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef librock_NOTLIBROCK /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #include #include #include #include #define librock_PTR #define librock_CONST const #define librock_VALIST va_list /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_aprintf #define librock_TYPES_INC_STDARG #include #include #include #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_aprintf /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_aprintf[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /* Function wrapping and tracing features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #define librock_body_vastrprintf librock_vastrprintf #define librock_body_astrprintf librock_astrprintf /**************************************************************/ #endif /**************************************************************/ #ifndef librock_NOIMPL_vastrprintf /**************************************************************/ int librock_body_vastrprintf(char librock_PTR * librock_PTR *ppasz,const char *format,librock_VALIST ap) {/* Copyright 1998-2002, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_vastrprintf /* librock_vastrprintf - printf into an allocated string. */ /**/ #include int librock_astrprintf( char librock_PTR * librock_PTR *ppasz, const char *format, ...); int librock_vastrprintf( char librock_PTR * librock_PTR *ppasz, const char *format, va_list ap); /* Operates as the ANSI standard fprintf() and vfprintf(), but the output is placed into a buffer large enough to hold the output, obtained using librock_astrensure() If your platform has vasprintf(), then #define librock_aprintf_USE_vasprintf when compiling for the most efficient implementation. (This can be done in ) Otherwise, this uses tmpfile() and vfprintf(), which will have significant performance penalties. Typical use is */ char *asz = 0; librock_astrprintf(&asz,"This is a test %s\n",pszLongString); . . . librock_astrfree(&asz); /* librock_astrensure() vasprintf() memcpy() //#ifdef librock_aprintf_USE_vasprintf vfprintf() tmpfile() ftell() rewind() fread() fclose() //#ifndef librock_aprintf_USE_vasprintf */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ int ret; FILE *f; #ifdef librock_aprintf_USE_vasprintf char *buf; ret = vasprintf(&buf,format,ap); if (ret >= 0) { /* Lack of error checking reported by James Antill */ if (!buf) { /* GNU vasprintf can return 0 on malloc failure */ return -1; } librock_astrensure(ppasz,ret); if (!*ppasz) { return -1; } memcpy(*ppasz,buf,ret+1); /* Could have embedded \0, so don't use astrcpy */ free(buf); } #else /* tmpfile() is sooo slow, that we take a shot at doing simple strings without it. We don't try to do '%c' because that could stick a \0 in the string, messing up astrcat() 2002-10-10 */ const char *ptr,*ptr2; int need; librock_astrcpy(ppasz,""); ptr = format; ptr2 = format; while(ptr) { ptr = strchr(ptr,'%'); if (!ptr) { librock_astrcat(ppasz,ptr2); /* Finished */ if (!*ppasz) { return -1; } return strlen(*ppasz); } librock_astrn0cat(ppasz,ptr2,ptr-ptr2); switch(ptr[1]) { case '%': librock_astrcat(ppasz,"%"); break; case 's': librock_astrcat(ppasz,va_arg(ap,const char *)); break; case 'd': librock_astrensure(ppasz,strlen(*ppasz)+sizeof(int)*4+1); if (!*ppasz) { return -1; } sprintf(*ppasz+strlen(*ppasz),"%d",va_arg(ap,int)); break; case 'u': librock_astrensure(ppasz,strlen(*ppasz)+sizeof(int)*4+1); if (!*ppasz) { return -1; } sprintf(*ppasz+strlen(*ppasz),"%d",va_arg(ap,unsigned)); break; default: f = tmpfile(); if (!f) { librock_astrfree(ppasz); ret = -1; } else { ret = vfprintf(f,ptr,(va_list) ap); need = ftell(f); rewind(f); librock_astrensure(ppasz,need+strlen(*ppasz)); if (*ppasz) { (*ppasz)[need] = '\0'; fread(*ppasz,1,need,f); } else { librock_astrfree(ppasz); ret = -1; } fclose(f); } return ret; break; } if (!*ppasz) { return -1; } if (!ptr) { break; } ptr += 2; ptr2 = ptr; } #endif return ret; } int librock_body_astrprintf(char **ppasz,const char *format,...) { int ret; va_list ap; va_start(ap, format); ret = librock_vastrprintf(ppasz,format,(librock_VALIST) ap); va_end(ap); return ret; } /* librock_astrprintf */ /**************************************************************/ #endif /* NOIMP section */ /* $Log: aprintf.c,v $ Revision 1.7 2003/02/25 06:05:27 forrest@mibsoftware.com rights=#1 GNU vasprintf returns 0 on malloc error. Handle it. Revision 1.6 2003/02/25 05:31:22 forrest@mibsoftware.com rights=#1 Handle cases when vasprintf fails. Revision 1.5 2003/02/24 23:10:48 forrest@mibsoftware.com rights=#1 Handle special cases, for speedup. Error checking for astring calls. Revision 1.4 2002/08/01 14:11:42 forrest@mibsoftware.com rights=#1 Moved CVS log to end. Changed text in NOTLIBROCK section. Revision 1.3 2002/07/31 15:36:57 forrest@mibsoftware.com rights=#1 _HC instead of _MD5 Added Quick reuse section. Man page fix. Revision 1.2 2002/04/09 03:28:47 forrest@mibsoftware.com rights=#1 FNTYPEs added. Updated LICENSE in manual. Revision 1.1 2002/03/16 22:33:39 forrest@mibsoftware.com rights=#1 Initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */