Cod sursa(job #1345737)

Utilizator GeorgianaElenaDolocan Georgiana Elena GeorgianaElena Data 17 februarie 2015 20:41:04
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.43 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;

	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];
				//		cout << 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] != ' '){
							//cout << 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++;
					}
				//cout <<",";
				values += ',';
			}
		} else {
			if (str [i] == ':') {
				i++;
				while (str [i] == ' '){
					i++;
				}
				while (str[i] != ',' && str[i] != '}'){
					if (str[i] != '\"' && quote_closed == true && str[i] != ' '){
						cout << str[i];
					//	values += str[i];
					}
					else if (str[i] == '\"') {
						quote_closed = !quote_closed;
						if (quote_closed == false) {
							i++;
							while (str[i] != '\"') {
								cout << str[i];
								i++;
							}
						}
						quote_closed = true;
						while (str [i] == ' '){
					i++;
				}
					}
				i++;
				}
				while (str [i] == ' '){
					i++;
				}
				cout <<",";
				//values += ',';
			}
		}
	
		if (quote_closed == true && str[i] == '{') {
			obj_closed = false;
		} 
		if (quote_closed == true && str[i] == '}') {
			if (obj == 0)
				cout << keys << endl;
			obj++;
			cout << values;
			values.clear();
			obj_closed = true;
			cout << endl;
		} 
		i++;
	}
}

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