00001 #ifndef CLASSLIB_STRING_FORMAT_H
00002 # define CLASSLIB_STRING_FORMAT_H
00003
00004
00005
00006 # include "classlib/sysapi/system.h"
00007 # include <iosfwd>
00008 # include <string>
00009 # include <utility>
00010 # include <list>
00011
00012 namespace lat {
00013
00014
00015
00016
00017 class StringFormat;
00018
00019
00020
00021
00022 std::ostream &
00023 operator<< (std::ostream &out, const StringFormat &x);
00024
00025
00026
00027 class StringFormat
00028 {
00029 public:
00030 explicit StringFormat (const std::string &pattern);
00031
00032
00033
00034
00035 const std::string & value (void) const;
00036 operator const std::string & (void) const;
00037
00038 StringFormat arg (long val,
00039 int fieldwidth = 0,
00040 int base = 10) const;
00041 StringFormat arg (unsigned long val,
00042 int fieldwidth = 0,
00043 int base = 10) const;
00044 StringFormat arg (int val,
00045 int fieldwidth = 0,
00046 int base = 10) const;
00047 StringFormat arg (unsigned int val,
00048 int fieldwidth = 0,
00049 int base = 10) const;
00050 StringFormat arg (short val,
00051 int fieldwidth = 0,
00052 int base = 10) const;
00053 StringFormat arg (unsigned short val,
00054 int fieldwidth = 0,
00055 int base = 10) const;
00056 StringFormat arg (char val, int fieldwidth = 0) const;
00057 StringFormat arg (unsigned char val, int fieldwidth = 0) const;
00058 StringFormat arg (signed char val, int fieldwidth = 0) const;
00059 StringFormat arg (const std::string &val, int fieldwidth = 0) const;
00060 StringFormat arg (double val,
00061 int fieldwidth = 0,
00062 char format = 'g',
00063 int precision = -1) const;
00064
00065 StringFormat rescan (void) const;
00066
00067 private:
00068 typedef std::list< std::pair<size_t, size_t> > ArgList;
00069
00070 StringFormat (void);
00071 StringFormat (const std::string &value, const std::string &replacement,
00072 size_t offset, size_t len,
00073 ArgList::const_iterator first, ArgList::const_iterator last);
00074
00075 void dorescan (void);
00076 StringFormat doarg (const std::string &replacement) const;
00077 static bool nextarg (const std::string &in,
00078 size_t &offset,
00079 size_t &len);
00080
00081 static std::string pad (const char *buf, size_t len, int fieldwidth);
00082
00083 static char * format (char *next, unsigned long val, int base);
00084 static std::string format (long val, int fieldwidth, int base);
00085 static std::string format (unsigned long val, int fieldwidth, int base);
00086 static std::string format (double val, int fieldwidth, char fmt,
00087 int precision);
00088
00089 std::string m_value;
00090 ArgList m_args;
00091 };
00092
00093
00094
00095
00096
00097 }
00098 #endif // CLASSLIB_STRING_FORMAT_H