Home | History | Annotate | Download | only in lwjgl
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.backends.lwjgl;
     18 
     19 import java.io.BufferedInputStream;
     20 import java.io.BufferedOutputStream;
     21 import java.io.File;
     22 import java.io.InputStream;
     23 import java.io.OutputStream;
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 import java.util.Map.Entry;
     27 import java.util.Properties;
     28 
     29 import com.badlogic.gdx.Files.FileType;
     30 import com.badlogic.gdx.Preferences;
     31 import com.badlogic.gdx.files.FileHandle;
     32 import com.badlogic.gdx.utils.GdxRuntimeException;
     33 import com.badlogic.gdx.utils.StreamUtils;
     34 
     35 public class LwjglPreferences implements Preferences {
     36 	private final String name;
     37 	private final Properties properties = new Properties();
     38 	private final FileHandle file;
     39 
     40 	public LwjglPreferences (String name, String directory) {
     41 		this(new LwjglFileHandle(new File(directory, name), FileType.External));
     42 	}
     43 
     44 	public LwjglPreferences (FileHandle file) {
     45 		this.name = file.name();
     46 		this.file = file;
     47 		if (!file.exists()) return;
     48 		InputStream in = null;
     49 		try {
     50 			in = new BufferedInputStream(file.read());
     51 			properties.loadFromXML(in);
     52 		} catch (Throwable t) {
     53 			t.printStackTrace();
     54 		} finally {
     55 			StreamUtils.closeQuietly(in);
     56 		}
     57 	}
     58 
     59 	@Override
     60 	public Preferences putBoolean (String key, boolean val) {
     61 		properties.put(key, Boolean.toString(val));
     62 		return this;
     63 	}
     64 
     65 	@Override
     66 	public Preferences putInteger (String key, int val) {
     67 		properties.put(key, Integer.toString(val));
     68 		return this;
     69 	}
     70 
     71 	@Override
     72 	public Preferences putLong (String key, long val) {
     73 		properties.put(key, Long.toString(val));
     74 		return this;
     75 	}
     76 
     77 	@Override
     78 	public Preferences putFloat (String key, float val) {
     79 		properties.put(key, Float.toString(val));
     80 		return this;
     81 	}
     82 
     83 	@Override
     84 	public Preferences putString (String key, String val) {
     85 		properties.put(key, val);
     86 		return this;
     87 	}
     88 
     89 	@Override
     90 	public Preferences put (Map<String, ?> vals) {
     91 		for (Entry<String, ?> val : vals.entrySet()) {
     92 			if (val.getValue() instanceof Boolean) putBoolean(val.getKey(), (Boolean)val.getValue());
     93 			if (val.getValue() instanceof Integer) putInteger(val.getKey(), (Integer)val.getValue());
     94 			if (val.getValue() instanceof Long) putLong(val.getKey(), (Long)val.getValue());
     95 			if (val.getValue() instanceof String) putString(val.getKey(), (String)val.getValue());
     96 			if (val.getValue() instanceof Float) putFloat(val.getKey(), (Float)val.getValue());
     97 		}
     98 		return this;
     99 	}
    100 
    101 	@Override
    102 	public boolean getBoolean (String key) {
    103 		return getBoolean(key, false);
    104 	}
    105 
    106 	@Override
    107 	public int getInteger (String key) {
    108 		return getInteger(key, 0);
    109 	}
    110 
    111 	@Override
    112 	public long getLong (String key) {
    113 		return getLong(key, 0);
    114 	}
    115 
    116 	@Override
    117 	public float getFloat (String key) {
    118 		return getFloat(key, 0);
    119 	}
    120 
    121 	@Override
    122 	public String getString (String key) {
    123 		return getString(key, "");
    124 	}
    125 
    126 	@Override
    127 	public boolean getBoolean (String key, boolean defValue) {
    128 		return Boolean.parseBoolean(properties.getProperty(key, Boolean.toString(defValue)));
    129 	}
    130 
    131 	@Override
    132 	public int getInteger (String key, int defValue) {
    133 		return Integer.parseInt(properties.getProperty(key, Integer.toString(defValue)));
    134 	}
    135 
    136 	@Override
    137 	public long getLong (String key, long defValue) {
    138 		return Long.parseLong(properties.getProperty(key, Long.toString(defValue)));
    139 	}
    140 
    141 	@Override
    142 	public float getFloat (String key, float defValue) {
    143 		return Float.parseFloat(properties.getProperty(key, Float.toString(defValue)));
    144 	}
    145 
    146 	@Override
    147 	public String getString (String key, String defValue) {
    148 		return properties.getProperty(key, defValue);
    149 	}
    150 
    151 	@Override
    152 	public Map<String, ?> get () {
    153 		Map<String, Object> map = new HashMap<String, Object>();
    154 		for (Entry<Object, Object> val : properties.entrySet()) {
    155 			if (val.getValue() instanceof Boolean)
    156 				map.put((String)val.getKey(), (Boolean)Boolean.parseBoolean((String)val.getValue()));
    157 			if (val.getValue() instanceof Integer) map.put((String)val.getKey(), (Integer)Integer.parseInt((String)val.getValue()));
    158 			if (val.getValue() instanceof Long) map.put((String)val.getKey(), (Long)Long.parseLong((String)val.getValue()));
    159 			if (val.getValue() instanceof String) map.put((String)val.getKey(), (String)val.getValue());
    160 			if (val.getValue() instanceof Float) map.put((String)val.getKey(), (Float)Float.parseFloat((String)val.getValue()));
    161 		}
    162 
    163 		return map;
    164 	}
    165 
    166 	@Override
    167 	public boolean contains (String key) {
    168 		return properties.containsKey(key);
    169 	}
    170 
    171 	@Override
    172 	public void clear () {
    173 		properties.clear();
    174 	}
    175 
    176 	@Override
    177 	public void flush () {
    178 		OutputStream out = null;
    179 		try {
    180 			out = new BufferedOutputStream(file.write(false));
    181 			properties.storeToXML(out, null);
    182 		} catch (Exception ex) {
    183 			throw new GdxRuntimeException("Error writing preferences: " + file, ex);
    184 		} finally {
    185 			StreamUtils.closeQuietly(out);
    186 		}
    187 	}
    188 
    189 	@Override
    190 	public void remove (String key) {
    191 		properties.remove(key);
    192 	}
    193 }