File: SettingsHandler.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (127 lines) | stat: -rw-r--r-- 2,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2012 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

// Thanks to Treeki for writing the original class - 29/01/2012

#include <cstddef>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>

#ifdef _WIN32
#include <mmsystem.h>
#include <sys/timeb.h>
#include <windows.h>
#include "Common/CommonFuncs.h" // snprintf
#endif

#include "Common/CommonTypes.h"
#include "Common/SettingsHandler.h"
#include "Common/Timer.h"

SettingsHandler::SettingsHandler()
{
	Reset();
}

const u8* SettingsHandler::GetData() const
{
	return m_buffer;
}

const std::string SettingsHandler::GetValue(const std::string& key)
{
	std::string delim = std::string("\r\n");
	std::string toFind = delim + key + "=";
	size_t found = decoded.find(toFind);

	if (found != decoded.npos)
	{
		size_t delimFound = decoded.find(delim, found + toFind.length());
		if (delimFound == decoded.npos)
			delimFound = decoded.length() - 1;
		return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
	}
	else
	{
		toFind = key + "=";
		found = decoded.find(toFind);
		if (found == 0)
		{
			size_t delimFound = decoded.find(delim, found + toFind.length());
			if (delimFound == decoded.npos)
				delimFound = decoded.length() - 1;
			return decoded.substr(found + toFind.length(), delimFound - (found + toFind.length()));
		}
	}

	return "";
}

void SettingsHandler::Decrypt()
{
	const u8* str = m_buffer;
	while (*str != 0)
	{
		if (m_position >= SETTINGS_SIZE)
			return;
		decoded.push_back((u8)(m_buffer[m_position] ^ m_key));
		m_position++;
		str++;
		m_key = (m_key >> 31) | (m_key << 1);
	}
}

void SettingsHandler::Reset()
{
	decoded = "";
	m_position = 0;
	m_key = INITIAL_SEED;
	memset(m_buffer, 0, SETTINGS_SIZE);
}

void SettingsHandler::AddSetting(const std::string& key, const std::string& value)
{
	for (const char& c : key)
	{
		WriteByte(c);
	}

	WriteByte('=');

	for (const char& c : value)
	{
		WriteByte(c);
	}

	WriteByte(13);
	WriteByte(10);
}

void SettingsHandler::WriteByte(u8 b)
{
	if (m_position >= SETTINGS_SIZE)
		return;

	m_buffer[m_position] = b ^ m_key;
	m_position++;
	m_key = (m_key >> 31) | (m_key << 1);
}

const std::string SettingsHandler::generateSerialNumber()
{
	time_t rawtime;
	tm* timeinfo;
	char buffer[12];
	char serialNumber[12];

	time(&rawtime);
	timeinfo = localtime(&rawtime);
	strftime(buffer, 11, "%j%H%M%S", timeinfo);

	snprintf(serialNumber, 11, "%s%i", buffer, (Common::Timer::GetTimeMs() >> 1) & 0xF);
	serialNumber[10] = 0;
	return std::string(serialNumber);
}