Advertisement
NickG

nbt.hpp

Apr 12th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #ifndef __NBTHPP__
  2. #define __NBTHPP__
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <cstdint>
  7.  
  8. enum NBT_Types {
  9.     TAG_End_id,
  10.     TAG_Byte_id,
  11.     TAG_Short_id,
  12.     TAG_Int_id,
  13.     TAG_Long_id,
  14.     TAG_Float_id,
  15.     TAG_Double_id,
  16.     TAG_Byte_Array_id,
  17.     TAG_String_id,
  18.     TAG_List_id,
  19.     TAG_Compound_id,
  20.     TAG_Int_Array_id
  21. };
  22.  
  23. class TAG {
  24.     public:
  25.         virtual ~TAG(){};
  26.         virtual unsigned char* encode(unsigned char*) =0;
  27.         virtual unsigned char* decode(unsigned char*) =0;
  28.         virtual unsigned int tag_size() =0;
  29.         virtual std::string pretty_print(int) =0;
  30.         virtual int get_id() =0;
  31.  
  32.         std::string get_name();
  33.         void set_name(std::string);
  34.  
  35.     protected:
  36.         std::string name;
  37. };
  38.  
  39. template<typename T, int id>
  40. class _TAG_Numeric: public TAG {
  41.     public:
  42.         _TAG_Numeric(){};
  43.         _TAG_Numeric(T);
  44.         virtual ~_TAG_Numeric(){};
  45.         unsigned int tag_size();
  46.         std::string pretty_print(int);
  47.         int get_id();
  48.  
  49.         T get_val();
  50.         void set_val(T);
  51.  
  52.     protected:
  53.         T val;
  54.  
  55. };
  56.  
  57. template<typename T, typename Shift_T, int id>
  58. class _TAG_Integral: public _TAG_Numeric<T, id> {
  59.     public:
  60.         _TAG_Integral(): _TAG_Numeric<T, id>(){};
  61.         _TAG_Integral(T new_val): _TAG_Numeric<T, id>(new_val){};
  62.         virtual ~_TAG_Integral(){};
  63.         unsigned char* encode(unsigned char*);
  64.         unsigned char* decode(unsigned char*);
  65.  
  66.     protected:
  67.         using _TAG_Numeric<T, id>::val;
  68. };
  69.  
  70. typedef _TAG_Integral<int8_t, uint8_t, TAG_Byte_id> TAG_Byte;
  71. typedef _TAG_Integral<int16_t, uint16_t, TAG_Short_id> TAG_Short;
  72. typedef _TAG_Integral<int32_t, uint32_t, TAG_Int_id> TAG_Int;
  73. typedef _TAG_Integral<int64_t, uint64_t, TAG_Long_id> TAG_Long;
  74.  
  75. template<typename T, typename Shift_T, int id>
  76. class _TAG_Precision: public _TAG_Numeric<T, id> {
  77.     public:
  78.         _TAG_Precision(): _TAG_Numeric<T, id>(){};
  79.         _TAG_Precision(T new_val): _TAG_Numeric<T, id>(new_val){};
  80.         virtual ~_TAG_Precision(){};
  81.         unsigned char* encode(unsigned char*);
  82.         unsigned char* decode(unsigned char*);
  83.  
  84.     protected:
  85.         using _TAG_Numeric<T, id>::val;
  86. };
  87.  
  88. typedef _TAG_Precision<float, uint32_t, TAG_Float_id> TAG_Float;
  89. typedef _TAG_Precision<double, uint64_t, TAG_Double_id> TAG_Double;
  90.  
  91. template<typename T, class TAG_T, int id>
  92. class _TAG_Array: public TAG {
  93.     public:
  94.         virtual ~_TAG_Array(){};
  95.         unsigned char* encode(unsigned char*);
  96.         unsigned char* decode(unsigned char*);
  97.         unsigned int tag_size();
  98.         std::string pretty_print(int);
  99.         int get_id();
  100.  
  101.         T& operator[](unsigned int);
  102.         unsigned int array_size();
  103.         void resize(unsigned int);
  104.  
  105.     protected:
  106.         std::vector<T> vtr;
  107. };
  108.  
  109. typedef _TAG_Array<int8_t, TAG_Byte, TAG_Byte_Array_id> TAG_Byte_Array;
  110. typedef _TAG_Array<int32_t, TAG_Int,  TAG_Int_Array_id> TAG_Int_Array;
  111.  
  112. class TAG_String: public TAG {
  113.     public:
  114.         TAG_String(){};
  115.         TAG_String(std::string);
  116.         unsigned char* encode(unsigned char*);
  117.         unsigned char* decode(unsigned char*);
  118.         unsigned int tag_size();
  119.         std::string pretty_print(int);
  120.         int get_id();
  121.  
  122.         std::string get_val();
  123.         void set_val(std::string);
  124.  
  125.     protected:
  126.         std::string str;
  127. };
  128.  
  129. class TAG_List: public TAG {
  130.     public:
  131.         TAG_List(){};
  132.         TAG_List(int, int);
  133.         ~TAG_List();
  134.         unsigned char* encode(unsigned char*);
  135.         unsigned char* decode(unsigned char*);
  136.         unsigned int tag_size();
  137.         std::string pretty_print(int);
  138.         int get_id();
  139.  
  140.         TAG* operator[](unsigned int);
  141.         unsigned int array_size();
  142.         void resize(unsigned int);
  143.         int get_type();
  144.         void set_type(int);
  145.  
  146.     protected:
  147.         std::vector<TAG*> vtr;
  148.         int tag_type;
  149.  
  150. };
  151.  
  152. class TAG_Compound: public TAG {
  153.     public:
  154.         unsigned char* encode(unsigned char*);
  155.         unsigned char* decode(unsigned char*);
  156.         unsigned int tag_size();
  157.         std::string pretty_print(int);
  158.         int get_id();
  159.  
  160.         TAG* operator[](unsigned int);
  161.         TAG* operator[](std::string);
  162.         unsigned int array_size();
  163.         void resize(unsigned int);
  164.  
  165.     protected:
  166.         std::vector<TAG*> vtr;
  167. };
  168.  
  169. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement