Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
4.7 KiB

  1. #include "precomp.h"
  2. #include "Message.h"
  3. CChatMessage *CChatMessage::ms_pFirst = NULL;
  4. int CChatMessage::ms_cMessages = 0;
  5. CChatMessage *CChatMessage::ms_pLast = NULL;
  6. int CChatMessage::ms_iMessageLimit = INITIAL_LIMIT;
  7. CChatMessage *CChatMessage::get_head()
  8. {
  9. return ms_pFirst;
  10. }
  11. int CChatMessage::get_count()
  12. {
  13. return ms_cMessages;
  14. }
  15. CChatMessage *CChatMessage::get_last()
  16. {
  17. return ms_pLast;
  18. }
  19. void CChatMessage::put_limit( int iLimit )
  20. {
  21. ASSERT( iLimit >= 1 );
  22. ms_iMessageLimit = iLimit;
  23. while( ms_cMessages > ms_iMessageLimit )
  24. {
  25. CChatMessage *pKillMe = ms_pFirst;
  26. ms_pFirst = ms_pFirst->m_pNext;
  27. delete pKillMe;
  28. }
  29. }
  30. int CChatMessage::get_limit()
  31. {
  32. return ms_iMessageLimit;
  33. }
  34. CChatMessage *CChatMessage::get_next() const
  35. {
  36. return m_pNext;
  37. }
  38. CChatMessage *CChatMessage::get_prev() const
  39. {
  40. return m_pPrev;
  41. }
  42. CChatMessage::CHAT_MSGTYPE CChatMessage::get_type() const
  43. {
  44. return m_msgType;
  45. }
  46. const LPTSTR CChatMessage::get_date() const
  47. {
  48. return m_szDate;
  49. }
  50. const LPTSTR CChatMessage::get_time() const
  51. {
  52. return m_szTime;
  53. }
  54. const LPTSTR CChatMessage::get_person() const
  55. {
  56. return m_szPerson;
  57. }
  58. const LPTSTR CChatMessage::get_message() const
  59. {
  60. return m_szMessage;
  61. }
  62. CChatMessage::CChatMessage( LPCTSTR szPerson, LPCTSTR szMessage, CHAT_MSGTYPE msgType )
  63. : m_msgType( msgType ), m_szDate( NULL ), m_szTime( NULL ),
  64. m_szPerson( NULL ), m_szMessage( NULL ), m_pNext( NULL ), m_pPrev( NULL )
  65. {
  66. _GetDate();
  67. _GetTime();
  68. m_szPerson = _CopyString( szPerson );
  69. m_szMessage = _CopyString( szMessage );
  70. if( 0 == ms_cMessages )
  71. {
  72. ms_pFirst = this;
  73. ms_pLast = this;
  74. }
  75. else
  76. {
  77. ms_pLast->m_pNext = this;
  78. m_pPrev = ms_pLast;
  79. ms_pLast = this;
  80. }
  81. ms_cMessages++;
  82. while( ms_cMessages > ms_iMessageLimit )
  83. {
  84. CChatMessage *pKillMe = ms_pFirst;
  85. ms_pFirst = ms_pFirst->m_pNext;
  86. delete pKillMe;
  87. }
  88. }
  89. CChatMessage::~CChatMessage()
  90. {
  91. delete [] m_szDate;
  92. delete [] m_szTime;
  93. delete [] m_szPerson;
  94. delete [] m_szMessage;
  95. ms_cMessages--;
  96. }
  97. void CChatMessage::DeleteAll()
  98. {
  99. CChatMessage *pMsg = ms_pFirst;
  100. while( pMsg )
  101. {
  102. CChatMessage *pNext = pMsg->m_pNext;
  103. delete pMsg;
  104. pMsg = pNext;
  105. }
  106. ms_pFirst = NULL;
  107. ms_pLast = NULL;
  108. ms_cMessages = 0;
  109. }
  110. LPTSTR CChatMessage::_CopyString( LPCTSTR sz )
  111. {
  112. LPTSTR szNew = NULL;
  113. if( NULL == sz )
  114. {
  115. DBG_SAVE_FILE_LINE
  116. szNew = new TCHAR[1];
  117. ASSERT( szNew );
  118. if( NULL == szNew )
  119. {
  120. return NULL;
  121. }
  122. szNew[0] = '\0';
  123. }
  124. else
  125. {
  126. int iLen = lstrlen( sz ) + 1;
  127. DBG_SAVE_FILE_LINE
  128. szNew = new TCHAR[ iLen ];
  129. ASSERT( szNew );
  130. if( NULL == szNew )
  131. {
  132. return NULL;
  133. }
  134. lstrcpyn( szNew, sz, iLen );
  135. }
  136. return szNew;
  137. }
  138. void CChatMessage::_GetDate()
  139. {
  140. int iLen = 1 + GetDateFormat(
  141. LOCALE_USER_DEFAULT, // locale for which date is to be formatted
  142. 0, // flags specifying function options
  143. NULL, // time to be formatted
  144. NULL, // time format string
  145. NULL, // buffer for storing formatted string
  146. 0 // size, in bytes or characters, of the buffer
  147. );
  148. m_szDate = new TCHAR[ iLen ];
  149. ASSERT( m_szDate );
  150. if( 0 == (iLen = GetDateFormat(
  151. LOCALE_USER_DEFAULT, // locale for which date is to be formatted
  152. 0, // flags specifying function options
  153. NULL, // time to be formatted
  154. NULL, // time format string
  155. m_szDate, // buffer for storing formatted string
  156. iLen // size, in bytes or characters, of the buffer
  157. ) ) )
  158. {
  159. DWORD dw = GetLastError();
  160. WARNING_OUT(( TEXT("CChatMessage::_GetDate: Can not get date") ));
  161. }
  162. else
  163. {
  164. m_szDate[ iLen ] = '\0';
  165. }
  166. }
  167. void CChatMessage::_GetTime(void)
  168. {
  169. int iLen = 1 + GetTimeFormat(
  170. LOCALE_USER_DEFAULT, // locale for which time is to be formatted
  171. 0, // flags specifying function options
  172. NULL, // time to be formatted
  173. NULL, // time format string
  174. NULL, // buffer for storing formatted string
  175. 0 // size, in bytes or characters, of the buffer
  176. );
  177. m_szTime = new TCHAR[ iLen ];
  178. ASSERT( m_szTime );
  179. if( 0 == (iLen = GetTimeFormat(
  180. LOCALE_USER_DEFAULT, // locale for which time is to be formatted
  181. 0, // flags specifying function options
  182. NULL, // time to be formatted
  183. NULL, // time format string
  184. m_szTime, // buffer for storing formatted string
  185. iLen // size, in bytes or characters, of the buffer
  186. ) ) )
  187. {
  188. DWORD dw = GetLastError();
  189. WARNING_OUT(( TEXT("CChatMessage::_GetTime: Can not get time") ));
  190. }
  191. else
  192. {
  193. m_szTime[ iLen ] = '\0';
  194. }
  195. }