src/format.h

Thu, 24 Jul 2014 16:54:45 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Thu, 24 Jul 2014 16:54:45 +0300
changeset 141
68d60e2cfa76
parent 139
cf11621ae422
permissions
-rw-r--r--

- now works properly (still debugging --help)

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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
36 inline String MakeFormatArgument (const String& a)
135
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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
41 inline String MakeFormatArgument (char a)
135
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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
46 inline String MakeFormatArgument (int a)
135
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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
51 inline String MakeFormatArgument (long a)
135
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
141
68d60e2cfa76 - now works properly (still debugging --help)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 139
diff changeset
56 inline String MakeFormatArgument (double a)
68d60e2cfa76 - now works properly (still debugging --help)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 139
diff changeset
57 {
68d60e2cfa76 - now works properly (still debugging --help)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 139
diff changeset
58 return String::fromNumber (a);
68d60e2cfa76 - now works properly (still debugging --help)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 139
diff changeset
59 }
68d60e2cfa76 - now works properly (still debugging --help)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 139
diff changeset
60
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
61 inline String MakeFormatArgument (size_t a)
135
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 String::fromNumber (long (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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
66 inline String MakeFormatArgument (const char* a)
135
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 return a;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
69 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
70
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
71 inline String MakeFormatArgument (const void* a)
135
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 text;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
74 text.sprintf ("%p", a);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
75 return text;
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
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
78 inline String MakeFormatArgument (std::nullptr_t)
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
79 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
80 return "(nullptr)";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
81 }
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 template<class T>
136
1c40bb4f8221 - major refactoring pretty much done, compiles now
Teemu Piippo <crimsondusk64@gmail.com>
parents: 135
diff changeset
84 inline String MakeFormatArgument (List<T> const& list)
135
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 String 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 if (list.isEmpty())
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
89 return "{}";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
90
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 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
94 {
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
95 if (it != list.begin())
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
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
98 result += MakeFormatArgument (*it);
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
99 }
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
100
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
101 result += " }";
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
102 return result;
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
103 }
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 class FormatArgument
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 public:
135
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
108 template<typename T>
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
109 FormatArgument (const T& a) :
8b9132fea327 - massive refactoring continues (doesn't compile yet)
Teemu Piippo <crimsondusk64@gmail.com>
parents: 133
diff changeset
110 m_text (MakeFormatArgument (a)) {}
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
112 inline const String& text() const
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
113 {
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
114 return m_text;
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
115 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
116
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
117 private:
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
118 String m_text;
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 };
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 #ifndef IN_IDE_PARSER
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 # ifdef DEBUG
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 # define devf(...) PrintTo (stderr, __VA_ARGS__)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 # 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
125 # else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 # define devf(...)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 # define dvalof( A )
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 # endif // DEBUG
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 #else
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 // print something in debug builds
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 void devf (void, ...);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 // print the value of @a
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 void dvalof (void a);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 #endif // IN_IDE_PARSER
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
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 // Formats the given string with the given args.
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 String formatArgs (const String& fmtstr, const std::vector<String>& args);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
143 // _________________________________________________________________________________________________
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
144 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
145 // Expands the given arguments into a vector of strings.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
146 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 template<typename T, typename... RestTypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 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
149 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 data.push_back (FormatArgument (arg).text());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 expandFormatArguments (data, rest...);
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
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 static void expandFormatArguments (std::vector<String>& data) __attribute__ ( (unused));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 static void expandFormatArguments (std::vector<String>& data)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 (void) data;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
160 // _________________________________________________________________________________________________
124
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 // Formats the given formatter string and args and returns the string.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
163 // This is essentially a modernized sprintf.
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 // 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
166 // 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
167 // QString::arg() syntax. Note: %0 is invalid.
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 // 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
170 // just before the digit. Currently supported modifiers are s, d and x.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
171 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
172 // - 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
173 // 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
174 // empty string.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
175 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
176 // - 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
177 // its previous expansion. Use this to get numeric forms of @c char
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
178 // arguments.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
179 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
180 // - x: The numeric argument will be represented in hexadecimal notation. This
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
181 // 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
182 // 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
183 // and 0x0 is printed.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
184 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 template<typename... argtypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 String format (const String& fmtstr, const argtypes&... raw_args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 std::vector<String> args;
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 expandFormatArguments (args, raw_args...);
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
190 ASSERT_EQ (args.size(), sizeof... (raw_args))
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 return formatArgs (fmtstr, args);
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
194 // _________________________________________________________________________________________________
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
195 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
196 // This is an overload of format() where no arguments are supplied.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
197 // It returns the formatter string as-is.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
198 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 static String format (const String& fmtstr) __attribute__ ( (unused));
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 static String format (const String& fmtstr)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 return fmtstr;
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
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
205 // _________________________________________________________________________________________________
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
206 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
207 // Processes the given formatter string using format() and prints it to the
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
208 // specified file pointer.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
209 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 template<typename... argtypes>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 void printTo (FILE* fp, const String& fmtstr, const argtypes&... args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 fprintf (fp, "%s", format (fmtstr, args...).c_str());
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
216 // _________________________________________________________________________________________________
124
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 // Processes the given formatter string using format() and appends it to the
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
219 // specified file by name.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
220 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
221 template<typename... argtypes>
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
222 void printTo (const String& filename, const String& fmtstr, const argtypes&... args)
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 FILE* fp = fopen (filename, "a");
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
225
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
226 if (fp != null)
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
227 {
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
228 fprintf (fp, "%s", format (fmtstr, args...).c_str());
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
229 fflush (fp);
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
230 fclose (fp);
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
231 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
232 }
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
233
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
234 // _________________________________________________________________________________________________
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
235 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
236 // Processes the given formatter string using format() and prints the result to
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
237 // stdout.
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>
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 void print (const String& fmtstr, const argtypes&... args)
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 {
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 printTo (stdout, fmtstr, args...);
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
139
cf11621ae422 - added proper commandline interface with options. synergizes very nicely with named enums. :-) added a verbosity level which does nothing just yet
Teemu Piippo <crimsondusk64@gmail.com>
parents: 136
diff changeset
245 // _________________________________________________________________________________________________
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
246 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
247 // Throws an std::runtime_error with the processed formatted string. The program
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
248 // 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
249 // caught in main() which prints the error to stderr and then exits.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
250 //
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 template<typename... argtypes>
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
252 void error (const char* fmtstr, const argtypes&... args)
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 {
125
85814c0918c5 - now with a smarter assert
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
254 error (format (String (fmtstr), args...));
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 }
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256
124
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
257 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
258 // An overload of error() with no string formatting in between.
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
259 //
a7b769a0e537 - commence refactoring
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
260 void error (const String& msg);
119
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261
bdf8d46c145f - renamed source files to camelCase
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262 #endif // BOTC_FORMAT_H

mercurial