src/format.h

changeset 1422
bf373bd83c1f
parent 1403
7a2d84112983
equal deleted inserted replaced
1421:25584737611c 1422:bf373bd83c1f
99 99
100 private: 100 private:
101 QString m_text; 101 QString m_text;
102 }; 102 };
103 103
104
105 // Helper function for format()
106 template<typename Arg1, typename... Rest>
107 void formatHelper (QString& str, Arg1 arg1, Rest... rest)
108 {
109 str = str.arg (StringFormatArg (arg1).text());
110 formatHelper (str, rest...);
111 }
112
113
114 static void formatHelper (QString& str) __attribute__ ((unused));
115 static void formatHelper (QString& str)
116 {
117 (void) str;
118 }
119
120
121 // Format the message with the given args. 104 // Format the message with the given args.
122 // 105 //
123 // The formatting ultimately uses String's arg() method to actually format the args so the format string should be 106 // The formatting ultimately uses String's arg() method to actually format the args so the format string should be
124 // prepared accordingly, with %1 referring to the first arg, %2 to the second, etc. 107 // prepared accordingly, with %1 referring to the first arg, %2 to the second, etc.
125 template<typename... Args> 108 template<typename T, typename... Rest>
126 QString format (QString fmtstr, Args... args) 109 QString format(const QString& formatString, const T& arg, Rest... rest)
127 { 110 {
128 formatHelper (fmtstr, args...); 111 return format(formatString.arg(StringFormatArg(arg).text()), rest...);
129 return fmtstr; 112 }
113
114 // Recursion base
115 inline QString format(const QString& formatString)
116 {
117 return formatString;
130 } 118 }
131 119
132 template<typename... Args> 120 template<typename... Args>
133 void fprint (FILE* fp, QString fmtstr, Args... args) 121 void fprint(FILE* fp, const QString& formatString, Args... args)
134 { 122 {
135 formatHelper (fmtstr, args...); 123 fprintf(fp, "%s", qPrintable(format(formatString, args...)));
136 fprintf (fp, "%s", qPrintable (fmtstr));
137 } 124 }
138 125
139 126
140 template<typename... Args> 127 template<typename... Args>
141 void fprint (QIODevice& dev, QString fmtstr, Args... args) 128 void fprint (QIODevice& dev, const QString& formatString, Args... args)
142 { 129 {
143 formatHelper (fmtstr, args...); 130 dev.write(format(formatString, args...).toUtf8());
144 dev.write (fmtstr.toUtf8());
145 } 131 }
146 132
147 class Printer : public QObject 133 class Printer : public QObject
148 { 134 {
149 Q_OBJECT 135 Q_OBJECT

mercurial