stringf.h

Go to the documentation of this file.
00001 /*
00002 This program is distributed under the terms of the 'MIT license'. The text
00003 of this licence follows...
00004 
00005 Copyright (c) 2007 J.D.Medhurst (a.k.a. Tixy)
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023 THE SOFTWARE.
00024 */
00025 
00032 #ifndef STRINGF_H
00033 #define STRINGF_H
00034 
00048 #include <stdarg.h> // for va_list
00049 
00079 class StringFormatter
00080     {
00081 public:
00092     size_t VFormat(const char* formatString, va_list args);
00093 
00104     size_t Format(const char* formatString, ...);
00105 
00122     size_t HexDumpLine(const void* data, size_t size, ptrdiff_t addressOffset=0);
00123 
00124 protected:
00131     virtual void Out(const char* text, size_t textSize) =0;
00132 
00139     virtual void Out(char character, size_t repeatCount) =0;
00140 
00141 protected:
00145     enum Flags
00146         {
00147         FlagMinus           = 1<<0,     
00148         FlagPlus            = 1<<1,     
00149         FlagSpace           = 1<<2,     
00150         FlagHash            = 1<<3,     
00151         FlagZero            = 1<<4,     
00152 
00153         LengthMod_hh        = 1<<8,     
00154         LengthMod_h         = 1<<9,     
00155         LengthMod_l         = 1<<10,    
00156         LengthMod_ll        = 1<<11,    
00157         LengthMod_L         = 1<<12,    
00158         LengthMod_unknown   = 1<<13,    
00159 
00160         SignedInt           = 1<<15     
00161         };
00162 
00166     class ConversionSpec
00167         {
00168     public:
00172         inline ConversionSpec(va_list args)
00173             : Args(args)
00174             {}
00175 
00183         const char* Decode(const char* format);
00184 
00195         longlong GetIntArg(unsigned flags);
00196 
00202         inline int GetIntArg()
00203             {
00204             return va_arg(Args,int);
00205             }
00206 
00212         inline void* GetPointerArg()
00213             {
00214             return va_arg(Args,void*);
00215             }
00216 
00217     private:
00228         const char* ReadInt(const char* format, int& val);
00229 
00230     public:
00231         va_list     Args;                   
00232         uint16_t    Flags;                  
00233         char        ConversionSpecifier;    
00234         int         FieldWidth;             
00235         int         Precision;              
00236         };
00237 
00257     virtual char* UnkownFormat(char*& dstEnd, ConversionSpec& spec);
00258 
00270     char* DefaultUnkownFormat(char*& dstEnd, ConversionSpec& spec);
00271 
00272 public:
00289     static char* PushHex(ulonglong val, char* dst, int precision=-1, int x='x', int prefix=false);
00290 
00304     static char* PushOctal(ulonglong val, char* dst, int precision=-1, int prefix=false);
00305 
00318     static char* PushDecimal(ulonglong val, char* dst, int precision=-1);
00319 
00320 #if !defined(_MSC_VER)
00321 
00325     static const size_t FormatTextBufferSize = 130;
00326 
00330     static const size_t HexDumpLineSize =
00331         sizeof(void*)*2     // digits in address
00332         +3*16               // 16 hex bytes with leading space
00333         +16/sizeof(void*)   // extra leading spaces used to group bytes
00334         +2                  // two spaces
00335         +16                 // 16 ascii characters
00336         +1+1;               // '\n' + NUL
00337 
00338 #else
00339     // old versions of MSVC don't like static const members, so use enums...
00340     enum { FormatTextBufferSize = 130 };
00341     enum { HexDumpLineSize = sizeof(void*)*2+3*16+16/sizeof(void*)+2+16+1+1 };
00342 
00343 #endif
00344 
00345     friend class StringFormatter::ConversionSpec;
00346     };
00347 
00348 
00366 class StringBufferFormatter : public StringFormatter
00367     {
00368 public:
00373     StringBufferFormatter(char* buffer, size_t size);
00374 
00375     // Implement output methods for StringFormatter...
00376     virtual void Out(const char* text, size_t textSize);
00377     virtual void Out(char character, size_t repeatCount);
00378 
00389     char* End();
00390 
00391 protected:
00392     char* BufferStart;  
00393     char* BufferEnd;    
00394     char* BufferPtr;    
00395     };
00396 
00397 
00407 extern "C" int vsprintf(char* str, const char* format, va_list ap);
00408 
00418 extern "C" int sprintf(char* str, const char* format, ...);
00419 
00431 extern "C" int vsnprintf(char* str, size_t size, const char* format, va_list ap);
00432 
00444 extern "C" int snprintf(char* str, size_t size, const char* format, ...);
00445 
00446  // End of group
00448 
00449 #endif // STRINGF_H

Generated by  doxygen 1.6.1