module PixelPerfectEngine.system.dictionary;

/*
 * Copyright (C) 2015-2017, by Laszlo Szeremi under the Boost license.
 *
 * Pixel Perfect Engine, dictionary module
 */
import sdlang;
import std.stdio;
/**
 * Does a two-way coding based on an SDLang file.
 */
public class Dictionary{
	private string[int] encodeTable;
	private int[string] decodeTable;
	/// Uses the tag names as the decode table.
	public this(string filename){
		
		try{
			Tag root = parseFile(filename);
			foreach(Tag t; root.tags){
				string s = t.name();
				int i = t.expectValue!int();
				encodeTable[i] = s;
				decodeTable[s] = i;
			}
		}
		catch(ParseException e){
			//writeln(e.msg);
		}
	}
	/// Returns the first value from the encodeTable, where decodeTable[i] == input
	public string encode(int input){
		return encodeTable.get(input, null);
	}
	/// Returns the first value from the decodeTable, where encodetable[i] == input
	public int decode(string input){
		return decodeTable.get(input, -1);
	}
}