Utility.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_UTILITY_H
  21. #define FIX_UTILITY_H
  22. #ifdef _MSC_VER
  23. #pragma warning( disable : 4503 4355 4786 4290 )
  24. #endif
  25. #ifndef _MSC_VER
  26. #include "Allocator.h"
  27. #endif
  28. #ifdef HAVE_STLPORT
  29. #define ALLOCATOR std::allocator
  30. #elif ENABLE_DEBUG_ALLOCATOR
  31. #include <ext/debug_allocator.h>
  32. #define ALLOCATOR __gnu_cxx::debug_allocator
  33. #elif ENABLE_NEW_ALLOCATOR
  34. #include <ext/new_allocator.h>
  35. #define ALLOCATOR __gnu_cxx::new_allocator
  36. #elif ENABLE_BOOST_FAST_POOL_ALLOCATOR
  37. #include <boost/pool/pool_alloc.hpp>
  38. #define ALLOCATOR boost::fast_pool_allocator
  39. #elif ENABLE_MT_ALLOCATOR
  40. #include <ext/mt_allocator.h>
  41. #define ALLOCATOR __gnu_cxx::__mt_alloc
  42. #elif ENABLE_BOOST_POOL_ALLOCATOR
  43. #include <boost/pool/pool_alloc.hpp>
  44. #define ALLOCATOR boost::pool_allocator
  45. #elif ENABLE_POOL_ALLOCATOR
  46. #include <ext/pool_allocator.h>
  47. #define ALLOCATOR __gnu_cxx::__pool_alloc
  48. #elif ENABLE_BITMAP_ALLOCATOR
  49. #include <ext/bitmap_allocator.h>
  50. #define ALLOCATOR __gnu_cxx::bitmap_allocator
  51. #elif ENABLE_TBB_ALLOCATOR
  52. #include <tbb/scalable_allocator.h>
  53. #define ALLOCATOR tbb::scalable_allocator
  54. #else
  55. #define ALLOCATOR std::allocator
  56. #endif
  57. #ifdef _MSC_VER
  58. /////////////////////////////////////////////
  59. #include <Winsock2.h>
  60. #include <process.h>
  61. #include <direct.h>
  62. #include <time.h>
  63. typedef int socklen_t;
  64. /////////////////////////////////////////////
  65. #else
  66. /////////////////////////////////////////////
  67. #include <sys/types.h>
  68. #include <sys/socket.h>
  69. #include <sys/ioctl.h>
  70. #include <sys/time.h>
  71. #include <sys/stat.h>
  72. #include <netinet/in.h>
  73. #include <netinet/tcp.h>
  74. #include <arpa/inet.h>
  75. #include <netdb.h>
  76. #include <fcntl.h>
  77. #include <unistd.h>
  78. #include <pthread.h>
  79. #include <signal.h>
  80. #include <errno.h>
  81. #include <time.h>
  82. #include <stdlib.h>
  83. /////////////////////////////////////////////
  84. #endif
  85. #include <string>
  86. #include <cstring>
  87. #include <cctype>
  88. #include <ctime>
  89. #include <cstdio>
  90. #include <cstdlib>
  91. namespace FIX
  92. {
  93. void string_replace( const std::string& oldValue,
  94. const std::string& newValue,
  95. std::string& value );
  96. std::string string_toLower( const std::string& value );
  97. std::string string_toUpper( const std::string& value );
  98. std::string string_strip( const std::string& value );
  99. void socket_init();
  100. void socket_term();
  101. int socket_createAcceptor( int port, bool reuse = false );
  102. int socket_createConnector();
  103. int socket_connect( int s, const char* address, int port );
  104. int socket_accept( int s );
  105. int socket_send( int s, const char* msg, int length );
  106. void socket_close( int s );
  107. bool socket_fionread( int s, int& bytes );
  108. bool socket_disconnected( int s );
  109. int socket_setsockopt( int s, int opt );
  110. int socket_setsockopt( int s, int opt, int optval );
  111. int socket_getsockopt( int s, int opt, int& optval );
  112. #ifndef _MSC_VER
  113. int socket_fcntl( int s, int opt, int arg );
  114. int socket_getfcntlflag( int s, int arg );
  115. int socket_setfcntlflag( int s, int arg );
  116. #endif
  117. void socket_setnonblock( int s );
  118. bool socket_isValid( int socket );
  119. #ifndef _MSC_VER
  120. bool socket_isBad( int s );
  121. #endif
  122. void socket_invalidate( int& socket );
  123. short socket_hostport( int socket );
  124. const char* socket_hostname( int socket );
  125. const char* socket_hostname( const char* name );
  126. const char* socket_peername( int socket );
  127. std::pair<int, int> socket_createpair();
  128. tm time_gmtime( const time_t* t );
  129. tm time_localtime( const time_t* t );
  130. #ifdef _MSC_VER
  131. typedef unsigned int (_stdcall THREAD_START_ROUTINE)(void *);
  132. #define THREAD_PROC unsigned int _stdcall
  133. #else
  134. extern "C" { typedef void * (THREAD_START_ROUTINE)(void *); }
  135. #define THREAD_PROC void *
  136. #endif
  137. #ifdef _MSC_VER
  138. typedef unsigned thread_id;
  139. #else
  140. typedef pthread_t thread_id;
  141. #endif
  142. bool thread_spawn( THREAD_START_ROUTINE func, void* var, thread_id& thread );
  143. bool thread_spawn( THREAD_START_ROUTINE func, void* var );
  144. void thread_join( thread_id thread );
  145. void thread_detach( thread_id thread );
  146. thread_id thread_self();
  147. void process_sleep( double s );
  148. std::string file_separator();
  149. void file_mkdir( const char* path );
  150. FILE* file_fopen( const char* path, const char* mode );
  151. void file_fclose( FILE* file );
  152. bool file_exists( const char* path );
  153. void file_unlink( const char* path );
  154. int file_rename( const char* oldpath, const char* newpath );
  155. std::string file_appendpath( const std::string& path, const std::string& file );
  156. }
  157. #if( _MSC_VER >= 1400 )
  158. #define HAVE_FSCANF_S 1
  159. #define FILE_FSCANF fscanf_s
  160. #else
  161. #define FILE_FSCANF fscanf
  162. #endif
  163. #if( _MSC_VER >= 1400 )
  164. #define HAVE_SPRINTF_S 1
  165. #define STRING_SPRINTF sprintf_s
  166. #else
  167. #define STRING_SPRINTF sprintf
  168. #endif
  169. #if (!defined(_MSC_VER) || (_MSC_VER >= 1300)) && !defined(HAVE_STLPORT)
  170. using std::abort;
  171. using std::sprintf;
  172. using std::atoi;
  173. using std::atol;
  174. using std::atof;
  175. using std::isdigit;
  176. using std::strcmp;
  177. using std::strftime;
  178. using std::strlen;
  179. using std::abs;
  180. using std::labs;
  181. using std::memcpy;
  182. using std::memset;
  183. using std::exit;
  184. using std::strtod;
  185. using std::strtol;
  186. using std::strerror;
  187. #endif
  188. #endif