#include "parser.h"

#include "pidir.h"
#include "piiostream.h"


PIVector<ParserSection> parse(const PIString & path) {
	PIVector<ParserSection> ret;
	PIFile f;
	if (!f.open(path, PIIODevice::ReadOnly)) return ret;
	PIString ext = f.fileInfo().extension().toLowerCase();
	PIString dir = PIDir(f.fileInfo().dir()).absolutePath() + "/";
	// piCout << dir;
	if (ext == "conf") return parseConf(f, dir);
	if (ext == "qrc") return parseQRC(f, dir);
	return ret;
}


ParserEntry makeEntry(PIString filename, const PIString & dir) {
	ParserEntry ret;
	ret.path = filename;
	if (!PIDir(PIFile::fileInfo(filename).dir()).isAbsolute()) {
		// piCout << "rel, add dir";
		ret.path.insert(0, dir);
	}
	// piCout << line << path;
	ret.name = filename;
	return ret;
}


PIVector<ParserSection> parseConf(PIFile & file, const PIString & dir) {
	PIVector<ParserSection> ret;
	if (!file.isOpened()) return ret;
	PIIOTextStream ts(&file);
	ParserSection ps;
	while (!ts.isEnd()) {
		PIString line = ts.readLine().trim();
		if (line.isEmpty()) continue;
		if (line.startsWith("[") && line.endsWith("]")) {
			if (!ps.files.isEmpty()) ret << ps;
			ps.name = line.cutLeft(1).cutRight(1).trim();
			ps.files.clear();
			continue;
		}
		PIString alias;
		int i = line.findLast("=");
		if (i > 0) {
			alias = line.takeMid(i + 1).trim();
			line.cutRight(1).trim();
		}
		ParserEntry e = makeEntry(line, dir);
		if (!alias.isEmpty()) e.alias = alias;
		ps.files << e;
	}
	if (!ps.files.isEmpty()) ret << ps;
	return ret;
}


PIVector<ParserSection> parseQRC(PIFile & file, const PIString & dir) {
	PIVector<ParserSection> ret;
	if (!file.isOpened()) return ret;
	PIIOTextStream ts(&file);
	ParserSection ps;
	bool is_files = false;
	while (!ts.isEnd()) {
		PIString line = ts.readLine().trim();
		if (line.isEmpty()) continue;
		if (line.startsWith("<qresource")) {
			if (!ps.files.isEmpty()) ret << ps;
			ps.name.clear();
			ps.files.clear();
			line.cutLeft(10);
			if (line.endsWith(">")) line.cutRight(1);
			line.trim();
			if (line.endsWith("/")) {
				is_files = false;
				continue;
			}
			is_files = true;
			if (line.startsWith("prefix")) {
				int i = line.find("=");
				if (i > 0) {
					line.cutLeft(i + 1).trim();
					if (line.startsWith("\"")) line.cutLeft(1);
					if (line.endsWith("\"")) line.cutRight(1);
					ps.name = line;
				}
			}
			// piCout << "section \"" << ps.name << "\"";
			continue;
		}
		if (line == "</qresource>") {
			is_files = false;
			continue;
		}
		if (!is_files) continue;
		if (!line.startsWith("<file")) continue;
		PIString alias = line.takeRange('<', '>', '\0');
		alias.cutLeft(4).trim();
		if (alias.startsWith("alias")) {
			int i = alias.find("=");
			if (i > 0) {
				alias.cutLeft(i + 1).trim();
				if (alias.startsWith("\"")) alias.cutLeft(1);
				if (alias.endsWith("\"")) alias.cutRight(1);
			}
		}
		if (line.endsWith("</file>")) line.cutRight(7).trim();
		ParserEntry e = makeEntry(line, dir);
		if (!alias.isEmpty()) e.alias = alias;
		ps.files << e;
		// piCout << "file =" << line << "alias =" << alias;
	}
	if (!ps.files.isEmpty()) ret << ps;
	return ret;
}
