Mega Code Archive

 
Categories / Java / Development Class
 

Using JNIEnv

/**    @version 1.10 1997-07-01    @author Cay Horstmann */ #include "Printf3.h" #include <string.h> #include <stdlib.h> #include <float.h> /**    @param format a string containing a printf format specifier    (such as "%8.2f"). Substrings "%%" are skipped.    @return a pointer to the format specifier (skipping the '%')    or NULL if there wasn't a unique format specifier */ char* find_format(const char format[]) {      char* p;    char* q;    p = strchr(format, '%');    while (p != NULL && *(p + 1) == '%') /* skip %% */       p = strchr(p + 2, '%');    if (p == NULL) return NULL;    /* now check that % is unique */    p++;    q = strchr(p, '%');    while (q != NULL && *(q + 1) == '%') /* skip %% */       q = strchr(q + 2, '%');    if (q != NULL) return NULL; /* % not unique */    q = p + strspn(p, " -0+#"); /* skip past flags */    q += strspn(q, "0123456789"); /* skip past field width */    if (*q == '.') { q++; q += strspn(q, "0123456789"); }       /* skip past precision */    if (strchr("eEfFgG", *q) == NULL) return NULL;       /* not a floating-point format */    return p; } JNIEXPORT void JNICALL Java_Printf3_fprint(JNIEnv* env, jclass cl,     jobject out, jstring format, jdouble x) {      const char* cformat;    char* fmt;    jstring str;    jclass class_PrintWriter;    jmethodID id_print;    cformat = (*env)->GetStringUTFChars(env, format, NULL);    fmt = find_format(cformat);    if (fmt == NULL)       str = format;    else    {         char* cstr;       int width = atoi(fmt);       if (width == 0) width = DBL_DIG + 10;       cstr = (char*) malloc(strlen(cformat) + width);       sprintf(cstr, cformat, x);       str = (*env)->NewStringUTF(env, cstr);       free(cstr);    }    (*env)->ReleaseStringUTFChars(env, format, cformat);    /* now call ps.print(str) */    /* get the class */    class_PrintWriter = (*env)->GetObjectClass(env, out);    /* get the method ID */    id_print = (*env)->GetMethodID(env, class_PrintWriter, "print", "(Ljava/lang/String;)V");    /* call the method */    (*env)->CallVoidMethod(env, out, id_print, str); } /*    This program is a part of the companion code for Core Java 8th ed.    (http://horstmann.com/corejava)    This program 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 3 of the License, or    (at your option) any later version.    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. */ import java.io.*; /**  * @version 1.10 1997-07-01  * @author Cay Horstmann  */ class Printf3 {    public static native void fprint(PrintWriter out, String format, double x);    static    {       System.loadLibrary("Printf3");    } } /*    This program is a part of the companion code for Core Java 8th ed.    (http://horstmann.com/corejava)    This program 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 3 of the License, or    (at your option) any later version.    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. */ import java.io.*; /**  * @version 1.10 1997-07-01  * @author Cay Horstmann  */ class Printf3Test {    public static void main(String[] args)    {       double price = 44.95;       double tax = 7.75;       double amountDue = price * (1 + tax / 100);       PrintWriter out = new PrintWriter(System.out);       Printf3.fprint(out, "Amount due = %8.2f\n", amountDue);       out.flush();    } }        Printf3Test.zip( 2 k)