fork/slippc

git clone https://git.julia.blue/fork/slippc

Patrick CrainFixed GALINT computation error; added stats for damage done during each neutral interaction27331b7

0.5.0
5.4 KiB193 linesraw
1#ifndef UTIL_H_
2#define UTIL_H_
3
4#include <iomanip>
5#include <iostream>
6#include <sstream>
7#include <fstream>
8#include <codecvt>
9#include <algorithm> //std::find
10
11namespace slip {
12
13//Indent Level
14#define ILEV 1
15
16//Strings for various indentation amounts
17const std::string SPACE[10] = {
18  "",
19  " ",
20  "  ",
21  "   ",
22  "    ",
23  "     ",
24  "      ",
25  "       ",
26  "        ",
27  "         ",
28};
29
30static const std::string base64_chars =
31             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32             "abcdefghijklmnopqrstuvwxyz"
33             "0123456789+/";
34
35
36static inline bool is_base64(unsigned char c) {
37  return (isalnum(c) || (c == '+') || (c == '/'));
38}
39
40//Hex Output stuff
41
42struct HexCharStruct {
43  unsigned char c;
44  HexCharStruct(unsigned char _c) : c(_c) { }
45};
46
47inline std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs) {
48  return (o << "0x" << std::hex << (int)hs.c) << std::dec;
49}
50
51inline HexCharStruct hex(unsigned char _c) {
52  return HexCharStruct(_c);
53}
54
55inline void printBytes(char* buff, unsigned nchars) {
56  for(unsigned i = 0; i < nchars; ++i) {
57    std::cout << hex(buff[i]) << " ";
58  }
59  std::cout << std::dec << std::endl;
60}
61
62//Bitwise comparison of 8-bytes array with uint_64
63inline bool same8(char* array, uint64_t other) {
64  return ((*((uint64_t*)array)) ^ other) == 0;
65}
66//Bitwise comparison of 4-bytes array with uint_32
67inline bool same4(char* array, uint32_t other) {
68  return ((*((uint32_t*)array)) ^ other) == 0;
69}
70//Load a big-endian 32-bit unsigned int from an array
71inline uint32_t readBE4U(char* array) { return __builtin_bswap32(*((uint32_t*)array)); }
72//Load a big-endian 16-bit unsigned int from an array
73inline uint16_t readBE2U(char* array) { return __builtin_bswap16(*((uint16_t*)array)); }
74//Load a big-endian 32-bit int from an array
75inline int32_t  readBE4S(char* array) { return __builtin_bswap32(*((int32_t*)array)); }
76//Load a big-endian 16-bit int from an array
77inline int16_t  readBE2S(char* array) { return __builtin_bswap16(*((int16_t*)array)); }
78//Load a big-endian float from an array
79inline float readBE4F(char* array) {
80   float r;
81   char *rc = ( char* ) & r;
82   rc[0] = array[3];
83   rc[1] = array[2];
84   rc[2] = array[1];
85   rc[3] = array[0];
86   return r;
87}
88
89//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
90inline std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
91  std::string ret;
92  int i = 0;
93  int j = 0;
94  unsigned char char_array_3[3];
95  unsigned char char_array_4[4];
96
97  while (in_len--) {
98    char_array_3[i++] = *(bytes_to_encode++);
99    if (i == 3) {
100      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
101      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
102      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
103      char_array_4[3] = char_array_3[2] & 0x3f;
104
105      for(i = 0; (i <4) ; i++)
106        ret += base64_chars[char_array_4[i]];
107      i = 0;
108    }
109  }
110
111  if (i)
112  {
113    for(j = i; j < 3; j++)
114      char_array_3[j] = '\0';
115
116    char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
117    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
118    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
119
120    for (j = 0; (j < i + 1); j++)
121      ret += base64_chars[char_array_4[j]];
122
123    while((i++ < 3))
124      ret += '=';
125
126  }
127
128  return ret;
129
130}
131
132//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
133inline std::string base64_decode(std::string const& encoded_string) {
134  int in_len = encoded_string.size();
135  int i = 0;
136  int j = 0;
137  int in_ = 0;
138  unsigned char char_array_4[4], char_array_3[3];
139  std::string ret;
140
141  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
142    char_array_4[i++] = encoded_string[in_]; in_++;
143    if (i ==4) {
144      for (i = 0; i <4; i++)
145        char_array_4[i] = base64_chars.find(char_array_4[i]);
146
147      char_array_3[0] = ( char_array_4[0] << 2       ) + ((char_array_4[1] & 0x30) >> 4);
148      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
149      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) +   char_array_4[3];
150
151      for (i = 0; (i < 3); i++)
152        ret += char_array_3[i];
153      i = 0;
154    }
155  }
156
157  if (i) {
158    for (j = 0; j < i; j++)
159      char_array_4[j] = base64_chars.find(char_array_4[j]);
160
161    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
162    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
163
164    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
165  }
166
167  return ret;
168}
169
170// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
171inline std::string escape_json(const std::string &s) {
172    std::ostringstream o;
173    for (auto c = s.cbegin(); c != s.cend(); c++) {
174        if (*c == '"' || *c == '\\' || ('\x00' <= *c && *c <= '\x1f')) {
175            o << "\\u"
176              << std::hex << std::setw(4) << std::setfill('0') << (int)*c;
177        } else {
178            o << *c;
179        }
180    }
181    return o.str();
182}
183
184inline std::string to_utf8(const std::u16string &s) {
185  std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> conv;
186  std::string u = conv.to_bytes(s);
187  u.erase(std::find(u.begin(), u.end(), '\0'), u.end());
188  return u;
189}
190
191}
192
193#endif /* UTIL_H_ */