diff --git a/prism/include/PrismNative.h b/prism/include/PrismNative.h new file mode 100644 index 00000000..013d22e5 --- /dev/null +++ b/prism/include/PrismNative.h @@ -0,0 +1,53 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class prism_PrismNative */ + +#ifndef _Included_prism_PrismNative +#define _Included_prism_PrismNative +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: prism_PrismNative + * Method: PN_GetStdout + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_prism_PrismNative_PN_1GetStdout + (JNIEnv *, jclass); + +/* + * Class: prism_PrismNative + * Method: PN_OpenFile + * Signature: (Ljava/lang/String;)J + */ +JNIEXPORT jlong JNICALL Java_prism_PrismNative_PN_1OpenFile + (JNIEnv *, jclass, jstring); + +/* + * Class: prism_PrismNative + * Method: PN_PrintToFile + * Signature: (JLjava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1PrintToFile + (JNIEnv *, jclass, jlong, jstring); + +/* + * Class: prism_PrismNative + * Method: PN_FlushFile + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1FlushFile + (JNIEnv *, jclass, jlong); + +/* + * Class: prism_PrismNative + * Method: PN_CloseFile + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1CloseFile + (JNIEnv *, jclass, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/prism/include/PrismUtils.h b/prism/include/PrismUtils.h deleted file mode 100644 index b6cf2260..00000000 --- a/prism/include/PrismUtils.h +++ /dev/null @@ -1,53 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class prism_PrismUtils */ - -#ifndef _Included_prism_PrismUtils -#define _Included_prism_PrismUtils -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: prism_PrismUtils - * Method: PU_GetStdout - * Signature: ()J - */ -JNIEXPORT jlong JNICALL Java_prism_PrismUtils_PU_1GetStdout - (JNIEnv *, jclass); - -/* - * Class: prism_PrismUtils - * Method: PU_OpenFile - * Signature: (Ljava/lang/String;)J - */ -JNIEXPORT jlong JNICALL Java_prism_PrismUtils_PU_1OpenFile - (JNIEnv *, jclass, jstring); - -/* - * Class: prism_PrismUtils - * Method: PU_PrintToFile - * Signature: (JLjava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1PrintToFile - (JNIEnv *, jclass, jlong, jstring); - -/* - * Class: prism_PrismUtils - * Method: PU_FlushFile - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1FlushFile - (JNIEnv *, jclass, jlong); - -/* - * Class: prism_PrismUtils - * Method: PU_CloseFile - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1CloseFile - (JNIEnv *, jclass, jlong); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/prism/src/prism/Makefile b/prism/src/prism/Makefile index 5c7af3ac..1b30d2f4 100644 --- a/prism/src/prism/Makefile +++ b/prism/src/prism/Makefile @@ -30,7 +30,7 @@ PRISM_CLASSPATH = "$(THIS_DIR)/$(PRISM_DIR_REL)/$(CLASSES_DIR)$(CLASSPATHSEP)$(T default: all -all: checks $(CLASS_FILES) $(PRISM_DIR_REL)/$(INCLUDE_DIR)/PrismUtils.h $(PRISM_DIR_REL)/$(LIB_DIR)/$(LIBPREFIX)prism$(LIBSUFFIX) +all: checks $(CLASS_FILES) $(PRISM_DIR_REL)/$(INCLUDE_DIR)/PrismNative.h $(PRISM_DIR_REL)/$(LIB_DIR)/$(LIBPREFIX)prism$(LIBSUFFIX) # Try and prevent accidental makes (i.e. called manually, not from top-level Makefile) checks: @@ -41,8 +41,8 @@ checks: $(PRISM_DIR_REL)/$(CLASSES_DIR)/$(THIS_DIR)/%.class: %.java (cd ..; $(JAVAC) -sourcepath $(THIS_DIR)/$(PRISM_DIR_REL)/$(SRC_DIR) -classpath $(PRISM_CLASSPATH) -d $(THIS_DIR)/$(PRISM_DIR_REL)/$(CLASSES_DIR) $(THIS_DIR)/$<) -$(PRISM_DIR_REL)/$(INCLUDE_DIR)/PrismUtils.h: $(PRISM_DIR_REL)/$(CLASSES_DIR)/$(THIS_DIR)/PrismUtils.class - ($(JAVAH) -classpath $(PRISM_DIR_REL)/$(CLASSES_DIR) -jni -o $@ $(THIS_DIR).PrismUtils; touch $@) +$(PRISM_DIR_REL)/$(INCLUDE_DIR)/PrismNative.h: $(PRISM_DIR_REL)/$(CLASSES_DIR)/$(THIS_DIR)/PrismNative.class + ($(JAVAH) -classpath $(PRISM_DIR_REL)/$(CLASSES_DIR) -jni -o $@ $(THIS_DIR).PrismNative; touch $@) $(PRISM_DIR_REL)/$(LIB_DIR)/$(LIBPREFIX)prism$(LIBSUFFIX): $(O_FILES) $(LD) $(SHARED) $(LDFLAGS) -o $@ $(O_FILES) $(LIBRARIES) diff --git a/prism/src/prism/PrismFileLog.java b/prism/src/prism/PrismFileLog.java index 19427b6b..a1914efd 100644 --- a/prism/src/prism/PrismFileLog.java +++ b/prism/src/prism/PrismFileLog.java @@ -47,11 +47,11 @@ public class PrismFileLog implements PrismLog { filename = s; if (s.equals("stdout")) { - fp = PrismUtils.PU_GetStdout(); + fp = PrismNative.PN_GetStdout(); stdout = true; } else { - fp = PrismUtils.PU_OpenFile(s); + fp = PrismNative.PN_OpenFile(s); stdout = false; } } @@ -153,17 +153,17 @@ public class PrismFileLog implements PrismLog public void flush() { - PrismUtils.PU_FlushFile(fp); + PrismNative.PN_FlushFile(fp); } public void close() { - if (!stdout) PrismUtils.PU_CloseFile(fp); + if (!stdout) PrismNative.PN_CloseFile(fp); } private void printToLog(String s) { - PrismUtils.PU_PrintToFile(fp, s); + PrismNative.PN_PrintToFile(fp, s); } } diff --git a/prism/src/prism/PrismUtils.cc b/prism/src/prism/PrismNative.cc similarity index 78% rename from prism/src/prism/PrismUtils.cc rename to prism/src/prism/PrismNative.cc index 0a2ee12a..d6e741e4 100644 --- a/prism/src/prism/PrismUtils.cc +++ b/prism/src/prism/PrismNative.cc @@ -2,7 +2,7 @@ // // Copyright (c) 2002- // Authors: -// * Dave Parker (University of Oxford, formerly University of Birmingham) +// * Dave Parker (University of Oxford) // //------------------------------------------------------------------------------ // @@ -24,7 +24,7 @@ // //============================================================================== -#include "PrismUtils.h" +#include "PrismNative.h" #include #include #include @@ -32,7 +32,7 @@ //------------------------------------------------------------------------------ -JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismUtils_PU_1GetStdout(JNIEnv *env, jclass cls) +JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismNative_PN_1GetStdout(JNIEnv *env, jclass cls) { return ptr_to_jlong(stdout); } @@ -40,7 +40,7 @@ JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismUtils_PU_1GetStdout(JNIEn //------------------------------------------------------------------------------ -JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismUtils_PU_1OpenFile(JNIEnv *env, jclass cls, jstring filename) +JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismNative_PN_1OpenFile(JNIEnv *env, jclass cls, jstring filename) { const char *str = env->GetStringUTFChars(filename, 0); FILE *fp = fopen(str, "w"); @@ -51,7 +51,7 @@ JNIEXPORT jlong __jlongpointer JNICALL Java_prism_PrismUtils_PU_1OpenFile(JNIEnv //------------------------------------------------------------------------------ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1PrintToFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp, jstring s) +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1PrintToFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp, jstring s) { const char *str = env->GetStringUTFChars(s, 0); // note: use fwrite not fprintf here because there is no formatting to do @@ -65,7 +65,7 @@ JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1PrintToFile(JNIEnv *env, jclass //------------------------------------------------------------------------------ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1FlushFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp) +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1FlushFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp) { fflush(jlong_to_FILE(fp)); } @@ -73,7 +73,7 @@ JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1FlushFile(JNIEnv *env, jclass c //------------------------------------------------------------------------------ -JNIEXPORT void JNICALL Java_prism_PrismUtils_PU_1CloseFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp) +JNIEXPORT void JNICALL Java_prism_PrismNative_PN_1CloseFile(JNIEnv *env, jclass cls, jlong __jlongpointer fp) { fclose(jlong_to_FILE(fp)); } diff --git a/prism/src/prism/PrismNative.java b/prism/src/prism/PrismNative.java new file mode 100644 index 00000000..4dc1f565 --- /dev/null +++ b/prism/src/prism/PrismNative.java @@ -0,0 +1,53 @@ +//============================================================================== +// +// Copyright (c) 2002- +// Authors: +// * Dave Parker (University of Oxford) +// +//------------------------------------------------------------------------------ +// +// This file is part of PRISM. +// +// PRISM is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// PRISM is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with PRISM; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +//============================================================================== + +package prism; + +/** + * A few utility methods, relying on native methods in the "prism" shared library + */ +public class PrismNative +{ + // Load "prism" shared library + static + { + try { + System.loadLibrary("prism"); + } + catch (UnsatisfiedLinkError e) { + System.out.println(e); + System.exit(1); + } + } + + public static native long PN_GetStdout(); + public static native long PN_OpenFile(String filename); + public static native void PN_PrintToFile(long fp, String s); + public static native void PN_FlushFile(long fp); + public static native void PN_CloseFile(long fp); +} + +//------------------------------------------------------------------------------ diff --git a/prism/src/prism/PrismUtils.java b/prism/src/prism/PrismUtils.java index 821d7a94..631d71a3 100644 --- a/prism/src/prism/PrismUtils.java +++ b/prism/src/prism/PrismUtils.java @@ -26,136 +26,114 @@ package prism; +import java.text.DecimalFormat; import java.util.Formatter; +/** + * Various general-purpose utility methods in Java + */ public class PrismUtils { - // load jni stuff from shared library - static - { - try { - System.loadLibrary("prism"); - } - catch (UnsatisfiedLinkError e) { - System.out.println(e); - System.exit(1); - } - } - -//------------------------------------------------------------------------------ - - // small utility methods implemented through jni - - public static native long PU_GetStdout(); - public static native long PU_OpenFile(String filename); - public static native void PU_PrintToFile(long fp, String s); - public static native void PU_FlushFile(long fp); - public static native void PU_CloseFile(long fp); - -//------------------------------------------------------------------------------ - - // Small utility methods in java - - // Logarithm of x to base b - + /** + * Compute logarithm of x to base b. + */ public static double log(double x, double b) { // If base is <=0 or ==1 (or +Inf/NaN), then result is NaN - if (b <= 0 || b == 1 || (Double.isInfinite(b)) || Double.isNaN(b)) return Double.NaN; - + if (b <= 0 || b == 1 || (Double.isInfinite(b)) || Double.isNaN(b)) + return Double.NaN; + // Otherwise, log_b (x) is log(x) / log(b) return Math.log(x) / Math.log(b); } - - // Logarithm of x to base 2 - + + /** + * Compute logarithm of x to base 2. + */ public static double log2(double x) { return Math.log(x) / Math.log(2); } - + + /** + * Format a large integer, represented by a double, as a string. + */ public static String bigIntToString(double d) { if (d <= Long.MAX_VALUE) { return "" + Math.round(d); - } - else { + } else { return "" + d; } } - - public static String commaSeparateBigInt(String s) - { - int l = s.length(); - if (l <= 3) { - return s; - } - else { - return commaSeparateBigInt(s.substring(0,l-3)) + "," + s.substring(l-3); - } - } - - public static String bigIntToHTML(double d) - { - int n; - String s; - if (d < 10000000) { - s = PrismUtils.bigIntToString(d); - s = PrismUtils.commaSeparateBigInt(s); - } - else { - n = (int)Math.floor((Math.log(d)/Math.log(10))); - d = (Math.round(100.0*(d/Math.pow(10, n))))/100.0; - s = d + "x10" + n + ""; - } - - return s; - } - + /** + * Modify a filename f, appending a counter i just before the filetype extension. + */ public static String addCounterSuffixToFilename(String f, int i) { int j = f.lastIndexOf("."); if (j != -1) { - return f.substring(0, j)+i+f.substring(j); - } - else { - return f+i; + return f.substring(0, j) + i + f.substring(j); + } else { + return f + i; } } + + /** + * Format a fraction as a percentage to 1 decimal place. + */ + public static String formatPercent1dp(double frac) + { + return formatterPercent1dp.format(frac); + } + + private static DecimalFormat formatterPercent1dp = new DecimalFormat("#0.0%"); + + /** + * Format a double to 2 decimal places. + */ + public static String formatDouble2dp(double d) + { + return formatterDouble2dp.format(d); + } + private static DecimalFormat formatterDouble2dp = new DecimalFormat("#0.00 secs"); + + /** + * Format a double, using PRISM settings. + */ public static String formatDouble(PrismSettings settings, double d) { return formatDouble(settings, new Double(d)); } - + public static String formatDouble(PrismSettings settings, Double d) { Formatter formatter = new Formatter(); - - formatter.format("%.6g",d); // [the way to format scientific notation with 6 being the precision] - - + + formatter.format("%.6g", d); // [the way to format scientific notation with 6 being the precision] + String res = formatter.toString().trim(); - + int trailingZeroEnd = res.lastIndexOf('e'); if (trailingZeroEnd == -1) trailingZeroEnd = res.length(); - - int x = trailingZeroEnd -1; - - while (x > 0 && res.charAt(x) == '0') + + int x = trailingZeroEnd - 1; + + while (x > 0 && res.charAt(x) == '0') x--; - + if (res.charAt(x) == '.') x++; - - res = res.substring(0,x + 1) + res.substring(trailingZeroEnd, res.length()); - + + res = res.substring(0, x + 1) + res.substring(trailingZeroEnd, res.length()); + //formatter.format("%.6f",d); //(just decimals) //formatter.format("%1$.2e", d); // [the way to format scientific notation with 6 being the precision] - - return res; + + return res; } }