OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WIOTools.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WIOTOOLS_H
26 #define WIOTOOLS_H
27 
28 #include <stdint.h>
29 
30 #include <algorithm>
31 #include <string>
32 
33 // Use filesystem version 2 for compatibility with newer boost versions.
34 #ifndef BOOST_FILESYSTEM_VERSION
35  #define BOOST_FILESYSTEM_VERSION 2
36 #endif
37 #include <boost/filesystem.hpp>
38 
39 #include "WExportCommon.h"
40 #include "WAssert.h"
41 
42 /**
43  * Checks if you are on a big endian machine or not.
44  */
45 inline bool isBigEndian()
46 {
47  union
48  {
49  uint32_t i;
50  char c[4];
51  } some = {0x01020305}; // NOLINT assigning an 32 bit unsigned integer
52 
53  return some.c[0] == 1;
54 }
55 
56 
57 /**
58  * Transforms a value of type T into the opposite byte order.
59  *
60  * \param value The value where byte swapping should be applied to
61  */
62 template< class T > T switchByteOrder( const T value )
63 {
64  size_t numBytes = sizeof( T );
65  T result = value;
66  if( numBytes == 1 )
67  {
68  return result;
69  }
70  WAssert( numBytes % 2 == 0 && numBytes > 0, "odd number of bytes whilte switching byte order" );
71  char *s = reinterpret_cast< char* >( &result );
72  for( size_t i = 0; i < numBytes / 2; ++i )
73  {
74  std::swap( s[i], s[ ( numBytes - 1 ) - i ] );
75  }
76  return result;
77 }
78 
79 /**
80  * Transform a whole array of elements (of type T and size of sizeof(T))
81  * into opposite byte order.
82  *
83  * \param array Array containing the data
84  * \param arraySize The number of elements which is not the number of
85  * bytes but e.g. the number of floats
86  */
87 template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize )
88 {
89  for( size_t i = 0; i < arraySize; ++i )
90  {
91  array[i] = switchByteOrder< T >( array[i] );
92  }
93 }
94 
95 /**
96  * \param name File name to get the extension or suffix from.
97  * \return filename suffix
98  */
99 inline std::string getSuffix( std::string name )
100 {
101  return boost::filesystem::path( name ).extension();
102 }
103 
104 /**
105  * Checks if a given path already exists or not
106  *
107  * \param name Path to be checked on existence
108  */
109 inline bool fileExists( const std::string& name )
110 {
111  return boost::filesystem::exists( boost::filesystem::path( name ) );
112 }
113 
114 /**
115  * Generate a file name with full path for a temp file.
116  * \return The file name.
117  */
118 boost::filesystem::path tempFileName();
119 
120 /**
121  * Get the contens of a file as a string.
122  *
123  * \param path Filename of the file to read.
124  *
125  * \throw WFileNotFound If file cannot be opened for reading
126  *
127  * \note The string is copied, which may result in performance issues when files are getting big.
128  *
129  * \return The file content in as string.
130  */
131 std::string OWCOMMON_EXPORT readFileIntoString( const boost::filesystem::path& path );
132 
133 /**
134  * Get the contens of a file as a string.
135  *
136  * \param name Filename of the file to read.
137  *
138  * \throw WFileNotFound If file cannot be opened for reading
139  *
140  * \note The string is copied, which may result in performance issues when files are getting big.
141  *
142  * \return The file content in as string.
143  */
144 std::string OWCOMMON_EXPORT readFileIntoString( const std::string& name );
145 
146 /**
147  * Writes the contens of a string to the given path.
148  *
149  * \param path The path of the file where all is written to
150  * \param content Payload written into that file
151  *
152  * \throw WFileOpenFailed If file cannot be opened for writing
153  */
154 void OWCOMMON_EXPORT writeStringIntoFile( const boost::filesystem::path& path, const std::string& content );
155 
156 /**
157  * Writes the contens of a string to the given path.
158  *
159  * \param name The path of the file where all is written to
160  * \param content Payload written into that file
161  *
162  * \throw WFileOpenFailed If file cannot be opened for writing
163  */
164 void OWCOMMON_EXPORT writeStringIntoFile( const std::string& name, const std::string& content );
165 
166 #endif // WIOTOOLS_H