summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile12
-rw-r--r--src/hastabel2idp/Main.java87
-rw-r--r--src/hastabel2idp/Parameters.java196
3 files changed, 292 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 3456fd2..7403528 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@ BIN_DIR ?= ${CURDIR}/bin/
LIB_DIR ?= ${CURDIR}/lib/
TARGET ?= hastabel2idp.jar
+RUN_SCRIPT ?= hastabel2idp.sh
INSTALL_DIR ?= $(LIB_DIR)
#### Where to get the missing Jar files.
@@ -50,12 +51,12 @@ CLASSPATH = "$(SRC_DIR):$(BIN_DIR):$(ANTLR_JAR):$(HASTABEL_JAR)"
## Makefile Magic ##############################################################
JAVA_SOURCES = \
- $(wildcard $(SRC_DIR)/hastabel/*.java) \
- $(wildcard $(SRC_DIR)/hastabel/*/*.java)
+ $(wildcard $(SRC_DIR)/hastabel2idp/*.java) \
+ $(wildcard $(SRC_DIR)/hastabel2idp/*/*.java)
CLASSES = $(patsubst $(SRC_DIR)/%,$(BIN_DIR)/%, $(JAVA_SOURCES:.java=.class))
## Makefile Rules ##############################################################
-$(TARGET): $(ANTLR_JAR) $(HASTABEL_JAR) $(JAVA_SOURCES) $(CLASSES)
+$(TARGET): $(ANTLR_JAR) $(HASTABEL_JAR) $(JAVA_SOURCES) $(CLASSES) $(RUN_SCRIPT)
rm -f $(TARGET) $(INSTALL_DIR)/$@
$(JAR) cf $@ -C $(BIN_DIR) .
cp $@ $(INSTALL_DIR)/$@
@@ -78,6 +79,11 @@ $(LIB_DIR):
$(BIN_DIR):
mkdir -p $@
+$(RUN_SCRIPT): Makefile
+ echo "#!/bin/sh" > $@
+ echo "$(JAVA) -cp \"$(CLASSPATH)\" hastabel2idp.Main $$*" >> $@
+ chmod +x $@
+
##### For my private use...
publish: $(TARGET)
scp $< dreamhost:~/noot-noot/tabellion/jar/
diff --git a/src/hastabel2idp/Main.java b/src/hastabel2idp/Main.java
new file mode 100644
index 0000000..5013c0c
--- /dev/null
+++ b/src/hastabel2idp/Main.java
@@ -0,0 +1,87 @@
+package hastabel2idp;
+
+import hastabel.World;
+
+import java.io.IOException;
+
+public class Main
+{
+ public static void main (String... args)
+ {
+ final Parameters params;
+ final World world;
+
+ params = new Parameters(args);
+
+ if (!params.are_valid())
+ {
+ return;
+ }
+
+ world = new World();
+
+ for (final String level_file: params.get_level_files())
+ {
+ load_file(world, level_file);
+
+ if (!world.is_valid())
+ {
+ return;
+ }
+ }
+
+ for (final String model_file: params.get_model_files())
+ {
+ load_file(world, model_file);
+
+ if (!world.is_valid())
+ {
+ return;
+ }
+ }
+
+ world.ensure_first_order();
+
+ if (!world.is_valid())
+ {
+ return;
+ }
+
+ try
+ {
+ world.load_property(params.get_property_file());
+ }
+ catch (final IOException ioe)
+ {
+ System.err.println
+ (
+ "[E] IOException when loading \""
+ + params.get_property_file()
+ + "\":\n"
+ + ioe.getMessage()
+ );
+
+ world.invalidate();
+ }
+ }
+
+ private static void load_file (final World world, final String filename)
+ {
+ try
+ {
+ world.load(filename);
+ }
+ catch (final IOException ioe)
+ {
+ System.err.println
+ (
+ "[E] IOException when loading \""
+ + filename
+ + "\":\n"
+ + ioe.getMessage()
+ );
+
+ world.invalidate();
+ }
+ }
+}
diff --git a/src/hastabel2idp/Parameters.java b/src/hastabel2idp/Parameters.java
new file mode 100644
index 0000000..a46e820
--- /dev/null
+++ b/src/hastabel2idp/Parameters.java
@@ -0,0 +1,196 @@
+package hastabel2idp;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class Parameters
+{
+ private final List<String> level_files;
+ private final List<String> model_files;
+ private final String property_file;
+ private final String output_file;
+ private final boolean be_verbose;
+
+ private final boolean are_valid;
+
+ public static void print_usage ()
+ {
+ System.out.println
+ (
+ "HaStABeL to IDP\n"
+ + "USAGE:\n"
+ + "\thastabel2idp.sh <OUTPUT_FILE> <FILES|OPTIONS>+\n"
+ + "PARAMETERS:\n"
+ + "\t- <OUTPUT_FILE>\tFile to write the solutions in.\n"
+ + "\t- <FILES>\tList of files to be loaded.\n"
+ + "OPTIONS:\n"
+ + "\t- -v|--verbose\tPrint informative messages to STDOUT.\n"
+ + "NOTES:\n"
+ + "\t- Exactly one property file must be in <FILES>.\n"
+ + "\t- Property files have a \".pro\" extension.\n"
+ + "\t- Model files have a \".mod\" extension.\n"
+ + "\t- Level files have a \".lvl\" extension.\n"
+ + "\t- The files may be given in any order."
+ );
+ }
+
+ public Parameters (final String... args)
+ {
+ boolean has_pro_file, has_error, should_be_verbose;
+ String prop_file;
+
+ level_files = new ArrayList<String>();
+ model_files = new ArrayList<String>();
+
+ should_be_verbose = false;
+
+ if (args.length < 2)
+ {
+ print_usage();
+
+ property_file = new String();
+ output_file = new String();
+
+ are_valid = false;
+ be_verbose = false;
+
+ return;
+ }
+
+ has_pro_file = false;
+ has_error = false;
+
+ output_file = args[0];
+
+ if
+ (
+ (output_file.equals("-v") || output_file.equals("--verbose"))
+ /* || ... */
+ )
+ {
+ print_usage();
+
+ System.err.println
+ (
+ "[F] An option was found in lieu of the output file."
+ );
+
+ System.exit(-1);
+ }
+
+ if
+ (
+ output_file.endsWith(".lvl")
+ || output_file.endsWith(".mod")
+ || output_file.endsWith(".pro")
+ )
+ {
+ print_usage();
+
+ System.err.println
+ (
+ "[F] The output file has an extension that could be used in an"
+ + " input file. It is most likely that you did not indicate an"
+ + " output file, meaning that one of the input files was about to"
+ + " be written over. So likely, in fact, that we'll abort here. The"
+ + " output file you indicated was \""
+ + output_file
+ + "\"."
+ );
+
+ System.exit(-1);
+ }
+
+ prop_file = new String();
+
+ for (int i = 1; i < args.length; ++i)
+ {
+ if (args[i].endsWith(".lvl"))
+ {
+ level_files.add(args[i]);
+ }
+ else if (args[i].endsWith(".mod"))
+ {
+ model_files.add(args[i]);
+ }
+ else if (args[i].endsWith(".pro"))
+ {
+ if (has_pro_file)
+ {
+ System.err.println
+ (
+ "[E] Both files \""
+ + prop_file
+ + "\" and \"."
+ + args[i]
+ + "\" contain a property. Only one can be used at a time."
+ );
+
+ has_error = true;
+ }
+ else
+ {
+ has_pro_file = true;
+ prop_file = args[i];
+ }
+ }
+ else if (args[i].equals("-v") || args[i].equals("--verbose"))
+ {
+ should_be_verbose = true;
+ }
+ else
+ {
+ System.err.println
+ (
+ "[E] Unknown file type \""
+ + args[i]
+ + "\"."
+ );
+
+ has_error = true;
+ }
+ }
+
+ property_file = prop_file;
+
+ if (!has_pro_file)
+ {
+ System.err.println("[E] There was no property file.");
+
+ has_error = true;
+ }
+
+ be_verbose = should_be_verbose;
+ are_valid = !has_error;
+ }
+
+ public List<String> get_level_files ()
+ {
+ return level_files;
+ }
+
+ public List<String> get_model_files ()
+ {
+ return model_files;
+ }
+
+ public String get_property_file ()
+ {
+ return property_file;
+ }
+
+ public String get_output_file ()
+ {
+ return output_file;
+ }
+
+ public boolean be_verbose ()
+ {
+ return be_verbose;
+ }
+
+ public boolean are_valid ()
+ {
+ return are_valid;
+ }
+}