SV_DATA.C
上传用户:bjghjy
上传日期:2007-01-07
资源大小:379k
文件大小:14k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <io.h>
  7. #include "hq.h"
  8. #include "data.h"
  9. #include "sv_dbf.h"
  10. #include "lzw.h"
  11. extern BOOL run_cancelled;
  12. extern HWND ghWndMain;
  13. extern int date_num, tim;
  14. HFILE hfHq[2], hfMmp[2], hfDp, hfMaxMin[2];
  15. extern void MsgLocal(LPSTR msg);
  16. extern BOOL ErrMsg(HWND, LPSTR);
  17. extern int UDPBlockingHook(void);
  18. extern long oldCjss[2];
  19. extern BOOL IsZsRec(int, int);
  20. //extern int RefreshHqData(int jys);
  21. extern char szDataPath[128];
  22. long zs_times[2][20];
  23. BOOL fWrited[2] ={FALSE, FALSE};
  24. int WriteHqAll(int jys)
  25. {
  26. if(WriteHqData(jys) <0) return -1;
  27. if(WriteMmpData(jys) <0) return -1;
  28. if(WriteMaxMinData(jys) <0) return -1;
  29. return 0;
  30. }
  31. int WriteHqData(int jys)
  32. {
  33. _llseek(hfHq[jys], 0, SEEK_SET);
  34. _lwrite(hfHq[jys], (short *)&HqData[jys].recCount, sizeof(short));
  35. if(_hwrite(hfHq[jys], HqData[jys].lpPreData,
  36. (long)HqData[jys].recCount*sizeof(HQ_PRE_DATA))
  37. !=(long)HqData[jys].recCount*sizeof(HQ_PRE_DATA))
  38. {
  39. ErrMsg(ghWndMain, "Error write hqdata");
  40. return -1;
  41. }
  42. if(_hwrite(hfHq[jys], HqData[jys].lpRefData,
  43. (long)HqData[jys].recCount*sizeof(HQ_REF_DATA))
  44. !=(long)HqData[jys].recCount*sizeof(HQ_REF_DATA))
  45. {
  46. ErrMsg(ghWndMain, "Error write hqdata");
  47. return -1;
  48. }
  49. CreateLzwFile(jys);
  50. return 0;
  51. }
  52. //////////////////////////////////////////////////////////////////////////
  53. //Function:to reduce transfer time between server and client,we must build
  54. //         compress data.this prog compress main mass data(szhq.dat.shhq.dat)
  55. //Para    :None
  56. //Return  :if successful return 0,otherwise return -1
  57. //Writer  :lym
  58. //Create  :1997/08/24
  59. //////////////////////////////////////////////////////////////////////////
  60. int CreateLzwFile(int jys)
  61. {
  62. char temp[256]; //,tmp[256];
  63.     OFSTRUCT os;                 
  64.     HQ_REF_DATA_V202  DataV202;
  65.     long i;
  66.     int lzw;
  67.     
  68.     //for(jys=0;jys<2;jys++)
  69.     //{   
  70. if(HqData[jys].recCount==0)
  71. return(-1);
  72. if(jys==0)
  73. sprintf(temp,"%s\hqdata\szhq.202",szDataPath);
  74. else
  75. sprintf(temp,"%s\hqdata\shhq.202",szDataPath);
  76. if((lzw=OpenFile(temp,&os,OF_WRITE|OF_SHARE_DENY_NONE))==HFILE_ERROR)
  77. if((lzw=OpenFile(temp,&os,OF_WRITE|OF_SHARE_DENY_NONE|OF_CREATE))==HFILE_ERROR)
  78. return(-1);
  79. _lwrite(lzw, (short *)&HqData[jys].recCount, sizeof(short));
  80. for(i=0;i<HqData[jys].recCount;i++)
  81. {
  82. while(UDPBlockingHook());
  83. if(run_cancelled) break;
  84. memset((char*)&DataV202,0,sizeof(HQ_REF_DATA_V202));
  85.  
  86.   strcpy(DataV202.zqdm,HqData[jys].lpPreData[i].zqdm);
  87.   strcpy(DataV202.zqmc,HqData[jys].lpPreData[i].zqmc);
  88.         
  89.          sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].zdjg*100.00);
  90.   DataV202.zdjg =atol(temp);
  91.  
  92.   sprintf(temp,"%7.2f",HqData[jys].lpPreData[i].zrsp*100.00);
  93.   DataV202.zrsp =atol(temp)-DataV202.zdjg;
  94.  
  95.   sprintf(temp,"%7.2f",HqData[jys].lpPreData[i].jrkp*100.00);
  96.   DataV202.jrkp =atol(temp)-DataV202.zdjg;
  97.  
  98.   sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].zgjg*100.00);
  99.   DataV202.zgjg =atol(temp)-DataV202.zdjg;
  100.  
  101.   sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].zdjm*100.00);
  102.   DataV202.zdjm =atol(temp)-DataV202.zdjg;
  103.  
  104.   sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].zgjm*100.00);
  105.   DataV202.zgjm =atol(temp)-DataV202.zdjg;
  106.  
  107.   sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].zjjg*100.00);
  108.   DataV202.zjjg =atol(temp)-DataV202.zdjg;
  109.  
  110.   sprintf(temp,"%7.2f",HqData[jys].lpRefData[i].wb*100.00);
  111.   DataV202.wb =atol(temp);
  112.  
  113.   DataV202.cjss =HqData[jys].lpRefData[i].cjss;
  114.   DataV202.npzl =HqData[jys].lpRefData[i].npzl;
  115.   DataV202.cjje =HqData[jys].lpRefData[i].cjje;
  116. _lwrite(lzw,(char *)&DataV202,sizeof(HQ_REF_DATA_V202));
  117. }
  118. _lclose(lzw);
  119. return TRUE;
  120. }
  121. int WriteDpData(int jys)
  122. {
  123. _llseek(hfDp, jys*sizeof(DpData[jys]), SEEK_SET);
  124. _lwrite(hfDp, &DpData[jys], sizeof(DpData[jys]));
  125. return 0;
  126. }
  127. int WriteMmpData(int jys)
  128. {
  129. _llseek(hfMmp[jys], 0, SEEK_SET);
  130. //_lwrite(hfMmp[jys], (int *)&MmpData[jys].recCount, sizeof(int));
  131. if(_hwrite(hfMmp[jys], MmpData[jys].lpMmp, (long)sizeof(MMP)*HqData[jys].recCount)
  132. !=(long)sizeof(MMP)*HqData[jys].recCount)
  133. {
  134. ErrMsg(NULL, "Write mmp failed");
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. int WriteMaxMinData(int jys)
  140. {
  141. _llseek(hfMaxMin[jys], 0, SEEK_SET);
  142. _lwrite(hfMaxMin[jys], &MaxMinData[jys], sizeof(MaxMinData[jys]));
  143. return 0;
  144. }
  145. long max_l(long val1, long val2)
  146. {
  147. return (val1>val2)? val1:val2;
  148. }
  149. long min_l(long val1, long val2)
  150. {
  151. return (val1<val2)? val1:val2;
  152. }
  153. float max_f(float val1, float val2)
  154. {
  155. return (val1>val2)? val1:val2;
  156. }
  157. float min_f(float val1, float val2)
  158. {
  159. return (val1<val2)? val1:val2;
  160. }
  161. extern void MsgRecv(char far *msg);
  162. //////////////////////////////////////////////////
  163. //功能 :读取分时数据文件,初始化内存分时数据头部信息
  164. //参数 :
  165. //    jys---交易所编码
  166. //返回码 :
  167. //    0  ---成功
  168. //         -1 ---失败
  169. //修改日期 :1997/10/15
  170.   
  171. int CheckGraHead(int jys)
  172. {
  173. int i;
  174. char temp[128];
  175. HFILE hFile;
  176. OFSTRUCT os;
  177. wsprintf(temp, "检查分时数据头:%d", jys);
  178. MsgLocal(temp);
  179. for(i =0; i<HqData[jys].recCount; i++)
  180. {
  181. while(UDPBlockingHook());
  182. if(run_cancelled) break;
  183. wsprintf(temp, "%s\%s.dat",
  184. GraphData[jys].szGraPath,
  185. HqData[jys].lpPreData[i].zqdm);
  186. hFile =OpenFile(temp, &os, OF_SHARE_DENY_NONE|OF_READ);
  187. //打开文件失败,必需重新初始化内存分时数据头部信息
  188. if(hFile ==HFILE_ERROR)
  189. goto reset_values;
  190. else
  191. {
  192. //该内存分时数据头部信息读不出,必需重新初始化
  193. if(_lread(hFile, &GraphData[jys].lpGraHead[i],
  194. sizeof(GRA_HEAD)) !=sizeof(GRA_HEAD))
  195. goto reset_values;
  196. //该内存分时数据头部信息日期错,必需重新初始化
  197. if(GraphData[jys].lpGraHead[i].dateNum !=date_num)
  198. goto reset_values;
  199. _lclose(hFile);
  200. //至此说明该内存分时数据头部信息正确,但如存在成交量查,则必须调整
  201. //内存行情数据的内盘总量
  202. HqData[jys].lpRefData[i].npzl =GraphData[jys].lpGraHead[i].npzl
  203. +(HqData[jys].lpRefData[i].cjss-GraphData[jys].lpGraHead[i].npzl
  204. -GraphData[jys].lpGraHead[i].wpzl)/2;
  205. continue;
  206. }
  207. reset_values:
  208. HqData[jys].lpRefData[i].npzl =HqData[jys].lpRefData[i].cjss/2;
  209. memset(&GraphData[jys].lpGraHead[i], 0, sizeof(GRA_HEAD));
  210. GraphData[jys].lpGraHead[i].dateNum =date_num;
  211. GraphData[jys].lpGraHead[i].zrsp =HqData[jys].lpPreData[i].zrsp;
  212. GraphData[jys].lpGraHead[i].zgjg =GraphData[jys].lpGraHead[i].zdjg
  213. =HqData[jys].lpRefData[i].zjjg-HqData[jys].lpPreData[i].zrsp;
  214. if(hFile ==HFILE_ERROR)
  215. {
  216. hFile =OpenFile(temp, &os, OF_SHARE_DENY_NONE|OF_CREATE|OF_READWRITE);
  217. if(hFile ==HFILE_ERROR) continue;
  218. }
  219. _lclose(hFile);
  220. }
  221. return 0;
  222. }
  223. BOOL WriteGraData(int jys,int i)
  224. char tmp[50];
  225. HFILE hFile;
  226. OFSTRUCT os;
  227.  
  228. wsprintf(tmp, "%s\%s.dat", GraphData[jys].szGraPath,
  229. HqData[jys].lpPreData[i].zqdm);
  230. //GraphData[jys].lpGraHead[i].dateNum =date_num;
  231. //GraphData[jys].lpGraHead.zrsp =HqData[jys].lpRefData[i].zrsp;
  232. //GraphData[jys].lpGraData[i].tim =tim;
  233. hFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  234. if(hFile ==HFILE_ERROR)
  235. return FALSE;
  236. if(_llseek(hFile, sizeof(GRA_HEAD)+(GraphData[jys].lpGraHead[i].minCount-1)*sizeof(GRA_DATA),
  237. SEEK_SET) ==HFILE_ERROR)
  238. return FALSE;
  239. _lwrite(hFile, &GraphData[jys].lpGraData[i],sizeof(GRA_DATA));
  240. _llseek(hFile, 0, SEEK_SET);
  241. _lwrite(hFile, &GraphData[jys].lpGraHead[i],sizeof(GRA_HEAD));
  242. _lclose(hFile);
  243. return TRUE;
  244. }
  245. int WriteMinData(int jys)
  246. {
  247. return 0;
  248. }
  249. BOOL WriteDayData(int jys)
  250. {
  251. int i;
  252. long day_cur;
  253. char tmp[128];
  254. DAY_DATA dayData;
  255. HFILE hFile;
  256. OFSTRUCT os;
  257. time_t ltime;
  258. struct tm *ptm;
  259. //static BOOL fWrited[2] ={FALSE, FALSE};
  260. long len;
  261.     
  262. if(fWrited[jys])
  263. {
  264. MsgLocal("history data already writed!");
  265. return TRUE;
  266. }
  267. MsgLocal("Write day data...");
  268.     
  269. time(&ltime);
  270. ptm =localtime(&ltime);
  271. day_cur =(long)(1900+ptm->tm_year)*10000+(long)(ptm->tm_mon+1)*100+ptm->tm_mday;
  272. for (i=0;i<HqData[jys].recCount;i++) 
  273. {         
  274. while(UDPBlockingHook());
  275. if(run_cancelled) break;
  276. if(GraphData[jys].lpGraHead[i].minCount <=0) continue;
  277. wsprintf(tmp, "%s\%s\%s.day", szDataPath, (jys ==0)?"SZDAY":"SHDAY",
  278. HqData[jys].lpPreData[i].zqdm);                          
  279. hFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  280. if(hFile ==HFILE_ERROR)
  281. {
  282. hFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_CREATE|OF_READWRITE);
  283. if(hFile ==HFILE_ERROR)
  284. {
  285. ErrMsg(NULL, "Create file failed");
  286. ErrMsg(NULL, tmp);
  287. continue;
  288. }
  289. }
  290. else
  291. {
  292. _llseek(hFile, 0, SEEK_SET);
  293. len =_llseek(hFile, 0, SEEK_END);
  294. if(len!=0)
  295. {
  296. if(_llseek(hFile, len-sizeof(dayData), SEEK_SET) ==HFILE_ERROR)
  297. {
  298. _llseek(hFile, 0, SEEK_SET);
  299. }
  300. else if(_lread(hFile, &dayData, sizeof(dayData))
  301. !=sizeof(dayData))
  302. {
  303. ErrMsg(NULL, "Read file failed");
  304. ErrMsg(NULL, tmp);
  305. _lclose(hFile);
  306. continue;
  307. }
  308. else if(dayData.day ==day_cur)
  309. {
  310. _lclose(hFile);
  311. continue;
  312. }
  313. }
  314. }
  315. dayData.day =day_cur;
  316. dayData.kpjg =HqData[jys].lpPreData[i].jrkp;
  317. dayData.spjg =HqData[jys].lpRefData[i].zjjg;
  318. dayData.zgjg =HqData[jys].lpRefData[i].zgjg;
  319. dayData.zdjg =HqData[jys].lpRefData[i].zdjg;
  320. dayData.cjss =HqData[jys].lpRefData[i].cjss;
  321. dayData.cjje =HqData[jys].lpRefData[i].cjje;
  322. _lwrite(hFile,&dayData,sizeof(DAY_DATA));
  323. _lclose(hFile);
  324. }    
  325. MsgLocal("Write day data ok");
  326. fWrited[jys] =TRUE;
  327. return TRUE;
  328. }
  329. BOOL WriteWeekData(int jys)
  330. {
  331. int i,j;
  332. char tmp[100];
  333. HISTORY_DATA WeekData,DayData,NextDayData;
  334. HFILE hWeekFile,hDayFile;
  335. OFSTRUCT os;
  336. time_t c_time1,c_time2;
  337. static BOOL fWrited[2] ={FALSE, FALSE};
  338.     
  339. if(fWrited[jys])
  340. {
  341. MsgLocal("week data already writed");
  342. return TRUE;
  343. }
  344. MsgLocal("Write week data...");
  345. for (i=0;i<HqData[jys].recCount;i++) 
  346. {   
  347. while(UDPBlockingHook());
  348. if(run_cancelled) break;
  349. //打开日线文件
  350. memset(tmp,0,100);                                                              
  351. if(jys==0)
  352. wsprintf(tmp, "%s%s.day","szase\day\" ,
  353. HqData[jys].lpPreData[i].zqdm);                          
  354. else
  355. wsprintf(tmp, "%s%s.day","shase\day\" ,
  356. HqData[jys].lpPreData[i].zqdm);                          
  357. //MsgLocal(HqData[jys].lpPreData[i].zqdm);      
  358. hDayFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  359. if(hDayFile ==HFILE_ERROR)
  360. {
  361. _lclose(hDayFile);
  362. continue;               
  363. }
  364. _llseek(hDayFile,-1*(long)(sizeof(HISTORY_DATA)),SEEK_END);
  365. if(_lread(hDayFile,&DayData,sizeof(HISTORY_DATA))!=
  366. sizeof(HISTORY_DATA))
  367. {          
  368. _lclose(hDayFile);      
  369. continue;
  370. }        
  371. memcpy(&WeekData,&DayData,sizeof(HISTORY_DATA));        
  372. memcpy(&c_time1,DayData.date+6,sizeof(time_t));
  373. j=2;            
  374. while(TRUE)
  375. {   
  376. if(j>7) break;
  377.     _llseek(hDayFile,-j*(long)(sizeof(HISTORY_DATA)),SEEK_END);
  378. if(_lread(hDayFile,&NextDayData,sizeof(HISTORY_DATA))!=
  379. sizeof(HISTORY_DATA))                           
  380. break;                          
  381. memcpy(&c_time2,NextDayData.date+6,sizeof(time_t));      
  382. if((int)(difftime(c_time1,c_time2)/(24.00*60.00))>=7)
  383. break;
  384. //计算周线  
  385. WeekData.ks=NextDayData.ks;
  386. WeekData.cj+=NextDayData.cj;
  387. if(WeekData.zg<NextDayData.zg)  WeekData.zg=NextDayData.zg;
  388. if(WeekData.zd>NextDayData.zd)  WeekData.zg=NextDayData.zd;                     
  389. j++;    
  390. }               
  391. //写周线文件            
  392. if(jys==0)
  393. wsprintf(tmp, "%s%s.wek","szase\week\",
  394. HqData[jys].lpPreData[i].zqdm);
  395. else
  396. wsprintf(tmp, "%s%s.wek","shase\week\",
  397. HqData[jys].lpPreData[i].zqdm);
  398. hWeekFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  399. if(hWeekFile ==HFILE_ERROR)
  400. {
  401. hWeekFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_CREATE|OF_READWRITE);
  402. }  
  403. _llseek(hWeekFile,0L,SEEK_END);
  404. _lwrite(hWeekFile,&WeekData,sizeof(HISTORY_DATA));
  405. _lclose(hWeekFile);
  406. _lclose(hDayFile);
  407.     }
  408. MsgLocal("Write week data ok");
  409. fWrited[jys] =TRUE;
  410. return TRUE;
  411. }
  412. BOOL WriteMonthData(int jys)
  413. {
  414.     int i,j;
  415.     char tmp[100];
  416.     HISTORY_DATA WeekData,MonthData,NextWeekData;
  417. HFILE hWeekFile,hMonthFile;
  418. OFSTRUCT os;
  419. static BOOL fWrited[2] ={FALSE, FALSE};
  420. if(fWrited[jys])
  421. {
  422. MsgLocal("month data already writed!");
  423. return TRUE;
  424. }
  425. MsgLocal("Write month data...");
  426. for (i=0;i<HqData[jys].recCount;i++) 
  427. {   
  428. while(UDPBlockingHook());
  429. if(run_cancelled) break;
  430. //打开周线文件
  431.     if(jys==0)
  432. wsprintf(tmp, "%s%s.wek","szase\week\" ,
  433. HqData[jys].lpPreData[i].zqdm);                          
  434. else
  435. wsprintf(tmp, "%s%s.wek","shase\week\" ,
  436. HqData[jys].lpPreData[i].zqdm);                          
  437. //MsgLocal(HqData[jys].lpPreData[i].zqdm);      
  438. hWeekFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  439. if(hWeekFile ==HFILE_ERROR)
  440. {
  441. _lclose(hWeekFile);
  442. continue;               
  443. }
  444. _llseek(hWeekFile,-1*(long)(sizeof(HISTORY_DATA)),SEEK_END);
  445. if(_lread(hWeekFile,&WeekData,sizeof(HISTORY_DATA))!=
  446. sizeof(HISTORY_DATA))
  447. {          
  448. _lclose(hWeekFile);     
  449. continue;
  450. }        
  451. memcpy(&MonthData,&WeekData,sizeof(HISTORY_DATA));        
  452. j=2;            
  453. while(TRUE)
  454. {   
  455. if(j>31)        break;
  456.     _llseek(hWeekFile,-j*(long)(sizeof(HISTORY_DATA)),SEEK_END);
  457. if(_lread(hWeekFile,&NextWeekData,sizeof(HISTORY_DATA))!=
  458. sizeof(HISTORY_DATA))                           
  459. break;                  
  460. if(!strncmp(WeekData.date+2,NextWeekData.date+2,2))
  461. break;
  462. //计算月线  
  463. MonthData.ks=NextWeekData.ks;
  464. MonthData.cj+=NextWeekData.cj;
  465. if(MonthData.zg<NextWeekData.zg)  MonthData.zg=NextWeekData.zg;
  466. if(MonthData.zd>NextWeekData.zd)  MonthData.zg=NextWeekData.zd;                 
  467. j++;    
  468. }               
  469. //写月线文件            
  470. if(jys==0)
  471. wsprintf(tmp, "%s%s.mot","szase\month\",
  472. HqData[jys].lpPreData[i].zqdm);
  473. else
  474. wsprintf(tmp, "%s%s.mot","shase\month\",
  475. HqData[jys].lpPreData[i].zqdm);
  476. hMonthFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_READWRITE);
  477. if(hMonthFile ==HFILE_ERROR)
  478. {
  479. hMonthFile =OpenFile(tmp, &os, OF_SHARE_DENY_NONE|OF_CREATE|OF_READWRITE);
  480. }  
  481. _llseek(hMonthFile,0L,SEEK_END);
  482. _lwrite(hMonthFile,&MonthData,sizeof(HISTORY_DATA));
  483. _lclose(hMonthFile);
  484. _lclose(hWeekFile);
  485.     }
  486. MsgLocal("Write month data ok");
  487. fWrited[jys] =TRUE;
  488. return TRUE;
  489. }