OpenWalnut
1.2.5
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifndef WIOTOOLS_H 00026 #define WIOTOOLS_H 00027 00028 #include <stdint.h> 00029 00030 #include <algorithm> 00031 #include <string> 00032 00033 // Use filesystem version 2 for compatibility with newer boost versions. 00034 #ifndef BOOST_FILESYSTEM_VERSION 00035 #define BOOST_FILESYSTEM_VERSION 2 00036 #endif 00037 #include <boost/filesystem.hpp> 00038 00039 #include "WExportCommon.h" 00040 #include "WAssert.h" 00041 00042 /** 00043 * Checks if you are on a big endian machine or not. 00044 */ 00045 inline bool isBigEndian() 00046 { 00047 union 00048 { 00049 uint32_t i; 00050 char c[4]; 00051 } some = {0x01020305}; // NOLINT assigning an 32 bit unsigned integer 00052 00053 return some.c[0] == 1; 00054 } 00055 00056 00057 /** 00058 * Transforms a value of type T into the opposite byte order. 00059 * 00060 * \param value The value where byte swapping should be applied to 00061 */ 00062 template< class T > T switchByteOrder( const T value ) 00063 { 00064 size_t numBytes = sizeof( T ); 00065 T result = value; 00066 if( numBytes == 1 ) 00067 { 00068 return result; 00069 } 00070 WAssert( numBytes % 2 == 0 && numBytes > 0, "odd number of bytes whilte switching byte order" ); 00071 char *s = reinterpret_cast< char* >( &result ); 00072 for( size_t i = 0; i < numBytes / 2; ++i ) 00073 { 00074 std::swap( s[i], s[ ( numBytes - 1 ) - i ] ); 00075 } 00076 return result; 00077 } 00078 00079 /** 00080 * Transform a whole array of elements (of type T and size of sizeof(T)) 00081 * into opposite byte order. 00082 * 00083 * \param array Array containing the data 00084 * \param arraySize The number of elements which is not the number of 00085 * bytes but e.g. the number of floats 00086 */ 00087 template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize ) 00088 { 00089 for( size_t i = 0; i < arraySize; ++i ) 00090 { 00091 array[i] = switchByteOrder< T >( array[i] ); 00092 } 00093 } 00094 00095 /** 00096 * \param name File name to get the extension or suffix from. 00097 * \return filename suffix 00098 */ 00099 inline std::string getSuffix( std::string name ) 00100 { 00101 return boost::filesystem::path( name ).extension(); 00102 } 00103 00104 /** 00105 * Checks if a given path already exists or not 00106 * 00107 * \param name Path to be checked on existence 00108 */ 00109 inline bool fileExists( const std::string& name ) 00110 { 00111 return boost::filesystem::exists( boost::filesystem::path( name ) ); 00112 } 00113 00114 /** 00115 * Generate a file name with full path for a temp file. 00116 * \return The file name. 00117 */ 00118 boost::filesystem::path tempFileName(); 00119 00120 /** 00121 * Get the contens of a file as a string. 00122 * 00123 * \param path Filename of the file to read. 00124 * 00125 * \throw WFileNotFound If file cannot be opened for reading 00126 * 00127 * \note The string is copied, which may result in performance issues when files are getting big. 00128 * 00129 * \return The file content in as string. 00130 */ 00131 std::string OWCOMMON_EXPORT readFileIntoString( const boost::filesystem::path& path ); 00132 00133 /** 00134 * Get the contens of a file as a string. 00135 * 00136 * \param name Filename of the file to read. 00137 * 00138 * \throw WFileNotFound If file cannot be opened for reading 00139 * 00140 * \note The string is copied, which may result in performance issues when files are getting big. 00141 * 00142 * \return The file content in as string. 00143 */ 00144 std::string OWCOMMON_EXPORT readFileIntoString( const std::string& name ); 00145 00146 /** 00147 * Writes the contens of a string to the given path. 00148 * 00149 * \param path The path of the file where all is written to 00150 * \param content Payload written into that file 00151 * 00152 * \throw WFileOpenFailed If file cannot be opened for writing 00153 */ 00154 void OWCOMMON_EXPORT writeStringIntoFile( const boost::filesystem::path& path, const std::string& content ); 00155 00156 /** 00157 * Writes the contens of a string to the given path. 00158 * 00159 * \param name The path of the file where all is written to 00160 * \param content Payload written into that file 00161 * 00162 * \throw WFileOpenFailed If file cannot be opened for writing 00163 */ 00164 void OWCOMMON_EXPORT writeStringIntoFile( const std::string& name, const std::string& content ); 00165 00166 #endif // WIOTOOLS_H