OpenWalnut  1.4.0
WIOTools.h
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 #include <boost/filesystem.hpp>
00034 
00035 #include "WDefines.h"
00036 #include "WAssert.h"
00037 
00038 /**
00039  * Checks if you are on a big endian machine or not.
00040  */
00041 inline bool isBigEndian()
00042 {
00043     union
00044     {
00045         uint32_t i;
00046         char c[4];
00047     } some = {0x01020305}; // NOLINT assigning an 32 bit unsigned integer
00048 
00049     return some.c[0] == 1;
00050 }
00051 
00052 
00053 /**
00054  * Transforms a value of type T into the opposite byte order.
00055  *
00056  * \param value The value where byte swapping should be applied to
00057  */
00058 template< class T > T switchByteOrder( const T value )
00059 {
00060     size_t numBytes = sizeof( T );
00061     T result = value;
00062     if( numBytes == 1 )
00063     {
00064         return result;
00065     }
00066     WAssert( numBytes % 2 == 0  && numBytes > 0, "odd number of bytes whilte switching byte order" );
00067     char *s  = reinterpret_cast< char* >( &result );
00068     for( size_t i = 0; i < numBytes / 2; ++i )
00069     {
00070         std::swap( s[i], s[ ( numBytes - 1 ) - i ] );
00071     }
00072     return result;
00073 }
00074 
00075 /**
00076  * Transform a whole array of elements (of type T and size of sizeof(T))
00077  * into opposite byte order.
00078  *
00079  * \param array Array containing the data
00080  * \param arraySize The number of elements which is not the number of
00081  * bytes but e.g. the number of floats
00082  */
00083 template< class T > void switchByteOrderOfArray( T *array, const size_t arraySize )
00084 {
00085     for( size_t i = 0; i < arraySize; ++i )
00086     {
00087         array[i] = switchByteOrder< T >( array[i] );
00088     }
00089 }
00090 
00091 /**
00092  * \param name File name to get the extension or suffix from.
00093  * \return filename suffix
00094  */
00095 inline std::string getSuffix( boost::filesystem::path name )
00096 {
00097     return  name.extension().string();
00098 }
00099 
00100 /**
00101  * \param name File name to get the extension or suffix from.
00102  * \return filename suffix
00103  */
00104 inline std::string getSuffix( std::string name )
00105 {
00106     return getSuffix( boost::filesystem::path( name ) );
00107 }
00108 
00109 /**
00110  * Checks if a given path already exists or not
00111  *
00112  * \param name Path to be checked on existence
00113  */
00114 inline bool fileExists( const std::string& name )
00115 {
00116     return boost::filesystem::exists( boost::filesystem::path( name ) );
00117 }
00118 
00119 /**
00120  * Generate a file name with full path for a temp file.
00121  * \deprecated use tempFilename instead
00122  * \return The file name.
00123  */
00124 OW_API_DEPRECATED boost::filesystem::path tempFileName();
00125 
00126 /**
00127  * Generate a file name with full path for a temp file.
00128  *
00129  * \param model this string defines a base filename for the temp file. The % is replaced randomly. For details, see boost::filesystem::unique_path.
00130  * \return The file name.
00131  */
00132 boost::filesystem::path tempFilename( boost::filesystem::path model = "%%%%%%%%" );
00133 
00134 /**
00135  * Get the contens of a file as a string.
00136  *
00137  * \param path Filename of the file to read.
00138  *
00139  * \throw WFileNotFound If file cannot be opened for reading
00140  *
00141  * \note The string is copied, which may result in performance issues when files are getting big.
00142  *
00143  * \return The file content in as string.
00144  */
00145 std::string readFileIntoString( const boost::filesystem::path& path );
00146 
00147 /**
00148  * Get the contens of a file as a string.
00149  *
00150  * \param name Filename of the file to read.
00151  *
00152  * \throw WFileNotFound If file cannot be opened for reading
00153  *
00154  * \note The string is copied, which may result in performance issues when files are getting big.
00155  *
00156  * \return The file content in as string.
00157  */
00158 std::string readFileIntoString( const std::string& name );
00159 
00160 /**
00161  * Writes the contens of a string to the given path.
00162  *
00163  * \param path The path of the file where all is written to
00164  * \param content Payload written into that file
00165  *
00166  * \throw WFileOpenFailed If file cannot be opened for writing
00167  */
00168 void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content );
00169 
00170 /**
00171  * Writes the contens of a string to the given path.
00172  *
00173  * \param name The path of the file where all is written to
00174  * \param content Payload written into that file
00175  *
00176  * \throw WFileOpenFailed If file cannot be opened for writing
00177  */
00178 void writeStringIntoFile( const std::string& name, const std::string& content );
00179 
00180 #endif  // WIOTOOLS_H