ClanLib  2.3.7
string_format.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
31 
32 #pragma once
33 
34 #include "../api_core.h"
35 #include <vector>
36 #include "string_types.h"
37 
42 {
45 
46 public:
47 
51  CL_StringFormat(const CL_StringRef &format_string);
52 
53  ~CL_StringFormat();
54 
58 
59 public:
60  const CL_String &get_result() const;
61 
65 
66 public:
67 
72  void set_arg(int index, const CL_StringRef &text);
73 
79  void set_arg(int index, int value, int min_length = 0);
80 
86  void set_arg(int index, unsigned int value, int min_length = 0);
87 
93  void set_arg(int index, long unsigned int value, int min_length = 0);
94 
100  void set_arg(int index, long long value, int min_length = 0);
101 
107  void set_arg(int index, unsigned long long value, int min_length = 0);
108 
113  void set_arg(int index, float value);
114 
119  void set_arg(int index, double value);
120 
124 
125 private:
126 
132  void create_arg(int index, int start, int length);
133 
134  CL_String string;
135 
136  struct ArgPosition
137  {
138  ArgPosition() : start(0), length(-1) { }
139  ArgPosition(int s, int l) : start(s), length(l) {}
140  int start;
141  int length;
142  };
143 
144  std::vector<ArgPosition> args;
146 };
147 
148 inline CL_String cl_format(const CL_StringRef &format)
149 { return format; }
150 
151 template <class Arg1>
152 CL_String cl_format(const CL_StringRef &format, Arg1 arg1)
153 { CL_StringFormat f(format); f.set_arg(1, arg1); return f.get_result(); }
154 
155 template <class Arg1, class Arg2>
156 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2)
157 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.get_result(); }
158 
159 template <class Arg1, class Arg2, class Arg3>
160 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
161 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.get_result(); }
162 
163 template <class Arg1, class Arg2, class Arg3, class Arg4>
164 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
165 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); return f.get_result(); }
166 
167 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
168 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
169 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); return f.get_result(); }
170 
171 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
172 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
173 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); return f.get_result(); }
174 
175 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
176 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
177 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); f.set_arg(7, arg7); return f.get_result(); }
178