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