/
pucatrade_format_converter.cpp
178 lines (150 loc) · 4.2 KB
/
pucatrade_format_converter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright: (c) Kayne Ruse 2013-2015
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
/* DOCUMENTATION
This is a rough format converter for pucatrade. Since pucatrade's "import cards"
system is tempremental, I wrote this to deal with it. Note that this was
tailored for my own personal collection, so most sets are not covered (but can
be added below), and that there is no explicit foil-handling or special edition
handling at the present time.
Compiles under C++11
Expected source format:
1x Shivan Dragon (ORI)
1x Urborg, Tomb of Yawgmoth (M15)
1x Chromatic Lantern (RTR)
Intended output format:
1x Shivan Dragon Magic Origins
1x Urborg, Tomb of Yawgmoth Magic 2015 Core Set
1x Chromatic Lantern Return to Ravnica
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <stdexcept>
#include <string>
typedef std::map<std::string, std::string> table_t;
const table_t conversions = {
//core sets
{"(ORI)", "Magic Origins"},
{"(M15)", "Magic 2015 Core Set"},
{"(M14)", "Magic 2014 Core Set"},
{"(M13)", "Magic 2013"},
{"(M12)", "Magic 2012"},
{"(M11)", "Magic 2011"},
{"(M10)", "Magic 2010"},
//blocks
{"(BFZ)", "Battle for Zendikar"},
{"(DTK)", "Dragons of Tarkir"},
{"(FRF)", "Fate Reforged"},
{"(KTK)", "Khans of Tarkir"},
{"(JOU)", "Journey into Nyx"},
{"(BNG)", "Born of the Gods"},
{"(THS)", "Theros"},
{"(DGM)", "Dragon's Maze"},
{"(GTC)", "Gatecrash"},
{"(RTR)", "Return to Ravnica"},
{"(AVR)", "Avacyn Restored"},
{"(DKA)", "Dark Ascension"},
{"(ISD)", "Innistrad"},
{"(WWK)", "Worldwake"},
//supplements
// {"(MMA)", "Modern Masters"},
{"(MM2)", "Modern Masters 2015"},
{"(C14)", "Commander 2014"},
{"(DDJ)", "Duel Decks: Izzet vs. Golgari"},
};
//compare two strings starting from the end
//returns similar to strncmp
int rstrncmp(std::string lhs, std::string rhs, int rnum) {
auto lit = lhs.rbegin();
auto rit = rhs.rbegin();
int ret = 0;
while (rnum-- > 0) {
if (*lit < *rit) {
ret = -1;
break;
}
if (*lit > *rit) {
ret = 1;
break;
}
//check if the "end" of the string has been reached
if (lit++ == lhs.rend()) {
rnum = -1;
ret += -1;
}
if (rit++ == rhs.rend()) {
rnum = -1;
ret += 1;
}
}
//rnum ran out or was set to -1
return ret;
}
int main(int argc, char* argv[]) {
//usage check
if (argc != 3) {
std::cout << "USAGE: " << argv[0] << " SOURCE DEST" << std::endl;
return 0;
}
//open the files
std::fstream is(argv[1], std::fstream::in);
if (!is.is_open()) {
std::cerr << "Failed to open " << argv[1] << std::endl;
return 1;
}
std::fstream os(argv[2], std::fstream::out);
if (!os.is_open()) {
std::cerr << "Failed to open " << argv[2] << std::endl;
is.close();
return 1;
}
//read in
std::list<std::string> inputList;
std::string tmp;
while(!is.eof()) {
getline(is, tmp);
inputList.push_back(tmp);
}
//convert
std::for_each(inputList.begin(), inputList.end(), [conversions](std::string& str) {
//compare the end of the given string to the conversion keys
for (auto const& it : conversions) {
if (rstrncmp(it.first, str, it.first.size()) == 0) {
//rewrite the end
str = str.substr(0, str.size() - it.first.size());
str += it.second;
return;
}
}
std::cout << "WARNING: Unknown card: " << str << std::endl;
});
//write the output
for (auto& it : inputList) {
os << it << std::endl;
}
//close the files
is.close();
os.close();
return 0;
}