PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Sources/Network/ESClient/Utils.cpp

http://mmo-resourse.googlecode.com/
C++ | 454 lines | 324 code | 117 blank | 13 comment | 40 complexity | 26cff5a285fc5ba2b182046ea63a3705 MD5 | raw file
Possible License(s): Zlib, LGPL-2.1, LGPL-2.0
  1. #include "Utils.h"
  2. #include "Exception.h"
  3. #include "CriticalSection.h"
  4. #include <memory>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <conio.h>
  8. #ifdef _UNICODE
  9. typedef std::wfstream _tfstream;
  10. #else
  11. typedef std::fstream _tfstream;
  12. #endif
  13. #include "Lmcons.h" // define UNLEN
  14. #define NONE_PRINTF_TRACE
  15. /*
  16. * Using directives
  17. */
  18. using std::auto_ptr;
  19. using std::endl;
  20. /*
  21. * namespace OnlineGameLib::Win32
  22. */
  23. namespace OnlineGameLib {
  24. namespace Win32 {
  25. CCriticalSection s_criticalSection;
  26. static _tfstream s_debugOut;
  27. static std::string s_logFileName = "\\NWS.log";
  28. void SetLogFileName( const _tstring &name )
  29. {
  30. USES_CONVERSION;
  31. if ( s_debugOut.is_open() )
  32. {
  33. s_debugOut.close();
  34. }
  35. s_logFileName = T2A( const_cast<PTSTR>( name.c_str() ) );
  36. }
  37. void Output( const _tstring &message )
  38. {
  39. #ifdef NONE_PRINTF_TRACE
  40. return;
  41. #endif
  42. #ifdef _DEBUG
  43. CCriticalSection::Owner lock( s_criticalSection );
  44. #ifdef _UNICODE
  45. std::wcout << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  46. #else
  47. std::cout << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  48. #endif
  49. const _tstring msg = ToString( GetCurrentThreadId() ) + _T(": ") + message + _T("\n");
  50. cprintf( msg.c_str() );
  51. // OutputDebugString( msg.c_str() );
  52. #ifdef TRACE2FILE
  53. if ( !s_debugOut.is_open() )
  54. {
  55. s_debugOut.open( s_logFileName.c_str(), std::ios_base::out | std::ios_base::app );
  56. s_debugOut << _T( "************ OnlineGame newest log *************" ) << endl;
  57. }
  58. s_debugOut << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  59. #endif // TRACE2FILE
  60. #else
  61. /*
  62. * symbol 'message' not referenced
  63. */
  64. #endif //_DEBUG
  65. }
  66. void OutPutInfo( const _tstring &message )
  67. {
  68. #ifdef NONE_PRINTF_TRACE
  69. return;
  70. #endif
  71. CCriticalSection::Owner lock( s_criticalSection );
  72. #ifdef _UNICODE
  73. std::wcout << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  74. #else
  75. std::cout << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  76. #endif
  77. const _tstring msg = ToString( GetCurrentThreadId() ) + _T(": ") + message + _T("\n");
  78. OutputDebugString( msg.c_str() );
  79. }
  80. void Trace2File( const _tstring &message )
  81. {
  82. #ifdef NONE_PRINTF_TRACE
  83. return;
  84. #endif
  85. if ( !s_debugOut.is_open() )
  86. {
  87. s_debugOut.open( s_logFileName.c_str(), std::ios_base::out | std::ios_base::app );
  88. s_debugOut << _T( "************ OnlineGame newest log *************" ) << endl;
  89. }
  90. s_debugOut << ToString( GetCurrentThreadId() ) << _T(": ") << message << endl;
  91. }
  92. _tstring GetLastErrorMessage( DWORD last_error )
  93. {
  94. static TCHAR errmsg[520];
  95. static size_t len = strlen( "[0x]" ) + sizeof( DWORD );
  96. sprintf( errmsg, "[0x%X]", last_error );
  97. errmsg[len] = '\0';
  98. if ( !FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
  99. 0,
  100. last_error,
  101. 0,
  102. &errmsg[len],
  103. 511,
  104. NULL) )
  105. {
  106. /*
  107. * if we fail, call ourself to find out why and return that error
  108. */
  109. return ( GetLastErrorMessage( GetLastError() ) );
  110. }
  111. return errmsg;
  112. }
  113. _tstring HexToString( const BYTE *pBuffer, size_t iBytes )
  114. {
  115. _tstring result;
  116. for ( size_t i = 0; i < iBytes; i++ )
  117. {
  118. BYTE c ;
  119. BYTE b = pBuffer[i] >> 4;
  120. if ( 9 >= b )
  121. {
  122. c = b + '0';
  123. }
  124. else
  125. {
  126. c = (b - 10) + 'A';
  127. }
  128. result += (TCHAR)c;
  129. b = pBuffer[i] & 0x0f;
  130. if ( 9 >= b )
  131. {
  132. c = b + '0';
  133. }
  134. else
  135. {
  136. c = (b - 10) + 'A';
  137. }
  138. result += (TCHAR)c;
  139. }
  140. return result;
  141. }
  142. void StringToHex( const _tstring &ts, BYTE *pBuffer, size_t nBytes )
  143. {
  144. USES_CONVERSION;
  145. const std::string s = T2A( const_cast<PTSTR>(ts.c_str()) );
  146. for ( size_t i = 0; i < nBytes; i++ )
  147. {
  148. const size_t stringOffset = i * 2;
  149. BYTE val = 0;
  150. const BYTE b = s[stringOffset];
  151. if ( isdigit( b ) )
  152. {
  153. val = (BYTE)( (b - '0') * 16 );
  154. }
  155. else
  156. {
  157. val = (BYTE)( ( ( toupper(b) - 'A' ) + 10 ) * 16 );
  158. }
  159. const BYTE b1 = s[stringOffset + 1];
  160. if ( isdigit( b1 ) )
  161. {
  162. val += b1 - '0' ;
  163. }
  164. else
  165. {
  166. val += (BYTE)( ( toupper(b1) - 'A' ) + 10 );
  167. }
  168. pBuffer[i] = val;
  169. }
  170. }
  171. _tstring GetCurrentDirectory()
  172. {
  173. DWORD size = ::GetCurrentDirectory( 0, 0 );
  174. auto_ptr<TCHAR> spBuf(new TCHAR[size]);
  175. if ( 0 == ::GetCurrentDirectory( size, spBuf.get() ) )
  176. {
  177. throw CException( _T("GetCurrentDirectory()"), _T("Failed to get current directory") );
  178. }
  179. return _tstring( spBuf.get() );
  180. }
  181. _tstring GetDateStamp()
  182. {
  183. SYSTEMTIME systime;
  184. GetSystemTime(&systime);
  185. static TCHAR buffer[7];
  186. _stprintf( buffer, _T("%02d%02d%02d"),
  187. systime.wDay,
  188. systime.wMonth,
  189. ( 1900 + systime.wYear) % 100 );
  190. return buffer;
  191. }
  192. _tstring GetTimeStamp()
  193. {
  194. SYSTEMTIME systime;
  195. GetLocalTime(&systime);
  196. static TCHAR buffer[9];
  197. _stprintf( buffer, _T("%02d:%02d:%02d"),
  198. systime.wHour,
  199. systime.wMinute,
  200. systime.wSecond );
  201. return buffer;
  202. }
  203. _tstring ToHex( BYTE c )
  204. {
  205. TCHAR hex[3];
  206. const int val = c;
  207. _stprintf( hex, _T("%02X"), val );
  208. return hex;
  209. }
  210. _tstring DumpData( const BYTE * const pData, size_t dataLength, size_t lineLength /* = 0 */ )
  211. {
  212. #ifdef NONE_PRINTF_TRACE
  213. return ( _tstring )( "" );
  214. #endif
  215. const size_t bytesPerLine = lineLength != 0 ? (lineLength - 1) / 3 : 0;
  216. _tstring result;
  217. _tstring hexDisplay;
  218. _tstring display;
  219. size_t i = 0;
  220. while ( i < dataLength )
  221. {
  222. const BYTE c = pData[i++];
  223. hexDisplay += ToHex(c) + _T(" ");
  224. if ( isprint( c ) )
  225. {
  226. display += (TCHAR)c;
  227. }
  228. else
  229. {
  230. display += _T('.');
  231. }
  232. if ( ( bytesPerLine && ( i % bytesPerLine == 0 && i != 0 ) ) || i == dataLength )
  233. {
  234. result += hexDisplay + _T(" - ") + display + _T("\n");
  235. hexDisplay = _T("");
  236. display = _T("");
  237. }
  238. }
  239. return result;
  240. }
  241. _tstring GetComputerName()
  242. {
  243. static bool gotName = false;
  244. static _tstring name = _T("UNAVAILABLE!");
  245. if ( !gotName )
  246. {
  247. TCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1] ;
  248. DWORD computerNameLen = MAX_COMPUTERNAME_LENGTH ;
  249. if ( ::GetComputerName( computerName, &computerNameLen ) )
  250. {
  251. name = computerName;
  252. }
  253. gotName = true;
  254. }
  255. return name;
  256. }
  257. _tstring GetModuleFileName( HINSTANCE hModule /* = 0 */ )
  258. {
  259. static bool gotName = false;
  260. static _tstring name = _T("UNAVAILABLE!");
  261. if ( !gotName )
  262. {
  263. TCHAR moduleFileName[MAX_PATH + 1] ;
  264. DWORD moduleFileNameLen = MAX_PATH ;
  265. if ( ::GetModuleFileName( hModule, moduleFileName, moduleFileNameLen ) )
  266. {
  267. name = moduleFileName;
  268. }
  269. gotName = true;
  270. }
  271. return name;
  272. }
  273. _tstring GetUserName()
  274. {
  275. static bool gotName = false;
  276. static _tstring name = _T("UNAVAILABLE!");
  277. if ( !gotName )
  278. {
  279. TCHAR userName[UNLEN + 1] ;
  280. DWORD userNameLen = UNLEN;
  281. if ( ::GetUserName( userName, &userNameLen ) )
  282. {
  283. name = userName;
  284. }
  285. gotName = true;
  286. }
  287. return name;
  288. }
  289. _tstring StripLeading( const _tstring &source, const char toStrip )
  290. {
  291. const TCHAR *pSrc = source.c_str();
  292. while ( pSrc && *pSrc == toStrip )
  293. {
  294. ++pSrc;
  295. }
  296. return pSrc;
  297. }
  298. _tstring StripTrailing( const _tstring &source, const char toStrip )
  299. {
  300. size_t i = source.length();
  301. const _TCHAR *pSrc = source.c_str() + i;
  302. --pSrc;
  303. while ( i && *pSrc == toStrip )
  304. {
  305. --pSrc;
  306. --i;
  307. }
  308. return source.substr( 0, i );
  309. }
  310. #pragma comment( lib, "Version.lib" )
  311. _tstring GetFileVersion()
  312. {
  313. _tstring version;
  314. const _tstring moduleFileName = GetModuleFileName( NULL );
  315. LPTSTR pModuleFileName = const_cast<LPTSTR>( moduleFileName.c_str() );
  316. DWORD zero = 0;
  317. DWORD verSize = ::GetFileVersionInfoSize( pModuleFileName, &zero );
  318. if ( verSize != 0 )
  319. {
  320. auto_ptr<BYTE> spBuffer( new BYTE[verSize] );
  321. if ( ::GetFileVersionInfo( pModuleFileName, 0, verSize, spBuffer.get() ) )
  322. {
  323. LPTSTR pVersion = 0;
  324. UINT verLen = 0;
  325. if ( ::VerQueryValue(spBuffer.get(),
  326. const_cast<LPTSTR>(_T("\\StringFileInfo\\080904b0\\ProductVersion")),
  327. (void**)&pVersion,
  328. &verLen) )
  329. {
  330. version = pVersion;
  331. }
  332. }
  333. }
  334. return version;
  335. }
  336. } // End of namespace OnlineGameLib
  337. } // End of namespace Win32