Cod sursa(job #1345751)

Utilizator GeorgianaElenaDolocan Georgiana Elena GeorgianaElena Data 17 februarie 2015 20:45:59
Problema Convertor Scor 20
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.47 kb
#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;
}

int eat_whitespace(string str, int pos) {
	while (str[pos] == ' ')
		pos++;
	return pos;
}

void find_pairs(string str)
{
	bool quote_closed = true;
	long length = str.length();
	//bool open_obj = false;
	bool obj_closed = false;
	size_t i = 0;
	int obj = 0;
	int pos = 0;
	ofstream out;
	out.open ("convertor.out");

	string keys;
	string values;

	while (i < length) {
		if (obj == 0){
			if (str[i] == '\"' && obj == 0) {
				quote_closed = !quote_closed;
				if (quote_closed == false) {
					i++;
					while (str[i] != '\"') {
						keys += str[i];
				//		out << endl << "HA!!!! " << str[i];
						i++;
						pos++;
					}
				}
				quote_closed = true;
				keys += ',';
			}
			else if (str [i] == ':' && obj == 0) {
				i++;
				while (str [i] == ' ')
					i++;
					while (str[i] != ',' && str[i] != '}'){
						if (str[i] != '\"' && quote_closed == true && str[i] != ' '){
							//out << str[i];
							values += str[i];
						}
						else if (str[i] == '\"') {
							quote_closed = !quote_closed;
							if (quote_closed == false) {
								i++;
								while (str[i] != '\"') {
									values += str[i];
									i++;
								}
							}
							quote_closed = true;
						}
						i++;
					}
				//out <<",";
				values += ',';
			}
		} else {
			if (str [i] == ':') {
				i++;
				while (str [i] == ' '){
					i++;
				}
				while (str[i] != ',' && str[i] != '}'){
					if (str[i] != '\"' && quote_closed == true && str[i] != ' '){
						out << str[i];
					//	values += str[i];
					}
					else if (str[i] == '\"') {
						quote_closed = !quote_closed;
						if (quote_closed == false) {
							i++;
							while (str[i] != '\"') {
								out << str[i];
								i++;
							}
						}
						quote_closed = true;
						while (str [i] == ' '){
					i++;
				}
					}
				i++;
				}
				while (str [i] == ' '){
					i++;
				}
				out <<",";
				//values += ',';
			}
		}
	
		if (quote_closed == true && str[i] == '{') {
			obj_closed = false;
		} 
		if (quote_closed == true && str[i] == '}') {
			if (obj == 0)
				out << keys << endl;
			obj++;
			out << values;
			values.clear();
			obj_closed = true;
			out << endl;
		} 
		i++;
	}
}

int main()
{
	string str = read_the_file();
	map <string, string> pair;
	find_pairs(str);
}