BlockSource.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // BlockSource.cpp: implementation of the BlockSource class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "BlockSource.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. namespace PackageCQ
  10. {
  11. BlockSource::~BlockSource()
  12. {
  13. }
  14. int BlockSource::GetID() const
  15. {
  16. return m_BlockID;
  17. }
  18. int BlockSource::GetLength() const
  19. {
  20. return m_BlockLength;
  21. }
  22. iostream& BlockSource::GetBlockStream() 
  23. {
  24. return m_BlockPoint.GetStreamPointToData (); 
  25. }
  26. bool BlockSource::operator ==(const BlockSource &Source) const
  27. {
  28. if (m_BlockID == Source.m_BlockID)
  29. {
  30. return true;
  31. }
  32. else
  33. {
  34. return false;
  35. }
  36. }
  37. bool BlockSource::operator <(const BlockSource &Source) const
  38. {
  39. if (m_BlockID < Source.m_BlockID)
  40. {
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. bool BlockSource::operator >(const BlockSource &Source) const
  49. {
  50. if (m_BlockID < Source.m_BlockID)
  51. {
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. const BlockSource& BlockSource::operator <<(iostream &DataStream)
  60. {
  61. if(!DataStream.eof())
  62. {
  63. DataStream.read((char *) &m_BlockID,sizeof(m_BlockID));
  64. }
  65. else
  66. {
  67. throw exception("File is End when Get BlockSource");
  68. }
  69. if(DataStream.bad() )
  70. {
  71. throw exception("read error when Get BlockSource");
  72. }
  73. unsigned  DataOffset = 0;
  74. if(!DataStream.eof())
  75. {
  76. DataStream.read((char *) &DataOffset,sizeof(DataOffset));
  77. }
  78. else
  79. {
  80. throw exception("File is End when Get BlockSource");
  81. }
  82. if(DataStream.bad() )
  83. {
  84. throw exception("read error when Get BlockSource");
  85. }
  86. if(!DataStream.eof())
  87. {
  88. DataStream.read((char *) &m_UnCompressBlockLength,sizeof(m_UnCompressBlockLength));
  89. }
  90. else
  91. {
  92. throw exception("File is End when Get BlockSource");
  93. }
  94. if(DataStream.bad() )
  95. {
  96. throw exception("read error when Get BlockSource");
  97. }
  98. if(!DataStream.eof())
  99. {
  100. DataStream.read((char *) &m_BlockLength,sizeof(m_BlockLength ) - 1);
  101. }
  102. else
  103. {
  104. throw exception("File is End when Get BlockSource");
  105. }
  106. if(DataStream.bad() )
  107. {
  108. throw exception("read error when Get BlockSource");
  109. }
  110. if(!DataStream.eof())
  111. {
  112. DataStream.read((char*)&m_CompressMethod,sizeof(m_CompressMethod));
  113. }
  114. else
  115. {
  116. throw exception("File is End when Get BlockSource");
  117. }
  118. if(DataStream.bad() )
  119. {
  120. throw exception("read error when Get BlockSource");
  121. }
  122. m_BlockPoint  = DataPointFromStream(DataStream,DataOffset);
  123. /*int Pos = DataStream.tellg();
  124.   m_BlockLength = ComputeBlockSize(m_UnCompressBlockLength,m_BlockPoint.GetStreamPointToData() );
  125.   
  126. DataStream.seekg(Pos,ios::beg); 
  127. */
  128. return * this;
  129. }
  130. BlockSource::BlockSource()
  131. :m_BlockPoint()
  132. ,m_BlockID(0)
  133. ,m_BlockLength(0)
  134. {
  135. }
  136. const BlockSource& BlockSource::operator =(BlockSource &Source)
  137. {
  138. if(&Source != this)
  139. {
  140. m_BlockID = Source.GetID();
  141. m_BlockLength = Source.GetLength();
  142. m_UnCompressBlockLength = Source.m_UnCompressBlockLength;
  143. m_CompressMethod        = Source.m_CompressMethod; 
  144. m_BlockPoint            = Source.m_BlockPoint; 
  145. }
  146. return * this;
  147. }
  148. const BlockSource& BlockSource::operator >>(iostream &DataStream)
  149. {
  150. DataStream.write((char *)&m_BlockID,sizeof(m_BlockID));
  151. unsigned Offset = m_BlockPoint.GetPointPosition();
  152. DataStream.write((char *)&(Offset),sizeof(Offset));
  153. DataStream.write((char *)&m_UnCompressBlockLength,sizeof(m_UnCompressBlockLength));
  154. DataStream.write((char *)&m_BlockLength,sizeof(m_BlockLength )- 1);
  155. DataStream.write((char *)&m_CompressMethod,sizeof(m_CompressMethod));
  156. if(DataStream.bad() )
  157. {
  158. throw exception("write BlockSource Error");
  159. }
  160. return * this;
  161. }
  162. BlockSource::BlockSource(DataPointFromStream &Point)
  163. :m_BlockLength(0)
  164. ,m_BlockID(0)
  165. ,m_BlockPoint(Point)
  166. ,m_UnCompressBlockLength(0)
  167. {
  168. }
  169. BlockSource::BlockSource(const BlockSource &Source)
  170. {
  171. m_BlockPoint     = Source.m_BlockPoint ;
  172. m_BlockID        = Source.m_BlockID ;
  173. m_BlockLength    = Source.m_BlockLength ;
  174. m_CompressMethod = Source.m_CompressMethod;
  175. m_UnCompressBlockLength = Source.m_UnCompressBlockLength ;
  176. }
  177. void BlockSource::CloneBlockSourceToMe(BlockSource& Source)
  178. {
  179. iostream& WrirtePoint = m_BlockPoint.GetStreamPointToData();
  180. m_BlockLength           = Source.m_BlockLength;
  181. m_BlockID               = Source.m_BlockID ;
  182. m_UnCompressBlockLength = Source.m_UnCompressBlockLength;
  183. m_CompressMethod        = Source.m_CompressMethod;
  184. iostream& SourcePoint = Source.GetBlockStream();
  185. char * Data = new char [m_BlockLength];
  186. auto_ptr<char> DataBlock(Data);
  187. SourcePoint.read(Data,m_BlockLength) ; 
  188.         if(SourcePoint.bad() )
  189. {
  190. throw exception ("read file error");
  191. }
  192. if(SourcePoint.eof() )
  193. {
  194. throw exception("File is End when Read File BlockData");
  195. }
  196. WrirtePoint.write(Data,m_BlockLength) ;
  197. if(WrirtePoint.bad() )
  198. {
  199. throw exception ("write file error");
  200. }
  201. /*istream_iterator<char> StartPoint (SourcePoint);
  202. istream_iterator<char> EndPoint =   StartPoint;
  203.         
  204.    
  205. for( int i = 0 ; i < Source.m_BlockLength ; ++i )
  206. {
  207. ++EndPoint;
  208. }
  209. copy(StartPoint, EndPoint, ostream_iterator<char>(WrirtePoint));
  210. */
  211. }
  212. BlockSource::BlockSource(int ID, int DataLength,int UnCompressLength,char CompressMethod, DataPointFromStream &Point)
  213.         :m_BlockID(ID)
  214. ,m_UnCompressBlockLength(UnCompressLength)
  215. ,m_BlockPoint(Point)
  216. ,m_BlockLength(DataLength)
  217. ,m_CompressMethod(CompressMethod)
  218. {
  219. }
  220. unsigned char BlockSource::GetCompressMethod()
  221. {
  222. return m_CompressMethod;
  223. }
  224. }