Cod sursa(job #1360922)

Utilizator raztechsPopa Razvan raztechs Data 25 februarie 2015 18:46:53
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.04 kb
#include <string>
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

ifstream f("convertor.in");
ofstream g("convertor.out");

class JParser {
	private:
		string *json;
		vector <vector <string> > values;
		vector <string> keys;
	public:
		JParser ();
		void JGetKeys();
		void JPrint ();
		void JGetValues();
		void JCSV();
};

JParser::JParser()
{
	f.seekg (0, f.end);
	int length = f.tellg();
	char *buff = new char [length];
	f.seekg (0, f.beg);
	f.read(buff, length);
	json = new string(buff);
	delete[] buff;
}


void JParser::JCSV()
{
	for(unsigned int i = 0; i < keys.size(); i++)
		g << keys[i] << ',';
	g << '\n';

	int x = values[0].size();
	for(int c = 0; c < x; c++)
	{
		for(unsigned int j = 0; j < values.size(); j++)
			g << values[j][c] << ',';
		g << '\n';
	}
}


void JParser::JGetKeys()
{
	string *temp = new string;
	int i = 0, j = 0;
	int g = 0, v = 0, s = 0;

	while(json->at(i) != '}')
	{
		switch(json->at(i))
		{
			case '\n':
				break;
			case '"':
				g++;
				break;
			case ':':
				s++;
				keys.push_back(*temp);
				temp->clear();
				j++;
				break;
			case ',':
				v++;
				break;
			default:
				if(g % 2 != 0 && s == v)
					temp->push_back(json->at(i));
				break;
		}
		i++;
	}
	delete temp;
}


void JParser::JGetValues()
{
	values.resize(keys.size());
	string *temp = new string;

	int i = 0, j = 0;
	int g = 0, v = 0, s = 0;

	while(json->at(i) != ']')
	{
		switch(json->at(i))
		{
			case '\n':
			case '[':
			case '{':
				break;
			case '}':
				values[j].push_back(*temp);
				j = 0;
				temp->clear();
				break;
			case ' ':
				if(g % 2 != 0)
					temp->push_back(json->at(i));
				break;
			case '"':
				if(s > v)
					g++;
				break;
			case ':':
				s++;
				break;
			case ',':
				v++;
				if(!temp->empty())
				{
					values[j].push_back(*temp);
					j++;
					temp->clear();
				}
				break;
			default:
				if(s > v)
					temp->push_back(json->at(i));
				break;
		}
		i++;
	}
	delete temp;
}


int main()
{
	JParser j;

	j.JGetKeys();
	j.JGetValues();
	j.JCSV();

	f.close();
	g.close();
	return 0;
}