OpenWalnut  1.5.0dev
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 #include <boost/filesystem.hpp>
34 
35 #include "WDefines.h"
36 #include "WAssert.h"
37 
38 /**
39  * Checks if you are on a big endian machine or not.
40  */
41 inline bool isBigEndian()
42 {
43  union
44  {
45  uint32_t i;
46  char c[4];
47  } some = {0x01020305}; // NOLINT assigning an 32 bit unsigned integer
48 
49  return some.c[0] == 1;
50 }
51 
52 
53 /**
54  * Transforms a value of type T into the opposite byte order.
55  *
56  * \param value The value where byte swapping should be applied to
57  */
58 template< class T > T switchByteOrder( const T value )
59 {
60  size_t numBytes = sizeof( T );
61  T result = value;
62  if( numBytes == 1 )
63  {
64  return result;
65  }
66  WAssert( numBytes % 2 == 0 && numBytes > 0, "odd number of bytes whilte switching byte order" );
67  char *s = reinterpret_cast< char* >( &result );
68  for( size_t i = 0; i < numBytes / 2; ++i )
69  {
70  std::swap( s[i], s[ ( numBytes - 1 ) - i ] );
71  }
72  return result;
73 }
74 
75 /**
76  * Transform a whole array of elements (of type T and size of sizeof(T))
77  * into opposite byte order.
78  *
79  * \param array Array containing the data
80  * \param arraySize The number of elements which is not the number of
81  * bytes but e.g. the number of floats
82  */
83 template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize )
84 {
85  for( size_t i = 0; i < arraySize; ++i )
86  {
87  array[i] = switchByteOrder< T >( array[i] );
88  }
89 }
90 
91 /**
92  * \param name File name to get the extension or suffix from.
93  * \return filename suffix
94  */
95 inline std::string getSuffix( boost::filesystem::path name )
96 {
97  return name.extension().string();
98 }
99 
100 /**
101  * \param name File name to get the extension or suffix from.
102  * \return filename suffix
103  */
104 inline std::string getSuffix( std::string name )
105 {
106  return getSuffix( boost::filesystem::path( name ) );
107 }
108 
109 /**
110  * Checks if a given path already exists or not
111  *
112  * \param name Path to be checked on existence
113  */
114 inline bool fileExists( const std::string& name )
115 {
116  return boost::filesystem::exists( boost::filesystem::path( name ) );
117 }
118 
119 /**
120  * Generate a file name with full path for a temp file.
121  * \deprecated use tempFilename instead
122  * \return The file name.
123  */
124 OW_API_DEPRECATED boost::filesystem::path tempFileName();
125 
126 /**
127  * Generate a file name with full path for a temp file.
128  *
129  * \param model this string defines a base filename for the temp file. The % is replaced randomly. For details, see boost::filesystem::unique_path.
130  * \return The file name.
131  */
132 boost::filesystem::path tempFilename( boost::filesystem::path model = "%%%%%%%%" );
133 
134 /**
135  * Get the contens of a file as a string.
136  *
137  * \param path Filename of the file to read.
138  *
139  * \throw WFileNotFound If file cannot be opened for reading
140  *
141  * \note The string is copied, which may result in performance issues when files are getting big.
142  *
143  * \return The file content in as string.
144  */
145 std::string readFileIntoString( const boost::filesystem::path& path );
146 
147 /**
148  * Get the contens of a file as a string.
149  *
150  * \param name Filename of the file to read.
151  *
152  * \throw WFileNotFound If file cannot be opened for reading
153  *
154  * \note The string is copied, which may result in performance issues when files are getting big.
155  *
156  * \return The file content in as string.
157  */
158 std::string readFileIntoString( const std::string& name );
159 
160 /**
161  * Writes the contens of a string to the given path.
162  *
163  * \param path The path of the file where all is written to
164  * \param content Payload written into that file
165  *
166  * \throw WFileOpenFailed If file cannot be opened for writing
167  */
168 void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content );
169 
170 /**
171  * Writes the contens of a string to the given path.
172  *
173  * \param name The path of the file where all is written to
174  * \param content Payload written into that file
175  *
176  * \throw WFileOpenFailed If file cannot be opened for writing
177  */
178 void writeStringIntoFile( const std::string& name, const std::string& content );
179 
180 #endif // WIOTOOLS_H
#define OW_API_DEPRECATED
In order to mark functions for the compiler as deprecated we need to put this before each deprecated ...
Definition: WDefines.h:44