Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
Tools.hpp
Go to the documentation of this file.
1//
2// Created by robin on 31.07.2020.
3//
4
5#ifndef TILESON_TOOLS_HPP
6#define TILESON_TOOLS_HPP
7
8#include <cstdint>
9#include <vector>
10#include <string_view>
11namespace tson
12{
13 class Tools
14 {
15
16 public:
17 Tools() = delete;
18 ~Tools() = delete;
19 inline static std::vector<uint8_t> Base64DecodedStringToBytes(std::string_view str);
20 inline static std::vector<uint32_t> BytesToUnsignedInts(const std::vector<uint8_t> &bytes);
21 inline static std::vector<std::string> SplitString(const std::string &s, char delim);
22 inline static bool Equal(float a, float b, float precision = 8192.f);
23
24 private:
25 template<typename Out>
26 static void split(const std::string &s, char delim, Out result)
27 {
28 std::stringstream ss;
29 ss.str(s);
30 std::string item;
31
32 while (std::getline(ss, item, delim))
33 {
34 *(result++) = item;
35 }
36 }
37 };
38
44 std::vector<uint8_t> Tools::Base64DecodedStringToBytes(std::string_view str)
45 {
46 std::vector<uint8_t> bytes;
47 for(size_t i = 0; i < str.size(); ++i)
48 {
49 uint8_t u8 = static_cast<uint8_t>(str[i]);
50 bytes.push_back(u8);
51 }
52 return bytes;
53 }
54
60 std::vector<uint32_t> Tools::BytesToUnsignedInts(const std::vector<uint8_t> &bytes)
61 {
62 std::vector<uint32_t> uints;
63 std::vector<uint8_t> toConvert;
64 //uint32_t size8 = (compressed[55] << 24) | (compressed[56] << 16) | (compressed[57] << 8) | compressed[58]; //Should be 66000
65
66 for(size_t i = 0; i < bytes.size(); ++i)
67 {
68 toConvert.push_back(bytes[i]);
69 if(toConvert.size() == 4)
70 {
71 uint32_t u32 = (toConvert[3] << 24) | (toConvert[2] << 16) | (toConvert[1] << 8) | toConvert[0];
72 uints.push_back(u32);
73 toConvert.clear();
74 }
75 }
76
77 return uints;
78 }
79
80 std::vector<std::string> Tools::SplitString(const std::string &s, char delim)
81 {
82 std::vector<std::string> elems;
83 split(s, delim, std::back_inserter(elems));
84 return elems;
85 }
86
93 bool Tools::Equal(float a, float b, float precision)
94 {
95 float threshold = 1.f / precision;
96 float diff = fabsf(a - b);
97 return diff <= threshold;
98 }
99}
100
101#endif //TILESON_TOOLS_HPP
Definition Tools.hpp:14
Tools()=delete
static bool Equal(float a, float b, float precision=8192.f)
Definition Tools.hpp:93
static std::vector< uint8_t > Base64DecodedStringToBytes(std::string_view str)
Definition Tools.hpp:44
static std::vector< std::string > SplitString(const std::string &s, char delim)
Definition Tools.hpp:80
static std::vector< uint32_t > BytesToUnsignedInts(const std::vector< uint8_t > &bytes)
Definition Tools.hpp:60
~Tools()=delete
Definition Base64.hpp:12