src/format.h

changeset 73
1ee9b312dc18
parent 72
03e4d9db3fd9
child 75
bf8c57437231
equal deleted inserted replaced
72:03e4d9db3fd9 73:1ee9b312dc18
1 /*
2 Copyright (c) 2013-2014, Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of the <organization> nor the
16 names of its contributors may be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
1 #ifndef BOTC_FORMAT_H 31 #ifndef BOTC_FORMAT_H
2 #define BOTC_FORMAT_H 32 #define BOTC_FORMAT_H
3 33
4 #include "str.h" 34 #include "str.h"
5 #include "containers.h" 35 #include "containers.h"
6 36
7 class format_arg 37 class format_arg
8 { 38 {
9 public: 39 public:
10 format_arg (const string& a) 40 format_arg (const string& a) : m_string (a) {}
11 { 41 format_arg (int a) : m_string (a) {}
12 m_string = a; 42 format_arg (long a) : m_string (a) {}
13 } 43 format_arg (const char* a) : m_string (a) {}
14
15 format_arg (int a)
16 {
17 m_string.sprintf ("%d", a);
18 }
19
20 format_arg (long a)
21 {
22 m_string.sprintf ("%ld", a);
23 }
24
25 format_arg (uint a)
26 {
27 m_string.sprintf ("%u", a);
28 }
29
30 format_arg (ulong a)
31 {
32 m_string.sprintf ("%lu", a);
33 }
34
35 format_arg (const char* a)
36 {
37 m_string = a;
38 }
39 44
40 format_arg (void* a) 45 format_arg (void* a)
41 { 46 {
42 m_string.sprintf ("%p", a); 47 m_string.sprintf ("%p", a);
43 } 48 }
66 } 71 }
67 72
68 m_string += " }"; 73 m_string += " }";
69 } 74 }
70 75
71 /*
72 template<class T, class R> format_arg (const std::map<T, R>& a)
73 {
74 if (a.size() == 0)
75 {
76 m_string = "{}";
77 return;
78 }
79
80 m_string = "{ ";
81
82 for (std::pair<T, R> it : a)
83 {
84 if (m_string != "{ ")
85 m_string += ", ";
86
87 m_string += format_arg (it.first).as_string() + "=" +
88 format_arg (it.second).as_string();
89 }
90
91 m_string += " }";
92 }
93 */
94
95 inline const string& as_string() const 76 inline const string& as_string() const
96 { 77 {
97 return m_string; 78 return m_string;
98 } 79 }
99 80
121 string format_args (const list<format_arg>& args); 102 string format_args (const list<format_arg>& args);
122 void print_args (FILE* fp, const list<format_arg>& args); 103 void print_args (FILE* fp, const list<format_arg>& args);
123 void do_fatal (const list<format_arg>& args); 104 void do_fatal (const list<format_arg>& args);
124 105
125 #ifndef IN_IDE_PARSER 106 #ifndef IN_IDE_PARSER
126 #define format(...) format_args({ __VA_ARGS__ }) 107 # define format(...) format_args({ __VA_ARGS__ })
127 #define fprint(A, ...) print_args( A, { __VA_ARGS__ }) 108 # define fprint(A, ...) print_args( A, { __VA_ARGS__ })
128 #define print(...) print_args( stdout, { __VA_ARGS__ }) 109 # define print(...) print_args( stdout, { __VA_ARGS__ })
129 #define fatal(...) do_fatal({ __VA_ARGS__ })
130 #else 110 #else
131 string format (void, ...); 111 string format (void, ...);
132 void fprint (FILE* fp, ...); 112 void fprint (FILE* fp, ...);
133 void print (void, ...); 113 void print (void, ...);
134 void fatal (void, ...);
135 #endif 114 #endif
136 115
137 #ifndef IN_IDE_PARSER 116 #ifndef IN_IDE_PARSER
138 # ifdef DEBUG 117 # ifdef DEBUG
139 # define devf(...) fprint( stderr, __VA_ARGS__ ) 118 # define devf(...) fprint (stderr, __VA_ARGS__)
140 # define dvalof( A ) fprint( stderr, "value of '%1' = %2\n", #A, A ) 119 # define dvalof( A ) fprint (stderr, "value of '%1' = %2\n", #A, A)
141 # else 120 # else
142 # define devf(...) 121 # define devf(...)
143 # define dvalof( A ) 122 # define dvalof( A )
144 # endif // DEBUG 123 # endif // DEBUG
145 #else 124 #else
125 // print something in debug builds
146 void devf (void, ...); 126 void devf (void, ...);
127
128 // print the value of @a
147 void dvalof (void a); 129 void dvalof (void a);
148 #endif // IN_IDE_PARSER 130 #endif // IN_IDE_PARSER
149 131
150 #endif // BOTC_FORMAT_H 132 #endif // BOTC_FORMAT_H

mercurial