1 /* -*- mode: c++ -*- */
2 /**
3  * @file   Tools.h
4  * @author Sebastien Fourey (GREYC)
5  * @date   Nov 2008
6  *
7  * @brief
8  * \@copyright
9  * This source code is part of the Board project, a C++ library whose
10  * purpose is to allow simple drawings in EPS, FIG or SVG files.
11  * Copyright (C) 2007 Sebastien Fourey <http://foureys.users.greyc.fr/>
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published
15  * by the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU Lesser General Public License for more details
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 #ifndef _BOARD_TOOLS_H_
27 #define _BOARD_TOOLS_H_
28 
29 #include <iostream>
30 #include <ctime>
31 #include <cstring>
32 #include "Rect.h"
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 namespace Tools {
43 
44 enum CaseSensitivity { CaseSensitive, CaseInsensitive };
45 
46 /**
47   * A "prefixable" message stream
48   */
49 class MessageStream {
50 public:
51 
52   inline MessageStream( std::ostream & out, const char * prefix  );
53 
54   template<typename T>
55   inline MessageStream operator<<( const T & v );
56 
57   inline MessageStream operator<<( std::ostream & (*fun)(std::ostream &) );
58 
59 private:
60   std::ostream & _out;
61   const char * _prefix;
62 };
63 
64 extern MessageStream error;
65 extern MessageStream warning;
66 extern MessageStream notice;
67 
MessageStream(std::ostream & out,const char * prefix)68 MessageStream::MessageStream( std::ostream & out, const char * prefix )
69   : _out( out ),
70     _prefix( prefix )
71 {
72 }
73 
74 template<typename T>
75 MessageStream MessageStream::operator<<( const T & v )
76 {
77   if ( _prefix ) {
78     _out << _prefix << v;
79   } else {
80     _out << v;
81   }
82   return MessageStream( _out, 0 );
83 }
84 
85 MessageStream 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, 0 );
93 }
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 Rect getEPSBoundingBox( const char * filename );
107 
108 bool canCreateFile( const char * filename );
109 
110 bool canReadFile( const char * filename );
111 
112 const char * temporaryFilename( const char * extension );
113 
114 unsigned int boardRand();
115 
116 }  // namespace Tools
117 
118 }  // namespace LibBoard
119 
120 #include "Tools.ih"
121 
122 #endif /* _SHAPE_H_ */
123