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