Cleaned up the decoder, weird failbit behaviour from gitline.

This commit is contained in:
MitchellHansen
2017-04-09 03:03:13 -07:00
parent 66ca187f45
commit c7ddfd8ba5
6 changed files with 190 additions and 241 deletions

View File

@@ -6,7 +6,12 @@ Decoder::Decoder() {
std::cout << "Loading patterns...";
for (std::experimental::filesystem::directory_entry p : std::experimental::filesystem::directory_iterator("../assets/patterns/")) {
pattern_list.push_back(p.path().generic_string());
// good lord c++
std::string s = p.path().generic_string();
const char* char_shit = new const char[s.size() + 1];
memcpy((void*)char_shit, s.c_str(), s.size()+1);
pattern_list.push_back(char_shit);
}
std::cout << "Done" << std::endl;
@@ -20,124 +25,130 @@ pattern_info Decoder::decodePattern(std::string pattern) {
pattern_info info;
//for (int i = 0; i < dimensions.x * dimensions.y; i++) {
// nodes[i] = 0;
//}
std::ifstream file(pattern);
if (!file.is_open()) {
std::cout << "unable to open file" << std::endl;
return info;
}
//std::ifstream file(filename);
std::vector<std::vector<char>> grid;
std::stringstream rle_stream;
//if (!file.is_open()) {
// std::cout << "unable to open file" << std::endl;
// return info;
//}
for (std::string line; std::getline(file, line); )
{
// Grab the header comments
if (line[0] == '#') {
//for (std::string line; std::getline(file, line); )
//{
// // Grab the header comments
// if (line[0] == '#') {
std::string data = line.substr(3, line.size() - 3);
// std::string data = line.substr(3, line.size() - 3);
switch (line[1]) {
// switch (line[1]) {
case 'C': {
info.comments += data;
break;
}
case 'O': {
info.author += data;
break;
}
case 'N': {
info.title += data;
break;
}
default: {
std::cout << "Case : " << line[1] << " not supported" << std::endl;
}
// case 'C': {
// info.comments += data;
// break;
// }
// case 'O': {
// info.author += data;
// break;
// }
// case 'N': {
// info.title += data;
// break;
// }
// default: {
// std::cout << "Case : " << line[1] << " not supported" << std::endl;
// }
// }
// }
}
}
// // grab the dimensions
// else if (line[0] == 'x') {
// grab the dimensions
else if (line[0] == 'x') {
// // Naively assume that it is in the exact form "x = <num>, y = <num>,"
// std::stringstream ss(line);
// Naively assume that it is in the exact form "x = <num>, y = <num>,"
std::stringstream ss(line);
// std::string temp;
// ss >> temp >> temp >> temp;
// info.dimensions.x = std::stoi(temp.substr(0, temp.size() - 1));
std::string temp;
ss >> temp >> temp >> temp;
info.dimensions.x = std::stoi(temp.substr(0, temp.size() - 1));
// ss >> temp >> temp >> temp;
// info.dimensions.y = std::stoi(temp.substr(0, temp.size() - 1));
// }
ss >> temp >> temp >> temp;
info.dimensions.y = std::stoi(temp.substr(0, temp.size() - 1));
}
// // Decode the RLE
// else {
// Decode the RLE
else {
// std::vector<std::vector<char>> grid;
rle_stream << line;
std::string token;
// std::stringstream ss(line);
// std::string token;
while (std::getline(rle_stream, token, '$')) {
// while (std::getline(ss, token, '$')) {
if (rle_stream.eof()) {
// std::vector<char> char_line;
// unsigned int token_pos = 0;
// std::string tmp;
if (token.back() != '!') {
rle_stream.seekg(-token.size(), std::ios::end);
break;
}
}
// while (token_pos < token.size()) {
std::vector<char> char_line;
unsigned int token_pos = 0;
std::string tmp;
// char status = -1;
// if (token[token_pos] == 'b')
// status = 0;
// else if (token[token_pos] == 'o')
// status = 1;
// else if (token[token_pos] == '!')
// break;
// else
// tmp += token[token_pos];
while (token_pos < token.size()) {
// token_pos++;
char status = -1;
if (token[token_pos] == 'b')
status = 0;
else if (token[token_pos] == 'o')
status = 1;
else if (token[token_pos] == '!')
break;
else
tmp += token[token_pos];
// if (status >= 0) {
// if (tmp.empty()) {
// char_line.push_back(status);
// }
// else {
// int count = std::stoi(tmp);
// for (int i = 0; i < count; i++) {
// char_line.push_back(status);
// }
// tmp.clear();
// }
// }
token_pos++;
// }
if (status >= 0) {
if (tmp.empty()) {
char_line.push_back(status);
}
else {
int count = std::stoi(tmp);
for (int i = 0; i < count; i++) {
char_line.push_back(status);
}
tmp.clear();
}
}
// grid.push_back(char_line);
// }
}
// int y_mod = 200;
// int x_mod = 200;
// for (int y = 0; y < grid.size(); y++) {
// for (int x = 0; x < grid.at(y).size(); x++) {
// nodes[(y + y_mod) * dimensions.x + (x + x_mod)] = grid.at(y).at(x);
// }
// }
char_line.resize(info.dimensions.x);
grid.push_back(char_line);
}
// }
//}
rle_stream.clear();
//std::cout << info.author << std::endl;
//std::cout << info.title << std::endl;
//std::cout << info.comments << std::endl;
}
}
info.dimensions = sf::Vector2i(grid.at(0).size(), grid.size());
info.nodes = new char[info.dimensions.x * info.dimensions.y];
for (int y = 0; y < grid.size(); y++) {
for (int x = 0; x < grid.at(y).size(); x++) {
info.nodes[y * info.dimensions.x + x] = grid.at(y).at(x);
}
}
file.close();
return info;
}
std::vector<std::string> Decoder::getPatternList() {
std::vector<const char*> Decoder::getPatternList() {
return pattern_list;