src/format.h

changeset 75
bf8c57437231
parent 73
1ee9b312dc18
child 79
2425fa6a4f21
equal deleted inserted replaced
74:007fbadfa7f9 75:bf8c57437231
1 /* 1 /*
2 Copyright (c) 2013-2014, Santeri Piippo 2 Copyright (c) 2014, Santeri Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met: 6 modification, are permitted provided that the following conditions are met:
7 7
36 36
37 class format_arg 37 class format_arg
38 { 38 {
39 public: 39 public:
40 format_arg (const string& a) : m_string (a) {} 40 format_arg (const string& a) : m_string (a) {}
41 format_arg (int a) : m_string (a) {} 41 format_arg (char a) : m_string (a) {}
42 format_arg (long a) : m_string (a) {} 42 format_arg (int a) : m_string (string::from_number (a)) {}
43 format_arg (long a) : m_string (string::from_number (a)) {}
43 format_arg (const char* a) : m_string (a) {} 44 format_arg (const char* a) : m_string (a) {}
44 45
45 format_arg (void* a) 46 format_arg (void* a)
46 { 47 {
47 m_string.sprintf ("%p", a); 48 m_string.sprintf ("%p", a);
97 inline string charnum (char a) 98 inline string charnum (char a)
98 { 99 {
99 return custom_format (a, "%d"); 100 return custom_format (a, "%d");
100 } 101 }
101 102
103 class script_error : public std::exception
104 {
105 public:
106 script_error (const string& msg) : m_msg (msg) {}
107
108 inline const char* what() const throw()
109 {
110 return m_msg.c_str();
111 }
112
113 private:
114 string m_msg;
115 };
116
102 string format_args (const list<format_arg>& args); 117 string format_args (const list<format_arg>& args);
103 void print_args (FILE* fp, const list<format_arg>& args); 118 void print_args (FILE* fp, const list<format_arg>& args);
104 void do_fatal (const list<format_arg>& args); 119 void do_fatal (const list<format_arg>& args);
105 120
106 #ifndef IN_IDE_PARSER 121 #ifndef IN_IDE_PARSER
107 # define format(...) format_args({ __VA_ARGS__ }) 122 # define format(...) format_args({ __VA_ARGS__ })
108 # define fprint(A, ...) print_args( A, { __VA_ARGS__ }) 123 # define fprint(A, ...) print_args( A, { __VA_ARGS__ })
109 # define print(...) print_args( stdout, { __VA_ARGS__ }) 124 # define print(...) print_args( stdout, { __VA_ARGS__ })
125 # define error(...) throw script_error (format (__VA_ARGS__))
110 #else 126 #else
111 string format (void, ...); 127 string format (void, ...);
112 void fprint (FILE* fp, ...); 128 void fprint (FILE* fp, ...);
113 void print (void, ...); 129 void print (void, ...);
130 void error (void, ...);
114 #endif 131 #endif
115 132
116 #ifndef IN_IDE_PARSER 133 #ifndef IN_IDE_PARSER
117 # ifdef DEBUG 134 # ifdef DEBUG
118 # define devf(...) fprint (stderr, __VA_ARGS__) 135 # define devf(...) fprint (stderr, __VA_ARGS__)

mercurial