string_format.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 #include "../api_core.h"
00035 #include <vector>
00036 #include "string_types.h"
00037 
00041 class CL_API_CORE CL_StringFormat
00042 {
00045 
00046 public:
00047 
00051         CL_StringFormat(const CL_StringRef &format_string);
00052 
00053         ~CL_StringFormat();
00054 
00058 
00059 public:
00060         const CL_String &get_result() const;
00061 
00065 
00066 public:
00067 
00072         void set_arg(int index, const CL_StringRef &text);
00073 
00079         void set_arg(int index, int value, int min_length = 0);
00080 
00086         void set_arg(int index, unsigned int value, int min_length = 0);
00087 
00093         void set_arg(int index, long unsigned int value, int min_length = 0);
00094 
00100         void set_arg(int index, long long value, int min_length = 0);
00101 
00107         void set_arg(int index, unsigned long long value, int min_length = 0);
00108 
00113         void set_arg(int index, float value);
00114 
00119         void set_arg(int index, double value);
00120 
00124 
00125 private:
00126 
00132         void create_arg(int index, int start, int length);
00133 
00134         CL_String string;
00135 
00136         struct ArgPosition
00137         {
00138                 ArgPosition() : start(0), length(0) { }
00139                 ArgPosition(int s, int l) : start(s), length(l) {}
00140                 int start;
00141                 int length;
00142         };
00143 
00144         std::vector<ArgPosition> args;
00146 };
00147 
00148 inline CL_String cl_format(const CL_StringRef &format)
00149 { return format; }
00150 
00151 template <class Arg1>
00152 CL_String cl_format(const CL_StringRef &format, Arg1 arg1)
00153 { CL_StringFormat f(format); f.set_arg(1, arg1); return f.get_result(); }
00154 
00155 template <class Arg1, class Arg2>
00156 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2)
00157 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); return f.get_result(); }
00158 
00159 template <class Arg1, class Arg2, class Arg3>
00160 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
00161 { CL_StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); return f.get_result(); }
00162 
00163 template <class Arg1, class Arg2, class Arg3, class Arg4>
00164 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
00165 { 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(); }
00166 
00167 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00168 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
00169 { 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(); }
00170 
00171 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
00172 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
00173 { 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(); }
00174 
00175 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
00176 CL_String cl_format(const CL_StringRef &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
00177 { 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(); }
00178