SuperTuxKart
Loading...
Searching...
No Matches
hardware_stats.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2014-2015 Joerg Henrichs
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_HARDWARE_STATS_HPP
20#define HEADER_HARDWARE_STATS_HPP
21
26#include "utils/no_copy.hpp"
27#include "utils/string_utils.hpp"
28
29namespace HardwareStats
30{
32 class Json : public NoCopy
33 {
34 private:
36 std::string m_data;
37 public:
40 {
41 m_data.reserve(1024);
42 m_data ="{";
43 } // Constructor
44
45 const std::string sanitize(std::string value)
46 {
47 // A string is a sequence of Unicode code points wrapped with quotation marks (U+0022). All code points may
48 // be placed within the quotation marks except for the code points that must be escaped: quotation mark
49 // (U+0022), reverse solidus (U+005C), and the control characters U+0000 to U+001F. There are two-character
50 // escape sequence representations of some characters.
51 char temp[7] = {0};
52 for (size_t i = 0; i < value.size(); i++)
53 {
54 uint8_t codepoint = value[i];
55 if (codepoint <= 0x1f)
56 {
57 sprintf(temp, "\\u%04x", codepoint);
58 std::string suffix = value.substr(i + 1);
59 value = value.substr(0, i);
60 value.append(temp);
61 value.append(suffix);
62 i += 5; // \u0000 = 6 chars, but we're replacing one so 5
63 }
64 else if (codepoint == '"' || codepoint == '\\')
65 {
66 std::string suffix = value.substr(i + 1);
67 value = value.substr(0, i);
68 value.push_back('\\');
69 value.push_back((char)codepoint);
70 value.append(suffix);
71 // Skip the added solidus
72 i++;
73 }
74 }
75 return value;
76 } // sanitize
77 // --------------------------------------------------------------------
79 template <typename C>
80 void add(const std::string &key, const C&value)
81 {
82 if(m_data.size()>1) // more than '{'
83 m_data += ",";
84 m_data += "\""+sanitize(key)+"\":"+StringUtils::toString(value);
85 } // add
86 // --------------------------------------------------------------------
89 void add(const std::string &key, const std::string &value)
90 {
91 if(m_data.size()>1) // more than '{'
92 m_data += ",";
93 m_data += "\""+sanitize(key)+"\":\""+sanitize(value)+"\"";
94 } // add
95 // --------------------------------------------------------------------
98 void add(const std::string &key, const char *s)
99 {
100 if(m_data.size()>1) // more than '{'
101 m_data += ",";
102 m_data += "\""+sanitize(key)+"\":\""+sanitize(s)+"\"";
103 } // add
104 // --------------------------------------------------------------------
105 void finish()
106 {
107 m_data += "}";
108 }
109 // --------------------------------------------------------------------
111 std::string toString() { return m_data; }
112 }; // class Json
113
114 // ========================================================================
115 void reportHardwareStats();
116 const std::string& getOSVersion();
117 int getNumProcessors();
118}; // HardwareStats
119
120#endif
A class to manage json data.
Definition: hardware_stats.hpp:33
std::string toString()
Returns the json data as one string.
Definition: hardware_stats.hpp:111
Json()
Constructor.
Definition: hardware_stats.hpp:39
void add(const std::string &key, const std::string &value)
Specialisation for adding string values.
Definition: hardware_stats.hpp:89
std::string m_data
The accumulated json data.
Definition: hardware_stats.hpp:36
void add(const std::string &key, const char *s)
Specialisation for adding character pointers.
Definition: hardware_stats.hpp:98
void add(const std::string &key, const C &value)
Adds a key-value pair to the json string.
Definition: hardware_stats.hpp:80
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26