AfxCore.cpp
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:9k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. /*
  2. Cross Platform Core Code.
  3. Copyright(R) 2001-2002 Balang Software.
  4. All rights reserved.
  5. Using:
  6. some afx-global-functions;
  7. */
  8. #include "stdafx.h"
  9. #include "../Include/AfxCore.h"
  10. #include <ctype.h>
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #endif
  18. ////////////////////////////////////////////////////////////////////////////////////
  19. // Chinese To English Translation
  20. BOOL AfxIsEnglish( const char * sz, int maxlen, float ratio )
  21. {
  22. UINT nAll = 0, nChCount = 0;
  23. int len = 0;
  24. while( *sz && len < maxlen )
  25. {
  26. if( IsDBCSLeadByte(BYTE(*sz)) )
  27. nChCount ++;
  28. nAll ++;
  29. sz ++;
  30. len ++;
  31. }
  32. if( ratio && nAll && nChCount )
  33. {
  34. return ((float)nChCount/(float)nAll) < ratio;
  35. }
  36. return 0 == nChCount;
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////////
  39. // global data
  40. CStProfile &AfxGetProfile()
  41. {
  42. static CStProfile g_stprofile;
  43. return g_stprofile;
  44. }
  45. CStockContainer &AfxGetStockContainer( )
  46. {
  47. static CStockContainer g_stockcontainer;
  48. return g_stockcontainer;
  49. }
  50. CStockContainer &AfxGetSListStockContainer( )
  51. {
  52. static CStockContainer g_sliststockcontainer;
  53. return g_sliststockcontainer;
  54. }
  55. CDomainContainer &AfxGetDomainContainer( )
  56. {
  57. static CDomainContainer g_domaincontainer;
  58. return g_domaincontainer;
  59. }
  60. CDomainContainer &AfxGetGroupContainer( )
  61. {
  62. static CDomainContainer g_groupcontainer;
  63. if( g_groupcontainer.GetSize() == 0 )
  64. {
  65. // Add Default
  66. CDomain domain;
  67. domain.m_strName = domain_self;
  68. g_groupcontainer.AddDomain( domain );
  69. }
  70. return g_groupcontainer;
  71. }
  72. static CStDatabase * g_pdb = NULL;
  73. void AfxSetDB(CStDatabase * pdb)
  74. {
  75. g_pdb = pdb;
  76. }
  77. CStDatabase & AfxGetDB()
  78. {
  79. if( g_pdb ) return *g_pdb;
  80. static CStDatabase g_stdatabase;
  81. return g_stdatabase;
  82. }
  83. CStock & AfxGetStockMain( DWORD dwMarket )
  84. {
  85. static CStock g_stockMain;
  86. if( !g_stockMain.GetStockInfo().IsValidStock() )
  87. g_stockMain.SetStockCode( CStock::marketSHSE, STKLIB_CODE_MAIN );
  88. if( CStock::marketSHSE == dwMarket )
  89. return g_stockMain;
  90. else if( CStock::marketSZSE == dwMarket )
  91. {
  92. static CStock g_stockMain2;
  93. if( !g_stockMain2.GetStockInfo().IsValidStock() )
  94. g_stockMain2.SetStockCode( CStock::marketSZSE, STKLIB_CODE_MAINSZN );
  95. return g_stockMain2;
  96. }
  97. else if( CStock::marketCYSE == dwMarket )
  98. return g_stockMain;
  99. else if( CStock::marketCHNA == dwMarket )
  100. return g_stockMain;
  101. else if( CStock::marketHKEX == dwMarket )
  102. return g_stockMain;
  103. else if( CStock::marketTBSE == dwMarket )
  104. return g_stockMain;
  105. else if( CStock::marketTKSE == dwMarket )
  106. return g_stockMain;
  107. else if( CStock::marketLSE == dwMarket )
  108. return g_stockMain;
  109. else if( CStock::marketFLKFSE == dwMarket )
  110. return g_stockMain;
  111. else if( CStock::marketNYSE == dwMarket )
  112. return g_stockMain;
  113. else if( CStock::marketNASDAQ == dwMarket )
  114. return g_stockMain;
  115. else
  116. return g_stockMain;
  117. }
  118. //////////////////////////////////////////////////////////////////////////////////
  119. // database functions
  120. BOOL AfxInitializeDB( LPSTR lpErr, UINT nMaxSize )
  121. {
  122. if( lpErr && nMaxSize > 0 )
  123. memset( lpErr, 0, nMaxSize );
  124. if( !CStDatabase::CreateSelfDB( AfxGetProfile().GetSelfDBPath() ) )
  125. {
  126. if( lpErr ) strncpy( lpErr, db_errcreateselfdb, min(nMaxSize-1,strlen(db_errcreateselfdb)) );
  127. return FALSE;
  128. }
  129. if( !AfxGetDB().SetRootPath( AfxGetProfile().GetSelfDBPath(), IStStore::dbtypeSelfDB ) )
  130. {
  131. if( lpErr ) strncpy( lpErr, db_errrootpath, min(nMaxSize-1,strlen(db_errrootpath)) );
  132. return FALSE;
  133. }
  134. CSPString strQianlongPath = AfxGetProfile().GetQianlongPath();
  135. if( strQianlongPath.GetLength() > 0
  136. && !AfxGetDB().AddAssistantRootPath( strQianlongPath, IStStore::dbtypeQianlong ) )
  137. {
  138. if( lpErr ) strncpy( lpErr, db_errqianlongpath, min(nMaxSize-1,strlen(db_errqianlongpath)) );
  139. return FALSE;
  140. }
  141. return TRUE;
  142. }
  143. BOOL AfxReleaseDB( )
  144. {
  145. AfxGetDB().SetRootPath( NULL );
  146. AfxGetDB().RemoveAllAssistant( );
  147. return TRUE;
  148. }
  149. BOOL AfxReloadStockMain( )
  150. {
  151. static CSPMutex g_mutexStockMain;
  152. CSPMutex::Scoped l(g_mutexStockMain);
  153. AfxReloadStock( AfxGetStockMain(CStock::marketSHSE) );
  154. AfxReloadStock( AfxGetStockMain(CStock::marketSZSE) );
  155. return TRUE;
  156. }
  157. BOOL AfxReloadStock( CStock & stock )
  158. {
  159. CStockInfo info = stock.GetStockInfo();
  160. // Reload AfxGetStockMain()
  161. AfxGetStockContainer().GetStockInfo( info.GetStockCode(), &info );
  162. stock.Clear( );
  163. stock.SetStockInfo( &info );
  164. stock.SetDatabase( &AfxGetDB() );
  165. stock.PrepareData( CStock::dataK, CKData::ktypeDay );
  166. // Merge New
  167. stock.GetKDataDay().MergeKData( &(info.m_kdata) );
  168. stock.PrepareData( CStock::dataK, CKData::ktypeWeek );
  169. stock.PrepareData( CStock::dataK, CKData::ktypeMonth );
  170. stock.PrepareData( CStock::dataK, CKData::ktypeMin5 );
  171. stock.PrepareData( CStock::dataK, CKData::ktypeMin15 );
  172. stock.PrepareData( CStock::dataK, CKData::ktypeMin30 );
  173. stock.PrepareData( CStock::dataK, CKData::ktypeMin60 );
  174. return TRUE;
  175. }
  176. BOOL AfxPrepareStockData( CStDatabase * pDatabase, CStock &stock, int nKType, int nKFormat, int nMaindataType, BOOL bFullFill, BOOL bReload )
  177. {
  178. CStockInfo info;
  179. if( !AfxGetStockContainer().GetStockInfo( stock.GetStockCode(), &info ) )
  180. info = stock.GetStockInfo( );
  181. stock.SetDatabase( pDatabase );
  182. stock.PrepareData( CStock::dataK, CKData::ktypeDay, bReload );
  183. stock.PrepareData( CStock::dataK, CKData::ktypeMin5, bReload );
  184. stock.PrepareData( CStock::dataDR, CKData::ktypeDay, bReload );
  185. if( bReload || stock.GetKDataDay().GetCurFormat() == CKData::formatOriginal )
  186. stock.GetKDataDay().MergeKData( &(info.m_kdata) );
  187. CKData & kday = stock.GetKData(CKData::ktypeDay);
  188. CKData & kdata = stock.GetKData( nKType );
  189. kdata.SetDRData( stock.GetDRData() );
  190. kday.SetDRData( stock.GetDRData() );
  191. if( bFullFill )
  192. kday.FullFillKData( AfxGetStockMain().GetKDataDay(), FALSE );
  193. kday.ChangeCurFormat( nKFormat, AfxGetProfile().GetAutoResumeDRBegin(), AfxGetProfile().GetAutoResumeDRLimit() );
  194. if( CKData::ktypeWeek == nKType || CKData::ktypeMonth == nKType )
  195. {
  196. // 周线和月线不能ChangeCurFormat(...)
  197. if( CKData::formatOriginal == nKFormat )
  198. stock.PrepareData( CStock::dataK, nKType, bReload );
  199. else
  200. stock.ExtractKData( nKType, TRUE );
  201. }
  202. else if( CKData::ktypeDay != nKType )
  203. {
  204. stock.PrepareData( CStock::dataK, nKType, bReload );
  205. kdata.ChangeCurFormat( nKFormat, AfxGetProfile().GetAutoResumeDRBegin(), AfxGetProfile().GetAutoResumeDRLimit() );
  206. }
  207. kdata.SetMaindataType( nMaindataType );
  208. return TRUE;
  209. }
  210. int AfxRefreshStockContainerMinute( CStockContainer & container, LONG lStockType, int nSecondsToRead )
  211. {
  212. container.Lock();
  213. time_t tmTradeLatest = -1;
  214. int nCount = 0;
  215. for( int i=0; i<container.GetSize(); i++ )
  216. {
  217. CStockInfo & info = container.ElementAt(i);
  218. if( info.GetType() != lStockType )
  219. continue;
  220. CStock stock;
  221. stock.SetStockInfo( &info );
  222. stock.SetDatabase( &AfxGetDB() );
  223. AfxGetDB().LoadMinute( &stock );
  224. info.m_minute.RemoveAll();
  225. CMinute & minstock = stock.GetMinute();
  226. if( -1 == tmTradeLatest && stock.GetMinute().GetSize() > 0 )
  227. tmTradeLatest = CSPTime::GetTimeTradeLatest( minstock[0].m_time );
  228. for( int i=0; i<minstock.GetSize(); i++ )
  229. {
  230. if( -1 == nSecondsToRead || tmTradeLatest-minstock[i].m_time<nSecondsToRead )
  231. info.m_minute.Add( minstock[i] );
  232. }
  233. nCount ++;
  234. }
  235. container.UnLock();
  236. return nCount;
  237. }
  238. //////////////////////////////////////////////////////////////////////////////////
  239. // usful functions
  240. CSPString AfxGetTimeString( time_t time, LPCTSTR lpszTimeFmt, BOOL bAddWeekDay )
  241. {
  242. SP_ASSERT( lpszTimeFmt );
  243. if( !lpszTimeFmt || strlen(lpszTimeFmt) == 0 )
  244. return "";
  245. CSPTime sptime(time);
  246. CSPString strTime = sptime.Format( lpszTimeFmt );
  247. if( bAddWeekDay )
  248. {
  249. int nDayOfWeek = sptime.GetDayOfWeek();
  250. switch( nDayOfWeek )
  251. {
  252. case 1: strTime += sz_sunday; break;
  253. case 2: strTime += sz_monday; break;
  254. case 3: strTime += sz_tuesday; break;
  255. case 4: strTime += sz_wednesday; break;
  256. case 5: strTime += sz_thursday; break;
  257. case 6: strTime += sz_friday; break;
  258. case 7: strTime += sz_saturday; break;
  259. default:;
  260. }
  261. }
  262. return strTime;
  263. }
  264. CSPString AfxGetReportTypeString( int reporttype )
  265. {
  266. switch( reporttype )
  267. {
  268. case CStock::reportQuarter:
  269. return sz_reportQuarter;
  270. case CStock::reportMid:
  271. return sz_reportMid;
  272. case CStock::reportQuarter3:
  273. return sz_reportQuarter3;
  274. case CStock::reportAnnals:
  275. return sz_reportAnnals;
  276. default:
  277. return "";
  278. }
  279. }
  280. CSPString AfxGetFileDirectory( CSPString strPathName, CSPString strDefault )
  281. {
  282. if( strPathName.IsEmpty() )
  283. return strDefault;
  284. CSPString strResult = strPathName;
  285. if( strResult.GetLength() > 0 )
  286. {
  287. int nIndex = strResult.ReverseFind( '\' );
  288. if( -1 == nIndex )
  289. nIndex = strResult.ReverseFind( '/' );
  290. if( -1 == nIndex )
  291. strResult = strDefault;
  292. else
  293. strResult = strResult.Left( nIndex );
  294. }
  295. if( strResult.GetLength() <= 0 )
  296. return strDefault;
  297. int nLen = strResult.GetLength();
  298. if( nLen > 0 && strResult[nLen-1] != '\' && strResult[nLen-1] != '/' )
  299. strResult += STRING_DIRSEP;
  300. return strResult;
  301. }
  302. CSPString AfxGetFilePath( CSPString strDir, CSPString strFileTitle )
  303. {
  304. if( strDir.GetLength() > 0 && ( strDir[strDir.GetLength()-1] == '\' || strDir[strDir.GetLength()-1] == '/' ) )
  305. strDir = strDir.Left( strDir.GetLength()-1 );
  306. strDir += STRING_DIRSEP;
  307. return ( strDir + strFileTitle );
  308. }