src/format.h

Mon, 21 Jul 2014 17:14:42 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 21 Jul 2014 17:14:42 +0300
changeset 135
8b9132fea327
parent 133
dbbdb870c835
child 136
1c40bb4f8221
permissions
-rw-r--r--

- massive refactoring continues (doesn't compile yet)

119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
133
dbbdb870c835 - changed all source files to use my correct legal name instead of my calling name
Teemu Piippo <crimsondusk64@gmail.com>
parents: 125
diff changeset
2 Copyright 2012-2014 Teemu Piippo
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 All rights reserved.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 are met:
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 3. The name of the author may not be used to endorse or promote products
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 derived from this software without specific prior written permission.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 */
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 #ifndef BOTC_FORMAT_H
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 #define BOTC_FORMAT_H
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
32 #include "stringClass.h"
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 #include "list.h"
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
34 #include "enumstrings.h"
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
35
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
36 String MakeFormatArgument (const String& a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
37 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
38 return a;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
39 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
40
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
41 String MakeFormatArgument (char a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
42 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
43 return String (a);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
44 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
45
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
46 String MakeFormatArgument (int a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
47 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
48 return String::fromNumber (a);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
49 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
50
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
51 String MakeFormatArgument (long a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
52 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
53 return String::fromNumber (a);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
54 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
55
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
56 String MakeFormatArgument (size_t a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
57 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
58 return String::fromNumber (long (a));
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
59 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
60
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
61 String MakeFormatArgument (const char* a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
62 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
63 return a;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
64 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
65
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
66 String MakeFormatArgument (const void* a)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
67 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
68 String text;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
69 text.sprintf ("%p", a);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
70 return text;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
71 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
72
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
73 String MakeFormatArgument (std::nullptr_t)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
74 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
75 return "(nullptr)";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
76 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
77
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
78 template<class T>
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
79 String MakeFormatArgument (List<T> const& list)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
80 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
81 String result;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
82
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
83 if (list.isEmpty())
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
84 return "{}";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
85
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
86 result = "{ ";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
87
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
88 for (auto it = list.begin(); it != list.end(); ++it)
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
89 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
90 if (it != list.begin())
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
91 result += ", ";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
92
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
93 result += MakeFormatArgument (*it);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
94 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
95
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
96 result += " }";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
97 return result;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
98 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 class FormatArgument
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 {
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
102 public:
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
103 template<typename T>
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
104 FormatArgument (const T& a) :
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
105 m_text (MakeFormatArgument (a)) {}
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
107 inline const String& text() const
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
108 {
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
109 return m_text;
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
110 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
111
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
112 private:
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
113 String m_text;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 };
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 #ifndef IN_IDE_PARSER
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 # ifdef DEBUG
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 # define devf(...) PrintTo (stderr, __VA_ARGS__)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 # define dvalof( A ) PrintTo (stderr, "value of '%1' = %2\n", #A, A)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 # else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 # define devf(...)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 # define dvalof( A )
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 # endif // DEBUG
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 #else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 // print something in debug builds
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 void devf (void, ...);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 // print the value of @a
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 void dvalof (void a);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 #endif // IN_IDE_PARSER
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
133 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
134 // Formats the given string with the given args.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
135 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 String formatArgs (const String& fmtstr, const std::vector<String>& args);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
138 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
139 // Expands the given arguments into a vector of strings.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
140 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 template<typename T, typename... RestTypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 void expandFormatArguments (std::vector<String>& data, const T& arg, const RestTypes& ... rest)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 data.push_back (FormatArgument (arg).text());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 expandFormatArguments (data, rest...);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 static void expandFormatArguments (std::vector<String>& data) __attribute__ ( (unused));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 static void expandFormatArguments (std::vector<String>& data)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 (void) data;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
154 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
155 // Formats the given formatter string and args and returns the string.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
156 // This is essentially a modernized sprintf.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
157 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
158 // Args in the format string take the form %n where n is a digit. The argument
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
159 // will be expanded to the nth argument passed. This is essentially Qt's
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
160 // QString::arg() syntax. Note: %0 is invalid.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
161 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
162 // Arguments can be passed a modifier which takes the form of a character
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
163 // just before the digit. Currently supported modifiers are s, d and x.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
164 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
165 // - s: The argument will expand into "s" if it would've not expanded into "1"
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
166 // otherwise. If it would have expanded into "1" it will expand into an
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
167 // empty string.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
168 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
169 // - d: The argument expands into the numeric form of the first character of
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
170 // its previous expansion. Use this to get numeric forms of @c char
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
171 // arguments.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
172 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
173 // - x: The numeric argument will be represented in hexadecimal notation. This
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
174 // will work if the argument is a string representing a number. If the
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
175 // argument did not expand into a number in the first place, 0 is used
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
176 // and 0x0 is printed.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
177 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 template<typename... argtypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 String format (const String& fmtstr, const argtypes&... raw_args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 std::vector<String> args;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 expandFormatArguments (args, raw_args...);
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
183 ASSERT_EQ (args.size(), sizeof... (raw_args))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 return formatArgs (fmtstr, args);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
187 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
188 // This is an overload of format() where no arguments are supplied.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
189 // It returns the formatter string as-is.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
190 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 static String format (const String& fmtstr) __attribute__ ( (unused));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 static String format (const String& fmtstr)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 return fmtstr;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
197 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
198 // Processes the given formatter string using format() and prints it to the
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
199 // specified file pointer.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
200 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 template<typename... argtypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 void printTo (FILE* fp, const String& fmtstr, const argtypes&... args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 fprintf (fp, "%s", format (fmtstr, args...).c_str());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
207 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
208 // Processes the given formatter string using format() and appends it to the
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
209 // specified file by name.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
210 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
211 template<typename... argtypes>
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
212 void printTo (const String& filename, const String& fmtstr, const argtypes&... args)
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
213 {
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
214 FILE* fp = fopen (filename, "a");
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
215
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
216 if (fp != null)
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
217 {
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
218 fprintf (fp, "%s", format (fmtstr, args...).c_str());
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
219 fflush (fp);
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
220 fclose (fp);
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
221 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
222 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
223
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
224 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
225 // Processes the given formatter string using format() and prints the result to
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
226 // stdout.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
227 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 template<typename... argtypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 void print (const String& fmtstr, const argtypes&... args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 printTo (stdout, fmtstr, args...);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
234 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
235 // Throws an std::runtime_error with the processed formatted string. The program
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
236 // execution terminates after a call to this function as the exception is first
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
237 // caught in main() which prints the error to stderr and then exits.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
238 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 template<typename... argtypes>
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
240 void error (const char* fmtstr, const argtypes&... args)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 {
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
242 error (format (String (fmtstr), args...));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
245 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
246 // An overload of error() with no string formatting in between.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
247 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
248 void error (const String& msg);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 #endif // BOTC_FORMAT_H

mercurial