-
Notifications
You must be signed in to change notification settings - Fork 218
/
FltkPreferenceImporter.cpp
149 lines (125 loc) · 3.57 KB
/
FltkPreferenceImporter.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FltkPreferenceImporter.h"
#include <Imports/SDL.h>
#include <string>
#include <Core/Debug.h>
#include "SdlFileStream.h"
#include <Core/Strings.h>
#ifdef WIN32
#include <windows.h>
#include <wchar.h>
#else
#include <unistd.h>
#endif
namespace spades {
#ifdef WIN32
static std::string SdlReceiveString(char *ptr) {
if(!ptr) {
return "";
}
std::string s(ptr);
SDL_free(ptr);
return s;
}
static std::wstring Utf8ToWString(const char *s) {
auto *ws = (WCHAR*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", (char *)(s), SDL_strlen(s)+1);
if(!ws) return L"";
std::wstring wss(ws);
SDL_free(ws);
return wss;
}
#endif
static std::string GetFltkPreferencePath() {
#if defined(WIN32)
std::string path = SdlReceiveString(SDL_GetPrefPath("yvt.jp", "OpenSpades.prefs"));
if(path.back() == '\\') path.resize(path.size() - 1);
return path;
#elif defined(__APPLE__)
const char *homeptr = getenv("HOME");
std::string home = homeptr ? homeptr : "";
home += "/Library/Preferences/yvt.jp/OpenSpades.prefs";
return home;
#else
const char *homeptr = getenv("HOME");
std::string home = homeptr ? homeptr : "";
home += "/.fltk/yvt.jp/OpenSpades.prefs";
return home;
#endif
}
std::vector<std::pair<std::string, std::string>> ImportFltkPreference() {
std::vector<std::pair<std::string, std::string>> ret;
auto path = GetFltkPreferencePath();
SPLog("Checking for legacy preference file: %s", path.c_str());
auto *rw = SDL_RWFromFile(path.c_str(), "rb");
if(rw == nullptr) {
SPLog("Legacy preference file wasn't found.");
return ret;
}
SdlFileStream stream(rw, true);
std::string text = stream.ReadAllBytes();
SPLog("Parsing %d bytes of legacy preference file", static_cast<int>(text.size()));
auto lines = SplitIntoLines(text);
std::string buf;
auto flush = [&]{
if(buf.empty()) return;
auto idx = buf.find(':');
if(idx != std::string::npos) {
auto name = buf.substr(0, idx);
auto value = buf.substr(idx + 1);
if(name.size() > 0)
ret.emplace_back(name, value);
}
buf.clear();
};
for(auto& line: lines) {
{
auto idx = line.find(';');
if(idx != std::string::npos) {
line.resize(idx);
}
}
if(line.size() == 0) continue;
if(line[0] == '[') continue; // group is not used
if(line[0] == '+') {
// continuation
buf.append(line, 1, line.size() - 1);
}else{
flush();
buf = line;
}
}
flush();
return ret;
}
void DeleteFltkPreference() {
auto path = GetFltkPreferencePath();
/*
#ifdef WIN32
DeleteFileW(Utf8ToWString(path.c_str()).c_str());
#else
unlink(path.c_str());
#endif
*/
// afraid of removing the pref file completely
SPLog("Moving %s to %s", path.c_str(), (path + "-old").c_str());
#ifdef WIN32
auto s = Utf8ToWString(path.c_str());
MoveFileW(s.c_str(), (s + L"-old").c_str());
#else
rename(path.c_str(), (path + "-old").c_str());
#endif
}
}