Board 0.9.6
Tools.h
Go to the documentation of this file.
1/* -*- mode: c++ -*- */
26#ifndef BOARD_TOOLS_H
27#define BOARD_TOOLS_H
28
29#include <cmath>
30#include <cstring>
31#include <ctime>
32#include <iostream>
33
34#if defined(_MSC_VER)
35#define secured_sprintf sprintf_s
36#else
37#define secured_sprintf snprintf
38#endif // defined( _MSC_VER )
39
40namespace LibBoard
41{
42
43struct Rect;
44
45namespace Tools
46{
47
49{
52};
53
58public:
59 inline MessageStream(std::ostream & out, const char * prefix);
60 template <typename T> inline MessageStream operator<<(const T & v);
61 inline MessageStream operator<<(std::ostream & (*fun)(std::ostream &));
62
63private:
64 std::ostream & _out;
65 const char * _prefix;
66};
67
68extern MessageStream error;
71
72MessageStream::MessageStream(std::ostream & out, const char * prefix) : _out(out), _prefix(prefix) {}
73
74template <typename T> //
76{
77 if (_prefix) {
78 _out << _prefix << v;
79 } else {
80 _out << v;
81 }
82 return MessageStream(_out, nullptr);
83}
84
85MessageStream MessageStream::operator<<(std::ostream & (*fun)(std::ostream &))
86{
87 if (_prefix) {
88 _out << _prefix << fun;
89 } else {
90 _out << fun;
91 }
92 return MessageStream(_out, nullptr);
93}
94
95inline void secured_strncpy(char * dst, const char * src, size_t count);
96
97inline void secured_ctime(char * str, const time_t * t, size_t count);
98
99bool base64encode(std::istream & in, std::ostream &, int linesize = 80);
100
101bool stringEndsWith(const char * str, const char * end, CaseSensitivity sensitivity = CaseSensitive);
102
103void flushFile(const char * filename, std::ostream & out);
104
105void getEPSBoundingBox(const char * filename, Rect & rect);
106
107bool canCreateFile(const char * filename);
108
109bool canReadFile(const char * filename);
110
111const char * temporaryFilename(const char * extension);
112
113void initBoardRand(unsigned long seed);
114
115unsigned int boardRand();
116
117double boardRandDouble();
118
119double boardRandDouble(double min, double max);
120
121inline bool almostEqual(const double & a, const double & b);
122
123inline double mix(const double & a, const double & b, const double & time)
124{
125 return a * (1 - time) + (b * time);
126}
127
128template <typename T> inline void unused(const T &, ...) {}
129
130// Inline methods and functions
131
132void secured_strncpy(char * dst, const char * src, size_t count)
133{
134#if defined(_MSC_VER)
135 strncpy_s(dst, count, src, _TRUNCATE);
136#else
137 strncpy(dst, src, count - 1);
138#endif // defined( _MSC_VER )
139}
140
141void secured_ctime(char * str, const time_t * t, size_t count)
142{
143#if defined(_MSC_VER)
144 ctime_s(str, count, t);
145#else
146 strncpy(str, ctime(t), count - 1);
147#endif // defined( _MSC_VER )
148}
149
150inline bool almostEqual(const double & a, const double & b)
151{
152 const double nothing = 1e-10;
153 return a > b ? ((a - b) < nothing) : (a < b) ? ((b - a) < nothing) : true;
154}
155
156template <typename T> void clamp(T & value, const T & min, const T & max)
157{
158 if (value < min) {
159 value = min;
160 return;
161 }
162 if (value > max) {
163 value = max;
164 return;
165 }
166}
167
168template <typename T> inline T square(const T & t)
169{
170 return t * t;
171}
172
173bool solveQuadratic(double a, double b, double c, double & x1, double & x2);
174
175inline double rad2deg(double angle)
176{
177 return (angle / M_PI) * 180;
178}
179
180inline double deg2rad(double angle)
181{
182 return (angle / 180) * M_PI;
183}
184
185} // namespace Tools
186
187} // namespace LibBoard
188
189#endif /* BOARD_TOOLS_H */
#define M_PI
Definition Shape.h:46
Definition Tools.h:57
MessageStream(std::ostream &out, const char *prefix)
Definition Tools.h:72
MessageStream operator<<(const T &v)
Definition Tools.h:75
void unused(const T &,...)
Definition Tools.h:128
double deg2rad(double angle)
Definition Tools.h:180
void flushFile(const char *filename, std::ostream &out)
Definition Tools.cpp:112
double mix(const double &a, const double &b, const double &time)
Definition Tools.h:123
bool canCreateFile(const char *filename)
Definition Tools.cpp:151
double boardRandDouble()
Definition Tools.cpp:232
void initBoardRand(unsigned long seed)
Definition Tools.cpp:220
bool base64encode(std::istream &in, std::ostream &, int linesize=80)
Definition Tools.cpp:55
T square(const T &t)
Definition Tools.h:168
bool almostEqual(const double &a, const double &b)
Definition Tools.h:150
void clamp(T &value, const T &min, const T &max)
Definition Tools.h:156
void getEPSBoundingBox(const char *filename, Rect &rect)
Definition Tools.cpp:127
unsigned int boardRand()
Definition Tools.cpp:225
const char * temporaryFilename(const char *extension)
Definition Tools.cpp:174
CaseSensitivity
Definition Tools.h:49
@ CaseSensitive
Definition Tools.h:50
@ CaseInsensitive
Definition Tools.h:51
MessageStream warning
void secured_ctime(char *str, const time_t *t, size_t count)
Definition Tools.h:141
void secured_strncpy(char *dst, const char *src, size_t count)
Definition Tools.h:132
double rad2deg(double angle)
Definition Tools.h:175
MessageStream notice
bool canReadFile(const char *filename)
Definition Tools.cpp:163
MessageStream error
bool solveQuadratic(double a, double b, double c, double &x1, double &x2)
Definition Tools.cpp:243
bool stringEndsWith(const char *str, const char *end, CaseSensitivity sensitivity=CaseSensitive)
Definition Tools.cpp:92
Definition Board.h:55
Struct representing a rectangle on the plane.
Definition Rect.h:40