+++ /dev/null
-#ifndef __cpExtensions__Utility__h__
-#define __cpExtensions__Utility__h__
-
-#include <chrono>
-#include <cstring>
-#include <fstream>
-#include <string>
-#include <cpExtensions/Config.h>
-
-// -------------------------------------------------------------------------
-#ifdef cpExtensions_OS_Windows
-# define cpExtensions_STRTOK( A, B, N ) strtok_s( A, B, N )
-# define cpExtensions_SPRINTF( B, S, O ) sprintf_s( B, S, "%s", O );
-#else // cpExtensions_OS_Windows
-# define cpExtensions_STRTOK( A, B, N ) std::strtok( A, B )
-# define cpExtensions_SPRINTF( B, S, O ) std::sprintf( B, "%s", O );
-#endif // cpExtensions_OS_Windows
-
-// -------------------------------------------------------------------------
-#define cpExtensions_CHRONO \
- std::chrono::duration_cast< std::chrono::milliseconds >( \
- std::chrono::system_clock::now( ).time_since_epoch( ) \
- ).count( )
-
-// -------------------------------------------------------------------------
-namespace cpExtensions
-{
- // -----------------------------------------------------------------------
- inline bool IsPathSeparator( char c )
- {
-#ifdef cpExtensions_OS_Windows
- return( c == '/' || c == cpExtensions_PATH_SEPARATOR );
-#else // cpExtensions_OS_Windows
- return( c == cpExtensions_PATH_SEPARATOR );
-#endif // cpExtensions_OS_Windows
- }
-
- // -----------------------------------------------------------------------
- inline bool IsBlank( const char& v )
- {
- return( v == ' ' || v == '\t' || v == '\n' || v == '\r' );
- }
-
- // -----------------------------------------------------------------------
- template< class _TTokens >
- inline void Tokenize(
- _TTokens& tokens, const std::string& str, const std::string& delims
- )
- {
- tokens.clear( );
- if( str.size( ) > 0 )
- {
- auto ssize = str.size( );
- char* buffer = new char[ ssize + 1 ];
- for( unsigned long i = 0; i < ssize; ++i )
- buffer[ i ] = str[ i ];
- buffer[ ssize ] = '\0';
- char* next;
- char* it = cpExtensions_STRTOK( buffer, delims.c_str( ), &next );
- while( it != NULL )
- {
- tokens.push_back( std::string( it ) );
- it = cpExtensions_STRTOK( NULL, delims.c_str( ), &next );
-
- } // elihw
- delete [] buffer;
-
- } // fi
- }
-
- // -----------------------------------------------------------------------
- inline std::string Replace(
- const std::string& str, const std::string& sub, const std::string& nsub
- )
- {
- std::string res = str;
- size_t index;
- while( ( index = res.find( sub ) ) != std::string::npos )
- res.replace( index, sub.size( ), nsub );
- return( res );
- }
-
- // -----------------------------------------------------------------------
- inline bool Read( std::string& buffer, const std::string& fname )
- {
- buffer = "";
- std::ifstream file_stream( fname.c_str( ) );
- if( !file_stream )
- return( false );
- file_stream.seekg( 0, std::ios::end );
- buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
- file_stream.seekg( 0, std::ios::beg );
- buffer.assign(
- ( std::istreambuf_iterator< char >( file_stream ) ),
- std::istreambuf_iterator< char >( )
- );
- file_stream.close( );
- return( true );
- }
-
- // -----------------------------------------------------------------------
- inline bool Write( const std::string& buffer, const std::string& fname )
- {
- std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
- if( !file_stream )
- return( false );
- file_stream.write( buffer.c_str( ), buffer.size( ) );
- return( true );
- }
-
- // -----------------------------------------------------------------------
- inline std::string CanonicalPath( const std::string& path )
- {
- std::string ret = "";
-#ifdef cpExtensions_OS_Windows
- TCHAR buffer[ 4096 ] = TEXT( "" );
- TCHAR** lppPart = { NULL };
- GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
- ret = std::string( buffer );
-#else // cpExtensions_OS_Windows
- char* canonical_path = realpath( path.c_str( ), NULL );
- if( canonical_path != NULL )
- {
- ret = canonical_path;
- free( canonical_path );
-
- } // fi
-#endif // cpExtensions_OS_Windows
- return( ret );
- }
-
-} // ecapseman
-
-#endif // __cpExtensions__Utility__h__
-
-// eof - $RCSfile$