memio.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Copyright (c) 2017-2026, Manticore Software LTD (https://manticoresearch.com)
  3. // Copyright (c) 2001-2016, Andrew Aksyonoff
  4. // Copyright (c) 2008-2016, Sphinx Technologies Inc
  5. // All rights reserved
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License. You should have
  9. // received a copy of the GPL license along with this program; if you
  10. // did not, you can find it at http://www.gnu.org/
  11. //
  12. #ifndef _memio_
  13. #define _memio_
  14. #include "sphinxstd.h"
  15. #include "fileio.h"
  16. class MemoryReader_c
  17. {
  18. public:
  19. MemoryReader_c ( const BYTE * pData, int iLen ) noexcept;
  20. explicit MemoryReader_c ( ByteBlob_t dData ) noexcept;
  21. int GetPos() const;
  22. void SetPos ( int iOff );
  23. uint64_t UnzipOffset();
  24. DWORD UnzipInt();
  25. CSphString GetString();
  26. SphOffset_t GetOffset();
  27. DWORD GetDword();
  28. void GetBytes ( void * pData, int iLen );
  29. const BYTE * Begin() const;
  30. int GetLength() const;
  31. bool HasData() const;
  32. template<typename T>
  33. T GetVal ();
  34. template<typename T>
  35. void GetVal ( T& tVal );
  36. protected:
  37. const BYTE * m_pData = nullptr;
  38. const int m_iLen = 0;
  39. const BYTE * m_pCur = nullptr;
  40. };
  41. template<typename T>
  42. T GetVal( MemoryReader_c& tReader );
  43. // first DWORD is len, then follows data
  44. template<typename VECTOR>
  45. void GetArray ( VECTOR& dBuf, MemoryReader_c& tIn );
  46. void GetArray ( CSphVector<CSphString>& dBuf, MemoryReader_c& tIn );
  47. class MemoryWriter_c : public Writer_i
  48. {
  49. public:
  50. MemoryWriter_c ( CSphVector<BYTE> & dBuf );
  51. int GetPos();
  52. void ZipOffset ( uint64_t uVal ) override;
  53. void ZipInt ( DWORD uVal ) override;
  54. void PutString ( const CSphString & sVal ) override;
  55. void PutString ( const char * szVal ) override;
  56. void PutZString ( const CSphString & sVal ) final;
  57. void PutZString ( const char * szVal ) final;
  58. void PutDword ( DWORD uVal ) override;
  59. void PutOffset ( SphOffset_t uValue ) override;
  60. void PutWord ( WORD uVal ) override;
  61. void PutBytes ( const void * pData, int64_t iLen ) override;
  62. void PutByte ( BYTE uVal ) override;
  63. void PutUint64 ( uint64_t uVal );
  64. template<typename T>
  65. void PutVal ( T tVal );
  66. protected:
  67. CSphVector<BYTE> & m_dBuf;
  68. };
  69. template<typename T>
  70. void PutVal ( MemoryWriter_c& tWriter, T tVal );
  71. // put DWORD size, then elems
  72. template<typename T>
  73. void SaveArray ( const VecTraits_T<T>& dBuf, MemoryWriter_c& tOut );
  74. void SaveArray ( const VecTraits_T<CSphString>& dBuf, MemoryWriter_c& tOut );
  75. // fixme: get rid of this
  76. class MemoryReader2_c : public MemoryReader_c
  77. {
  78. public:
  79. MemoryReader2_c ( const BYTE * pData, int iLen );
  80. uint64_t UnzipInt();
  81. uint64_t UnzipOffset();
  82. };
  83. // fixme: get rid of this
  84. class MemoryWriter2_c : public MemoryWriter_c
  85. {
  86. public:
  87. MemoryWriter2_c ( CSphVector<BYTE> & dBuf );
  88. void ZipOffset ( uint64_t uVal ) override;
  89. void ZipInt ( DWORD uVal ) override;
  90. };
  91. #include "memio_impl.h"
  92. #endif // _memio_