Právě prohlížíš: mcadmin/packet_data_types.h
Zpět do složkyStáhnout
  1. #ifndef _PACKET_DATA_TYPES
  2. #define _PACKET_DATA_TYPES
  3. namespace mcp {
  4. class mc_int;
  5. class mc_short;
  6. class mc_string;
  7.  
  8. class mc_int {
  9. private:
  10. int val; //actual int
  11. public:
  12. int value() const;
  13. int value(int);
  14. mc_int();
  15. mc_int(int);
  16. void asBytes(char*);
  17. void fromBytes(char*) const;
  18. void fromBytes(char*,bool) const;
  19. void showBytes(); //Debug function, requires STD iostream
  20.  
  21. mc_int& operator=(int);
  22. mc_int& operator=(const mc_int&);
  23. mc_int& operator=(const mc_short&);
  24.  
  25. bool endianity; //true for little
  26. };
  27. class mc_short {
  28. private:
  29. short val;
  30. public:
  31. short value() const;
  32. short value(short);
  33. mc_short();
  34. mc_short(short);
  35. void asBytes(char*);
  36. void fromBytes(char*) const;
  37. void fromBytes(char*,bool) const;
  38.  
  39. mc_short& operator=(int);
  40. mc_short& operator=(const mc_int&);
  41. mc_short& operator=(const mc_short&);
  42.  
  43. bool endianity; //true for little endian
  44. };
  45. #ifndef _INC_STRING
  46. #include <string.h>
  47. #endif
  48. class mc_string {
  49. private:
  50. std::string val;
  51. public:
  52. std::string value() const;
  53. std::string value(std::string);
  54. int length() const; //size of output with length identifier
  55.  
  56. mc_string();
  57. mc_string(char*);
  58. mc_string(std::string);
  59.  
  60. mc_string& operator=(const char*);
  61. mc_string& operator=(const mc_string&);
  62. mc_string& operator=(const std::string&);
  63.  
  64. void showBytes();
  65.  
  66. void asBytes(char*);
  67.  
  68. bool endianity; //Endianity of length identifier
  69. };
  70.  
  71.  
  72. mc_int::mc_int() {
  73. val = 0;
  74. endianity = true;
  75. }
  76. mc_int::mc_int(int integer) {
  77. val = integer;
  78. endianity = true;
  79. }
  80. int mc_int::value() const {
  81. return val;
  82. }
  83. int mc_int::value(int integer) {
  84. val = integer;
  85. return val;
  86. }
  87. void mc_int::asBytes(char* data) {
  88. for (int i = 0; i < 4; i++)
  89. data[endianity?i:3-i] = (val >> (i * 8));
  90. }
  91. void mc_int::fromBytes(char* data) const {
  92. this->fromBytes(data, endianity);
  93. }
  94. void mc_int::fromBytes(char* data, bool endian) const {
  95. int result = 0;
  96. for (int i = 0; i < 4; i++)
  97. result = (result<<8)+data[endian?i:3-i];
  98. }
  99. void mc_int::showBytes() {
  100. #ifdef _IOSTREAM_
  101. char data[4];
  102. this->asBytes(data);
  103. std::cout<<'[';
  104. for(unsigned char i=0; i<4; i++) {
  105. std::cout<<(int)data[i];
  106. if(i<3)
  107. std::cout<<", ";
  108. else
  109. std::cout<<']';
  110.  
  111. }
  112.  
  113. #endif
  114. }
  115.  
  116. mc_int& mc_int::operator=(int integer) {
  117. val = integer;
  118. return *this;
  119. }
  120. mc_int& mc_int::operator=(const mc_int& other) {
  121. val = other.value();
  122. return *this;
  123. }
  124. mc_int& mc_int::operator=(const mc_short& other) {
  125. val = (int)other.value();
  126. return *this;
  127. }
  128.  
  129.  
  130. mc_short::mc_short() {
  131. val = 0;
  132. endianity = true;
  133. }
  134. mc_short::mc_short(short integer) {
  135. val = integer;
  136. endianity = true;
  137. }
  138. short mc_short::value() const {
  139. return val;
  140. }
  141. short mc_short::value(short integer) {
  142. val = integer;
  143. return val;
  144. }
  145. void mc_short::asBytes(char* data) {
  146. for (int i = 0; i < 2; i++)
  147. data[endianity?i:1-i] = (val >> (i * 8));
  148. }
  149. void mc_short::fromBytes(char* data) const {
  150. this->fromBytes(data, endianity);
  151. }
  152. void mc_short::fromBytes(char* data, bool endian) const {
  153. int result = 0;
  154. for (int i = 0; i < 2; i++)
  155. result = (result<<8)+data[endian?i:1-i];
  156. }
  157. mc_short& mc_short::operator=(int integer) {
  158. val = (short)integer;
  159. return *this;
  160. }
  161. mc_short& mc_short::operator=(const mc_int& other) {
  162. val = (short)other.value();
  163. return *this;
  164. }
  165. mc_short& mc_short::operator=(const mc_short& other) {
  166. val = other.value();
  167. return *this;
  168. }
  169.  
  170. mc_string::mc_string() {
  171. val = std::string("");
  172. }
  173. mc_string::mc_string(char* data) {
  174. val = std::string(data);
  175. }
  176. mc_string::mc_string(std::string data) {
  177. val = data;
  178. }
  179. std::string mc_string::value() const {
  180. return val;
  181. }
  182. std::string mc_string::value(std::string data) {
  183. val = data;
  184. return val;
  185. }
  186. void mc_string::asBytes(char* data) {
  187. mc_short size((short)val.length());
  188. size.endianity = endianity;
  189. size.asBytes(data);
  190. for(short i=0; i<size.value(); i++) {
  191. data[i+2] = val[i];
  192. }
  193. }
  194. int mc_string::length() const {
  195. return val.length()+2;
  196. }
  197. void mc_string::showBytes() {
  198. #ifdef _IOSTREAM_
  199. char data[2];
  200. mc_short size((short)val.length());
  201. size.endianity = endianity;
  202. size.asBytes(data);
  203. std::cout<<'[';
  204. std::cout<<(int)data[0]<<", "<<(int)data[1]<<", ";
  205. for(unsigned char i=0; i<val.length(); i++) {
  206. std::cout<<(char)val[i];
  207. if(i<val.length()-1)
  208. std::cout<<", ";
  209. else
  210. std::cout<<']';
  211.  
  212. }
  213.  
  214. #endif
  215. }
  216.  
  217.  
  218. }
  219.  
  220.  
  221.  
  222. #endif
Parsed using GeSHi 1.0.8.11 at 0.027