Cod sursa(job #1337827)

Utilizator tinamaneaValentina Manea tinamanea Data 9 februarie 2015 15:52:43
Problema Convertor Scor 70
Compilator java Status done
Runda rosedu_cdl_2015 Marime 1.79 kb
import java.lang.*;
import java.nio.file.*;
import java.io.*;
import java.util.*;

public class Main {

	static char consumeWhite(FileInputStream fis) throws IOException {
		char c;
		while ((c = (char)fis.read()) == ' ' || c == '\n');
		
		return c;
	}

	public static void main(String[] args) throws IOException {

		StringBuffer sbuf = new StringBuffer();
		StringBuffer buf = new StringBuffer();
		String text, toSplit, key, value;
		int begin, end, begin_pair, end_pair;
		int quote;
		String[] result;
		boolean firstPass = true;
		boolean isInt = false;
		char c;

		FileWriter fw = new FileWriter("convertor.out");
		BufferedWriter bw = new BufferedWriter(fw);

		FileInputStream fis = new FileInputStream("convertor.in");

		while ((c = (char)fis.read()) != '[');
		/* reached [ */
		while ((c = (char)fis.read()) != '{');
		/* reached { */
		while (c == '{') {
			while (c != '}') {
				/* reach quote or number */
				c = consumeWhite(fis);
				if (c != '\"' && c != '\n')
					buf.append(c);
				while ((c = (char)fis.read()) != ':')
					if (c != '\"' && c != '\n')
						buf.append(c);

				key = buf.toString().trim();
				if (firstPass)
					bw.write(key + ",");
				buf = new StringBuffer();

				c = consumeWhite(fis);
				if (c != '\"' && c != '\n') {
					isInt = true;
					buf.append(c);
				}

				while ((c = (char)fis.read()) != ',' && c != '}')
					if (c != '\"' && c != '\n')
						buf.append(c);

				value = buf.toString().trim();
				if (firstPass)
					sbuf.append(value + ",");
				else
					bw.write(value + ",");
				buf = new StringBuffer();
			}

			bw.newLine();

			if (firstPass) {
				bw.write(sbuf.toString());
				bw.newLine();
				firstPass = false;
			}

			while (fis.available() > 0) {
				c = (char)fis.read();
				if (c == '{' || c == ']')
					break;
			}
		}
		bw.close();
	}
}