Utility.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "Utility.h"
00027 
00028 #ifdef USING_STREAMS
00029 #include <stropts.h>
00030 #include <sys/conf.h>
00031 #endif
00032 #include <string.h>
00033 #include <math.h>
00034 #include <stdio.h>
00035 #include <algorithm>
00036 #include <fstream>
00037 
00038 namespace FIX
00039 {
00040 void string_replace( const std::string& oldValue,
00041                      const std::string& newValue,
00042                      std::string& value )
00043 {
00044   for( std::string::size_type pos = value.find(oldValue);
00045        pos != std::string::npos;
00046        pos = value.find(oldValue, pos) )
00047   {
00048     value.replace( pos, oldValue.size(), newValue );
00049     pos += newValue.size();
00050   }
00051 }
00052 
00053 std::string string_toUpper( const std::string& value )
00054 {
00055   std::string copy = value;
00056   std::transform( copy.begin(), copy.end(), copy.begin(), toupper );
00057   return copy;
00058 }
00059 
00060 std::string string_toLower( const std::string& value )
00061 {
00062   std::string copy = value;
00063   std::transform( copy.begin(), copy.end(), copy.begin(), tolower );
00064   return copy;
00065 }
00066 
00067 std::string string_strip( const std::string& value )
00068 {
00069   if( !value.size() )
00070     return value;
00071 
00072   int startPos = value.find_first_not_of(" \t\r\n");
00073   int endPos = value.find_last_not_of(" \t\r\n");
00074 
00075   if( startPos == -1 )
00076    return value;
00077 
00078    return std::string( value, startPos, endPos - startPos + 1 );
00079 }
00080 
00081 void socket_init()
00082 {
00083 #ifdef _MSC_VER
00084   WORD version = MAKEWORD( 2, 2 );
00085   WSADATA data;
00086   WSAStartup( version, &data );
00087 #else
00088   struct sigaction sa;
00089   sa.sa_handler = SIG_IGN;
00090   sigemptyset( &sa.sa_mask );
00091   sa.sa_flags = 0;
00092   sigaction( SIGPIPE, &sa, 0 );
00093 #endif
00094 }
00095 
00096 void socket_term()
00097 {
00098 #ifdef _MSC_VER
00099   WSACleanup();
00100 #endif
00101 }
00102 
00103 int socket_createAcceptor(int port, bool reuse)
00104 {
00105   int socket = ::socket( PF_INET, SOCK_STREAM, 0 );
00106   if ( socket < 0 ) return -1;
00107 
00108   sockaddr_in address;
00109   socklen_t socklen;
00110 
00111   address.sin_family = PF_INET;
00112   address.sin_port = htons( port );
00113   address.sin_addr.s_addr = INADDR_ANY;
00114   socklen = sizeof( address );
00115   if( reuse )
00116     socket_setsockopt( socket, SO_REUSEADDR );
00117 
00118   int result = bind( socket, reinterpret_cast < sockaddr* > ( &address ),
00119                      socklen );
00120   if ( result < 0 ) return -1;
00121   result = listen( socket, SOMAXCONN );
00122   if ( result < 0 ) return -1;
00123   return socket;
00124 }
00125 
00126 int socket_createConnector()
00127 {
00128   return ::socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
00129 }
00130 
00131 int socket_connect( int socket, const char* address, int port )
00132 {
00133   const char* hostname = socket_hostname( address );
00134   if( hostname == 0 ) return -1;
00135 
00136   sockaddr_in addr;
00137   addr.sin_family = PF_INET;
00138   addr.sin_port = htons( port );
00139   addr.sin_addr.s_addr = inet_addr( hostname );
00140 
00141   int result = connect( socket, reinterpret_cast < sockaddr* > ( &addr ),
00142                         sizeof( addr ) );
00143 
00144   return result;
00145 }
00146 
00147 int socket_accept( int s )
00148 {
00149   if ( !socket_isValid( s ) ) return -1;
00150   return accept( s, 0, 0 );
00151 }
00152 
00153 int socket_send( int s, const char* msg, int length )
00154 {
00155   return send( s, msg, length, 0 );
00156 }
00157 
00158 void socket_close( int s )
00159 {
00160   shutdown( s, 2 );
00161 #ifdef _MSC_VER
00162   closesocket( s );
00163 #else
00164   close( s );
00165 #endif
00166 }
00167 
00168 bool socket_fionread( int s, int& bytes )
00169 {
00170   bytes = 0;
00171 #if defined(_MSC_VER)
00172   return ::ioctlsocket( s, FIONREAD, &( ( unsigned long& ) bytes ) ) == 0;
00173 #elif defined(USING_STREAMS)
00174   return ::ioctl( s, I_NREAD, &bytes ) >= 0;
00175 #else
00176   return ::ioctl( s, FIONREAD, &bytes ) == 0;
00177 #endif
00178 }
00179 
00180 bool socket_disconnected( int s )
00181 {
00182   char byte;
00183   return ::recv (s, &byte, sizeof (byte), MSG_PEEK) <= 0;
00184 }
00185 
00186 int socket_setsockopt( int s, int opt )
00187 {
00188 #ifdef _MSC_VER
00189   BOOL optval = TRUE;
00190 #else
00191   int optval = 1;
00192 #endif
00193   return socket_setsockopt( s, opt, optval );
00194 }
00195 
00196 int socket_setsockopt( int s, int opt, int optval )
00197 {
00198   int level = SOL_SOCKET;
00199   if( opt == TCP_NODELAY )
00200     level = IPPROTO_TCP;
00201 
00202 #ifdef _MSC_VER
00203   return ::setsockopt( s, level, opt,
00204                        ( char* ) & optval, sizeof( optval ) );
00205 #else
00206   return ::setsockopt( s, level, opt,
00207                        &optval, sizeof( optval ) );
00208 #endif
00209 }
00210 
00211 int socket_getsockopt( int s, int opt, int& optval )
00212 {
00213   int level = SOL_SOCKET;
00214   if( opt == TCP_NODELAY )
00215     level = IPPROTO_TCP;
00216 
00217 #ifdef _MSC_VER
00218   int length = sizeof(int);
00219 #else
00220   socklen_t length = sizeof(socklen_t);
00221 #endif
00222 
00223   return ::getsockopt( s, level, opt, 
00224                        ( char* ) & optval, & length );
00225 }
00226 
00227 #ifndef _MSC_VER
00228 int socket_fcntl( int s, int opt, int arg )
00229 {
00230   return ::fcntl( s, opt, arg );
00231 }
00232 
00233 int socket_getfcntlflag( int s, int arg )
00234 {
00235   return socket_fcntl( s, F_GETFL, arg );
00236 }
00237 
00238 int socket_setfcntlflag( int s, int arg )
00239 {
00240   int oldValue = socket_getfcntlflag( s, arg );
00241   oldValue |= arg;
00242   return socket_fcntl( s, F_SETFL, arg );
00243 }
00244 #endif
00245 
00246 void socket_setnonblock( int socket )
00247 {
00248 #ifdef _MSC_VER
00249   u_long opt = 1;
00250   ::ioctlsocket( socket, FIONBIO, &opt );
00251 #else
00252   socket_setfcntlflag( socket, O_NONBLOCK );
00253 #endif
00254 }
00255 bool socket_isValid( int socket )
00256 {
00257 #ifdef _MSC_VER
00258   return socket != INVALID_SOCKET;
00259 #else
00260   return socket >= 0;
00261 #endif
00262 }
00263 
00264 #ifndef _MSC_VER
00265 bool socket_isBad( int s )
00266 {
00267   struct stat buf;
00268   fstat( s, &buf );
00269   return errno == EBADF;
00270 }
00271 #endif
00272 
00273 void socket_invalidate( int& socket )
00274 {
00275 #ifdef _MSC_VER
00276   socket = INVALID_SOCKET;
00277 #else
00278   socket = -1;
00279 #endif
00280 }
00281 
00282 short socket_hostport( int socket )
00283 {
00284   struct sockaddr_in addr;
00285   socklen_t len = sizeof(addr);
00286   if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
00287     return 0;
00288 
00289   return ntohs( addr.sin_port );
00290 }
00291 
00292 const char* socket_hostname( int socket )
00293 {
00294   struct sockaddr_in addr;
00295   socklen_t len = sizeof(addr);
00296   if( getsockname(socket, (struct sockaddr*)&addr, &len) < 0 )
00297     return 0;
00298 
00299   return inet_ntoa( addr.sin_addr );
00300 }
00301 
00302 const char* socket_hostname( const char* name )
00303 {
00304   struct hostent* host_ptr = 0;
00305   struct in_addr** paddr;
00306   struct in_addr saddr;
00307 
00308 #if( GETHOSTBYNAME_R_INPUTS_RESULT || GETHOSTBYNAME_R_RETURNS_RESULT )
00309   hostent host;
00310   char buf[1024];
00311   int error;
00312 #endif
00313 
00314   saddr.s_addr = inet_addr( name );
00315   if ( saddr.s_addr != ( unsigned ) - 1 ) return name;
00316 
00317 #if GETHOSTBYNAME_R_INPUTS_RESULT
00318   gethostbyname_r( name, &host, buf, sizeof(buf), &host_ptr, &error );
00319 #elif GETHOSTBYNAME_R_RETURNS_RESULT
00320   host_ptr = gethostbyname_r( name, &host, buf, sizeof(buf), &error );
00321 #else
00322   host_ptr = gethostbyname( name );
00323 #endif
00324 
00325   if ( host_ptr == 0 ) return 0;
00326 
00327   paddr = ( struct in_addr ** ) host_ptr->h_addr_list;
00328   return inet_ntoa( **paddr );
00329 }
00330 
00331 const char* socket_peername( int socket )
00332 {
00333   struct sockaddr_in addr;
00334   socklen_t len = sizeof(addr);
00335   if( getpeername( socket, (struct sockaddr*)&addr, &len ) < 0 )
00336     return "UNKNOWN";
00337   char* result = inet_ntoa( addr.sin_addr );
00338   if( result )
00339     return result;
00340   else
00341     return "UNKNOWN";
00342 }
00343 
00344 std::pair<int, int> socket_createpair()
00345 {
00346 #ifdef _MSC_VER
00347   int acceptor = socket_createAcceptor(0, true);
00348   const char* host = socket_hostname( acceptor );
00349   short port = socket_hostport( acceptor );
00350   int client = socket_createConnector();
00351   socket_connect( client, "localhost", port );
00352   int server = socket_accept( acceptor );
00353   return std::pair<int, int>( client, server );
00354 #else
00355   int pair[2];
00356   socketpair( AF_UNIX, SOCK_STREAM, 0, pair );
00357   return std::pair<int, int>( pair[0], pair[1] );
00358 #endif
00359 }
00360 
00361 tm time_gmtime( const time_t* t )
00362 {
00363 #ifdef _MSC_VER
00364   #if( _MSC_VER >= 1400 )
00365     tm result;
00366     gmtime_s( &result, t );
00367     return result;
00368   #else
00369     return *gmtime( t );
00370   #endif
00371 #else
00372   tm result;
00373   return *gmtime_r( t, &result );
00374 #endif
00375 }
00376 
00377 tm time_localtime( const time_t* t)
00378 {
00379 #ifdef _MSC_VER
00380   #if( _MSC_VER >= 1400 )
00381     tm result;
00382     localtime_s( &result, t );
00383     return result;
00384   #else
00385     return *localtime( t );
00386   #endif
00387 #else
00388   tm result;
00389   return *localtime_r( t, &result );
00390 #endif
00391 }
00392 
00393 bool thread_spawn( THREAD_START_ROUTINE func, void* var, thread_id& thread )
00394 {
00395 #ifdef _MSC_VER
00396   thread_id result = 0;
00397   unsigned int id = 0;
00398   result = _beginthreadex( NULL, 0, &func, var, 0, &id );
00399   if ( result == 0 ) return false;
00400 #else
00401   thread_id result = 0;
00402   if( pthread_create( &result, 0, func, var ) != 0 ) return false;
00403 #endif
00404   thread = result;
00405   return true;
00406 }
00407 
00408 bool thread_spawn( THREAD_START_ROUTINE func, void* var )
00409 { 
00410   thread_id thread = 0;
00411   return thread_spawn( func, var, thread );
00412 }
00413 
00414 void thread_join( thread_id thread )
00415 {
00416 #ifdef _MSC_VER
00417   WaitForSingleObject( ( void* ) thread, INFINITE );
00418   CloseHandle((HANDLE)thread);
00419 #else
00420   pthread_join( ( pthread_t ) thread, 0 );
00421 #endif
00422 }
00423 
00424 void thread_detach( thread_id thread )
00425 {
00426 #ifdef _MSC_VER
00427   CloseHandle((HANDLE)thread);
00428 #else
00429   pthread_t t = thread;
00430   pthread_detach( t );
00431 #endif
00432 }
00433 
00434 thread_id thread_self()
00435 {
00436 #ifdef _MSC_VER
00437   return (unsigned)GetCurrentThread();
00438 #else
00439   return pthread_self();
00440 #endif
00441 }
00442 
00443 void process_sleep( double s )
00444 {
00445 #ifdef _MSC_VER
00446   Sleep( (long)(s * 1000) );
00447 #else
00448   timespec time, remainder;
00449   double intpart;
00450   time.tv_nsec = (long)(modf(s, &intpart) * 1e9);
00451   time.tv_sec = (int)intpart;
00452   while( nanosleep(&time, &remainder) == -1 )
00453   time = remainder;
00454 #endif
00455 }
00456 
00457 std::string file_separator()
00458 {
00459 #ifdef _MSC_VER
00460   return "\\";
00461 #else
00462   return "/";
00463 #endif
00464 }
00465 
00466 void file_mkdir( const char* path )
00467 {
00468   int length = strlen( path );
00469   std::string createPath = "";
00470 
00471   for( const char* pos = path; pos - path <= length; ++pos )
00472   {
00473     createPath += *pos;
00474     if( *pos == '/' || *pos == '\\' || pos - path == length )
00475     {
00476     #ifdef _MSC_VER
00477       _mkdir( createPath.c_str() );
00478     #else
00479       // use umask to override rwx for all
00480       mkdir( createPath.c_str(), 0777 );
00481     #endif
00482     }
00483   }
00484 }
00485 
00486 FILE* file_fopen( const char* path, const char* mode )
00487 {
00488 #if( _MSC_VER >= 1400 )
00489   FILE* result = 0;
00490   fopen_s( &result, path, mode );
00491   return result;
00492 #else
00493   return fopen( path, mode );
00494 #endif
00495 }
00496 
00497 void file_fclose( FILE* file )
00498 {
00499   fclose( file );
00500 }
00501 
00502 bool file_exists( const char* path )
00503 {
00504   std::ifstream stream;
00505   stream.open( path, std::ios_base::in );
00506   if( stream.is_open() )
00507   {
00508     stream.close();
00509     return true;
00510   }
00511   return false;
00512 }
00513 
00514 void file_unlink( const char* path )
00515 {
00516 #ifdef _MSC_VER
00517   _unlink( path );
00518 #else
00519   unlink( path );
00520 #endif
00521 }
00522 
00523 int file_rename( const char* oldpath, const char* newpath )
00524 {
00525   return rename( oldpath, newpath );
00526 }
00527 
00528 std::string file_appendpath( const std::string& path, const std::string& file )
00529 {
00530   const char last = path[path.size()-1];
00531   if( last == '/' || last == '\\' )
00532     return std::string(path) + file;
00533   else
00534     return std::string(path) + file_separator() + file;
00535 }
00536 }

Generated on Mon Jun 23 2014 23:49:38 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001