#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>
#include <map>
using namespace std;
/*string read_the_file()
{
ifstream t("convertor.in");
string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
*/
void read_the_file(int &pos, string &str, int &eof)
{
ifstream in("convertor.in");
string line;
while (pos <= 3000 && !in.eof()) {
getline(in, line);
str += line;
pos++;
}
if (in.eof())
eof = 1;
}
size_t eat_whitespace(const string &str, size_t pos) {
while (str[pos] == ' ')
pos++;
return pos;
}
void inside_quote (const string &str, string &keys, size_t &pos,
bool quote_closed, bool first_object, ofstream &file)
{
quote_closed = !quote_closed;
if (quote_closed == false) {
pos++;
while (str[pos] != '\"') {
if (first_object == true)
keys += str[pos];
else
file << str[pos];
pos++;
}
}
quote_closed = true;
}
void return_keys(const string &str, string &keys, size_t &pos,
bool quote_closed, bool first_object, ofstream &file)
{
inside_quote (str, keys, pos, quote_closed, first_object, file);
keys += ',';
}
void return_values (const string &str, string &values, size_t &pos,
bool quote_closed, bool first_object, ofstream &file)
{
pos = eat_whitespace(str, pos + 1);
while (str[pos] != ',' && str[pos] != '}') {
if (str[pos] != '\"' && quote_closed == true &&
str[pos] != ' ' && str[pos] != '\n') {
if (first_object == true)
values += str[pos];
else
file << str[pos];
}
else if (str[pos] == '\"') {
inside_quote (str, values, pos, quote_closed, first_object, file);
}
pos++;
}
if (first_object == true)
values += ',';
else
file << ",";
}
void find_pairs(const string &str)
{
bool quote_closed = true;
int obj = 0;
size_t length = str.length();
size_t i = 0;
ofstream out;
out.open ("convertor.out");
string keys;
string values;
while (i < length) {
if (obj == 0){
if (str[i] == '\"') {
return_keys(str, keys, i, quote_closed, true, out);
}
else if (str [i] == ':') {
return_values(str, values, i, quote_closed, true, out);
}
} else {
if (str [i] == ':') {
return_values(str, values, i, quote_closed, false, out);
}
}
if (quote_closed == true && str[i] == '}') {
if (obj == 0)
out << keys << endl;
obj++;
out << values << endl;
values.clear();
}
i++;
}
}
int main()
{
int pos = 0;
string str;
int end_of_file = 0;
read_the_file(pos, str, end_of_file);
cout << str << endl;
find_pairs(str);
while (end_of_file == 0 && pos == 3000) {
pos = 0;
read_the_file(pos, str, end_of_file);
cout << str << endl;
find_pairs(str);
}
}