Debian Codegraph @

source/code/stats.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2016 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 http://www.blockattack.net
21 ===========================================================================
22 */
23
24 #include "stats.h"
25 #include "common.h"
26 #include "os.hpp"
27 #include "sago/SagoMisc.hpp"
28 #include <sstream>
29
30
31 Stats* Stats::instance = nullptr;
32
33 const char* const statsFileName = "statsFile";
34
35 Stats::Stats() {
36 statMap.clear();
37 load();
38 }
39
40 void Stats::load() {
41 std::string fileContent = sago::GetFileContent(statsFileName);
42 std::stringstream inFile(fileContent);
43 std::string key;
44 std::string value;
45 if (inFile) {
46 while (!inFile.eof()) {
47 inFile >> key; // The key is first on line
48 inFile.get(); //Take the space
49 getline(inFile, value); //The rest of the line is the value.
50 statMap[key] = str2int(value);
51 }
52 }
53 }
54
55 Stats* Stats::getInstance() {
56 if (!Stats::instance) {
57 Stats::instance = new Stats();
58
59 }
60 return Stats::instance;
61 }
62
63 void Stats::save() {
64 std::stringstream outFile;
65 std::map<std::string, unsigned int>::iterator iter;
66 for (iter = statMap.begin(); iter != statMap.end(); ++iter) {
67 outFile << iter->first << " " << iter->second << "\n";
68 }
69 sago::WriteFileContent(statsFileName, outFile.str());
70 }
71
72 unsigned int Stats::getNumberOf(const std::string& statName) {
73 if (exists(statName)) {
74 return statMap[statName];
75 }
76 else {
77 return 0;
78 }
79 }
80
81 void Stats::addOne(const std::string& statName) {
82 std::map<std::string, unsigned int>::iterator iter = statMap.find(statName);
83 if (iter == statMap.end()) {
84 statMap[statName] = 1;
85 }
86 else {
87 iter->second++;
88 }
89 }
90
91 bool Stats::exists(const std::string& statName) {
92 //Using that 'find' returns an iterator to the end of the map if not found
93 return statMap.find(statName) != statMap.end();
94 }