Skip to content

Instantly share code, notes, and snippets.

@cobolfoo
Created April 22, 2015 03:17
Show Gist options
  • Save cobolfoo/cf1fc63812643ca1a0b4 to your computer and use it in GitHub Desktop.
Save cobolfoo/cf1fc63812643ca1a0b4 to your computer and use it in GitHub Desktop.
Ini loader
package org.shadebob.itsatrap.tools;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.OrderedMap;
/**
* A replacement class for ini4j, I don't like having too much dependencies. This class let you handle
* ini files.
*
* @author cobolfoo
*
*/
public class IniFile {
private FileHandle file;
private OrderedMap<String, ObjectMap<String,String>> items = new OrderedMap<String, ObjectMap<String,String>>();
private String currentBlock = null;
/**
* Object constructor
* @param file File handle to use
*/
public IniFile(FileHandle file) {
this.file = file;
if (file != null) {
load();
}
}
public IniFile() {
this.file = null;
}
/**
* Clear everything
*/
public void clear() {
currentBlock = null;
items.clear();
}
/**
* Store to current file
*/
public void save() {
save(file);
}
/**
* Store to a specific handle
* @param handle file handle
*/
public void save(FileHandle handle) {
ObjectMap.Keys<String> iter = items.keys();
while(iter.hasNext) {
String key = iter.next();
handle.writeString("[" + key + "]\r\n", true);
ObjectMap.Keys<String> iter2 = items.get(key).keys();
while(iter2.hasNext) {
String subKey = iter2.next();
String value = items.get(key).get(subKey);
handle.writeString(subKey + " = " + value + "\r\n", true);
}
handle.writeString("\r\n", true);
}
}
/**
* Loading from current file
*/
public void load() {
System.out.println("Loading from file: " + file.name());
if (file.exists() == true) {
load(file.readString());
}
}
/**
* Load from string content
* @param data String content
*/
public void load(String data) {
clear();
String lines[] = data.split("\n");
for(String line: lines) {
line = line.replace("\r","");
line = line.replace(" = ", "=");
line = line.replace(" =", "=");
line = line.replace("= ", "=");
if (line.startsWith("[") && line.endsWith("]")) {
// Block detected
currentBlock = line.substring(1, line.length()-1);
createBlock(currentBlock);
} else if ((line.startsWith("#") == false) && (line.contains("="))) {
// Value
String[] values = line.split("=");
String key = values[0];
String value = "";
if (values.length == 2) {
values[1] = values[1].trim();
value = values[1];
}
//Gdx.app.log("IniFile", "[" + currentBlock +"] Key: " + values[0] +", Value: " + values[1]);
if (currentBlock == null) {
Gdx.app.error("IniFile", "Value outside of block! (Key: " + key + ", Value: " + value + ")");
System.exit(-1);
} else {
items.get(currentBlock).put(key, value);
}
}
}
}
/**
* Create a block (a section)
* @param blockName Name to use
*/
private void createBlock(String blockName) {
items.put(blockName, new ObjectMap<String,String>());
}
/**
* Set value
* @param blockName Block name
* @param key Key to use
* @param value Value to use
*/
public void set(String blockName, String key, String value) {
ObjectMap<String,String> block = items.get(blockName);
if (block == null) {
createBlock(blockName);
block = items.get(blockName);
}
block.put(key, value);
}
/**
* Retrieve a value
*
* @param block Block to use
* @param key Key to use
* @return value
*/
public String get(String block, String key, String defaultValue) {
ObjectMap<String,String> item = items.get(block);
if (item == null) {
return defaultValue;
}
String result = item.get(key);
if (result == null) {
return defaultValue;
} else {
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment