StringCompressor.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief \b Compresses/Decompresses ASCII strings and writes/reads them to BitStream class instances. You can use this to easily serialize and deserialize your own strings.
  12. ///
  13. #ifndef __STRING_COMPRESSOR_H
  14. #define __STRING_COMPRESSOR_H
  15. #include "Export.h"
  16. #include "DS_Map.h"
  17. #include "RakMemoryOverride.h"
  18. #include "NativeTypes.h"
  19. #ifdef _STD_STRING_COMPRESSOR
  20. #include <string>
  21. #endif
  22. /// Forward declaration
  23. namespace RakNet
  24. {
  25. class BitStream;
  26. class RakString;
  27. };
  28. namespace RakNet
  29. {
  30. /// Forward declarations
  31. class HuffmanEncodingTree;
  32. /// \brief Writes and reads strings to and from bitstreams.
  33. ///
  34. /// Only works with ASCII strings. The default compression is for English.
  35. /// You can call GenerateTreeFromStrings to compress and decompress other languages efficiently as well.
  36. class RAK_DLL_EXPORT StringCompressor
  37. {
  38. public:
  39. // Destructor
  40. ~StringCompressor();
  41. /// static function because only static functions can access static members
  42. /// The RakPeer constructor adds a reference to this class, so don't call this until an instance of RakPeer exists, or unless you call AddReference yourself.
  43. /// \return the unique instance of the StringCompressor
  44. static StringCompressor* Instance(void);
  45. /// Given an array of strings, such as a chat log, generate the optimal encoding tree for it.
  46. /// This function is optional and if it is not called a default tree will be used instead.
  47. /// \param[in] input An array of bytes which should point to text.
  48. /// \param[in] inputLength Length of \a input
  49. /// \param[in] languageID An identifier for the language / string table to generate the tree for. English is automatically created with ID 0 in the constructor.
  50. void GenerateTreeFromStrings( unsigned char *input, unsigned inputLength, uint8_t languageId );
  51. /// Writes input to output, compressed. Takes care of the null terminator for you.
  52. /// \param[in] input Pointer to an ASCII string
  53. /// \param[in] maxCharsToWrite The max number of bytes to write of \a input. Use 0 to mean no limit.
  54. /// \param[out] output The bitstream to write the compressed string to
  55. /// \param[in] languageID Which language to use
  56. void EncodeString( const char *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 );
  57. /// Writes input to output, uncompressed. Takes care of the null terminator for you.
  58. /// \param[out] output A block of bytes to receive the output
  59. /// \param[in] maxCharsToWrite Size, in bytes, of \a output . A NULL terminator will always be appended to the output string. If the maxCharsToWrite is not large enough, the string will be truncated.
  60. /// \param[in] input The bitstream containing the compressed string
  61. /// \param[in] languageID Which language to use
  62. bool DecodeString( char *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 );
  63. #ifdef _CSTRING_COMPRESSOR
  64. void EncodeString( const CString &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 );
  65. bool DecodeString( CString &output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 );
  66. #endif
  67. #ifdef _STD_STRING_COMPRESSOR
  68. void EncodeString( const std::string &input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 );
  69. bool DecodeString( std::string *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 );
  70. #endif
  71. void EncodeString( const RakNet::RakString *input, int maxCharsToWrite, RakNet::BitStream *output, uint8_t languageId=0 );
  72. bool DecodeString( RakNet::RakString *output, int maxCharsToWrite, RakNet::BitStream *input, uint8_t languageId=0 );
  73. /// Used so I can allocate and deallocate this singleton at runtime
  74. static void AddReference(void);
  75. /// Used so I can allocate and deallocate this singleton at runtime
  76. static void RemoveReference(void);
  77. StringCompressor();
  78. private:
  79. /// Singleton instance
  80. static StringCompressor *instance;
  81. /// Pointer to the huffman encoding trees.
  82. DataStructures::Map<int, HuffmanEncodingTree *> huffmanEncodingTrees;
  83. static int referenceCount;
  84. };
  85. } // namespace RakNet
  86. #endif
粤ICP备19079148号