Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.app;

import java.util.HashMap;

import com.badlogic.gdx.files.FileHandle;
import com.gebauz.bauzoid.parser.Preprocessor;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;

public class Settings
{
        // Constants========================================================================================
       
        public static final String LOG_TAG = "Settings";

        // Embedded Types===================================================================================

        // Members==========================================================================================
       
        protected HashMap<String, String> mSettingsMap = new HashMap<String, String>();

        // Methods==========================================================================================
       
        public void loadSettings(FileHandle file)
        {
                String fileContents = file.readString();
               
                Tokenizer t = new Tokenizer(Preprocessor.stripComments(fileContents, "#", "", ""));
               
                try
                {
                        while (!t.checkNoMoreTokens())
                        {
                                String key = t.readIdentifier();
                                t.readToken("=");
                                String value = t.readUntilNewLine();
                               
                                setSetting(key, value);
                        }
                }
                catch (ScanException e)
                {
                        e.log(LOG_TAG);                
                }
        }
       
        /** Can be Overridden by derived classes in order to store values in variables to minimize HashMap access. */
        public void handleSetting(String key, String value)
        {
        }

        // Getters/Setters==================================================================================
       
        /** Get Settings value. */
        public final String getSetting(String key)
        {
                if (!mSettingsMap.containsKey(key))
                        return "";
               
                return mSettingsMap.get(key);
        }
       
        /** Set Settings value. */
        public final void setSetting(String key, String value)
        {
                mSettingsMap.put(key, value);
        }
}