//============================================================================
// Name : Converter.cpp
// Author : andrei
// Version :
// Copyright : Your copyright notice
// Description : InfoArena problem
//============================================================================
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
std::vector<std::string> &split(const std::string &s, char delim,
std::vector<std::string> &elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
string clear(string &val) {
if (val == "\"\"") {
return "";
}
if (val.find('"', 0) != string::npos) {
int a = val.find_first_of('"');
int b = val.find_last_of('"');
return val.substr(a + 1, b - a - 1);
}
val.erase(remove_if(val.begin(), val.end(), ::isspace), val.end());
if (val.find('?', 0) != string::npos) {
return val.substr(1, val.size());
}
if (val.find('}', 0) != string::npos) {
return val.substr(0, val.find('}'));
}
return val;
}
void printNames(string line, FILE *out, int &size) {
std::vector<std::string> v = split(line, ',');
std::vector<std::string> p;
for (std::vector<std::string>::iterator it = v.begin(); it != v.end();
++it) {
p = split(*it, ':');
if (p.size() >= 2) {
fputs(clear(p[0]).c_str(), out);
fputs(",", out);
size++;
}
}
}
void printValues(string line, FILE *out, int &size, int &count) {
std::vector<std::string> v = split(line, ',');
std::vector<std::string> p;
for (std::vector<std::string>::iterator it = v.begin(); it != v.end();
++it) {
p = split(*it, ':');
if (p.size() >= 2) {
if (count % size == 0) {
fputs("\n", out);
}
fputs(clear(p[1]).c_str(), out);
fputs(",", out);
count++;
}
}
}
void convert(FILE *in, FILE *out) {
int size = 0, count = 0;
char line[1024];
fgets(line, 1024, in);
while (line != NULL && strchr(line, '}') == NULL) {
printNames(line, out, size);
fgets(line, 1024, in);
}
printNames(string(line).substr(0, string(line).find("}")), out, size);
fseek(in, 0, 0);
while (!feof(in)) {
fgets(line, 1024, in);
printValues(line, out, size, count);
}
}
int main() {
FILE *in = fopen("converter.in", "r");
if (in == NULL) {
perror(0);
}
FILE *out = fopen("converter.out", "w");
if (out == NULL) {
perror(0);
}
convert(in, out);
fclose(in);
fclose(out);
return 0;
}