[1727] | 1 | /* |
---|
| 2 | * ====================================================================== |
---|
| 3 | * AUTHOR: Ben Rafferty, Purdue University |
---|
[3177] | 4 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[1727] | 5 | * |
---|
| 6 | * See the file "license.terms" for information on usage and |
---|
| 7 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 8 | * ====================================================================== |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /* |
---|
| 12 | * This file defines the native functions which are called by the java Units |
---|
| 13 | * class methods, and in turn call the corresponding rappture RpUnits methods. |
---|
| 14 | */ |
---|
| 15 | |
---|
[1722] | 16 | #include "jRpUnits.h" |
---|
| 17 | #include "rappture.h" |
---|
| 18 | |
---|
[2076] | 19 | // convertString |
---|
[1722] | 20 | JNIEXPORT jstring JNICALL Java_rappture_Units_jRpUnitsConvert |
---|
| 21 | (JNIEnv *env, jclass cls, jstring javaFromVal, jstring javaTo, jboolean units){ |
---|
| 22 | const char* nativeFromVal = env->GetStringUTFChars(javaFromVal, 0); |
---|
| 23 | const char* nativeTo = env->GetStringUTFChars(javaTo, 0); |
---|
[1753] | 24 | int err; |
---|
| 25 | jclass ex; |
---|
| 26 | std::string errorMsg; |
---|
| 27 | |
---|
| 28 | // perform conversion |
---|
| 29 | std::string retStr = RpUnits::convert(nativeFromVal, nativeTo, |
---|
| 30 | (int)units, &err); |
---|
| 31 | |
---|
| 32 | // raise a runtime exception on error |
---|
| 33 | if (err){ |
---|
| 34 | ex = env->FindClass("java/lang/RuntimeException"); |
---|
| 35 | if (ex){ |
---|
[2076] | 36 | errorMsg = "Cannot convert "; |
---|
[1753] | 37 | errorMsg += nativeFromVal; |
---|
| 38 | errorMsg += " to "; |
---|
| 39 | errorMsg += nativeTo; |
---|
| 40 | env->ThrowNew(ex, errorMsg.c_str()); |
---|
| 41 | } |
---|
| 42 | env->DeleteLocalRef(ex); |
---|
| 43 | } |
---|
| 44 | |
---|
[1722] | 45 | env->ReleaseStringUTFChars(javaFromVal, nativeFromVal); |
---|
| 46 | env->ReleaseStringUTFChars(javaTo, nativeTo); |
---|
[1753] | 47 | |
---|
| 48 | // create new java string and return |
---|
[1722] | 49 | return (env->NewStringUTF(retStr.c_str())); |
---|
| 50 | } |
---|
| 51 | |
---|