Cod sursa(job #1345488)

Utilizator cosmincavCrecana Constantin-Cosmin cosmincav Data 17 februarie 2015 17:36:24
Problema Convertor Scor 0
Compilator java Status done
Runda rosedu_cdl_2015 Marime 3.19 kb
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Mai m = new Mai();
		m.open();
		m.process();
	}

}
class Keys {
	List<String> keys = new ArrayList<String>();
	public void add(String key) {
		keys.add(key);
	}
}

class Mai {
	Keys keys = new Keys();
	Scanner reader;
	PrintWriter writer;
	int index;
	int numberOfKeys = 0;

	public void open() {
		try {
			reader = new Scanner(new FileInputStream("convertor.in"));
			writer = new PrintWriter("convertor.out");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int findKey(String line, int i) {
		for (int j = i + 1; j < line.length(); j++) {
			if (line.charAt(j) == '"') {
				keys.add(line.substring(i + 1, j));
				return j;

			}
		}
		return 0;
	}

	public void start(String line) {
		int position = 0;
		boolean found = false;
		while (true) {
			for (int i = 0; i < line.length(); i++) {
				if (line.charAt(i) == '"') {
					position = findKey(line, i);
					found = true;
					break;
				}
			}
			if (!found) {
				line = reader.nextLine();

			} else {
				found = false;
				line = line.substring(position + 1);
				while (true) {
					for (int i = 0; i < line.length(); i++) {
						if (line.charAt(i) == ',') {
							found = true;
							line = line.substring(i + 1);
							break;
						}
						if (line.charAt(i) == '}') {
							return;
						}
					}
					if (!found) {

						line = reader.nextLine();

					} else {
						found = false;
						break;

					}
				}
			}
		}
	}

	public String finish(String line) {
		String value;
		int i;
		if (line.charAt(0) == '"') {
			value = line.substring(0, line.indexOf('"', 1) + 1);

		} else {
			for( i = 0; i < line.length(); i++ ) {
				if( line.charAt(i) == ' ' ||  line.charAt(i) == ',')
					break;
			}
			value = line.substring(0, i);
		}

		writer.write(value + ",");
		index++;
		if (index % numberOfKeys == 0) {
			writer.write("\n");
		}
		return line.substring(value.length());
	}

	public String getValue(String line) {
		while (true) {
			for (int i = 0; i < line.length(); i++) {
				if (line.charAt(i) != ' ') {
					line = line.substring(i);
					return finish(line);

				}
			}
			line = reader.nextLine();
		}
	}

	public void process() {
		String line = reader.nextLine();
		if (!line.contains("{")) {
			line = reader.nextLine();
		}
		line.substring(line.indexOf('{') + 1);
		start(line);
		reader.close();
		open();

		String first = "";
		for (String s : keys.keys) {
			first += s + (",");
			numberOfKeys++;
		}
		writer.write(first + "\n");
		line = reader.nextLine();
		while (line != null) {
			if (line.length() != 0) {
				if (line.contains(":")) {
					line = line.substring(line.indexOf(':') + 1);
					line = getValue(line);

				} else {
					if (!reader.hasNextLine())
						break;
					line = reader.nextLine();
				}
			} else {
				if (!reader.hasNextLine())
					break;
				line = reader.nextLine();
			}

		}
		writer.close();
		reader.close();
	}
}