00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032 #ifndef STRINGF_H
00033 #define STRINGF_H
00034
00048 #include <stdarg.h>
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
00332 +3*16
00333 +16/sizeof(void*)
00334 +2
00335 +16
00336 +1+1;
00337
00338 #else
00339
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
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
00448
00449 #endif // STRINGF_H