Author: mturk
Date: Wed Apr 15 14:42:04 2009
New Revision: 765216

URL: http://svn.apache.org/viewvc?rev=765216&view=rev
Log:
Add util functions for converting Java String classes

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h?rev=765216&r1=765215&r2=765216&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h Wed Apr 
15 14:42:04 2009
@@ -74,6 +74,19 @@
  */
 ACR_DECLARE(char *) ACR_StrUpr(char *src);
 
+/** Convert java string to wide char string
+ * @param env Current JNI environment.
+ * @param s String to convert.
+ * @remark When done use ACR_Free to free the allocated memory.
+ */
+ACR_DECLARE(wchar_t *) ACR_GetJavaStringW(JNIEnv *env, jstring s);
+
+/** Convert java string to UTF-8 string
+ * @param env Current JNI environment.
+ * @param s String to convert.
+ * @remark When done use ACR_Free to free the allocated memory.
+ */
+ACR_DECLARE(char *) ACR_GetJavaStringU(JNIEnv *env, jstring s);
 
 #ifdef __cplusplus
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h?rev=765216&r1=765215&r2=765216&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_types.h Wed Apr 
15 14:42:04 2009
@@ -61,14 +61,6 @@
 
 #define JSSIZE_MAX  1048576
 
-typedef struct acr_str_t {
-    jsize len;
-    union {
-        char  *c;
-        jchar *w;
-    } str;
-} acr_str_t;
-
 /* Standard Java classes */
 typedef enum {
     ACR_CC_OBJECT,

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=765216&r1=765215&r2=765216&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Wed Apr 15 
14:42:04 2009
@@ -19,6 +19,7 @@
 #include "acr_string.h"
 #include "acr_memory.h"
 #include "acr_error.h"
+#include "acr_types.h"
 
 /*
  * Apache's "replacement" for the strncpy() function. We roll our
@@ -164,3 +165,26 @@
     }
     return src;
 }
+
+ACR_DECLARE(wchar_t *) ACR_GetJavaStringW(JNIEnv *_E, jstring s)
+{
+    wchar_t *rv;
+    WSTR_DECLARE(s);
+
+    WPTR_DECLARE(s);
+    rv = J2W(s);
+    J2W(s) = NULL;
+    WSTR_RELEASE(s);
+    return rv;
+}
+
+ACR_DECLARE(char *) ACR_GetJavaStringU(JNIEnv *_E, jstring s)
+{
+    char *rv = NULL;
+    CSTR_DECLARE(s);
+
+    if (J2S(s))
+        rv = strdup(J2S(s));
+    CSTR_RELEASE(s);
+    return rv;
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=765216&r1=765215&r2=765216&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Apr 15 
14:42:04 2009
@@ -130,3 +130,23 @@
     CSTR_RELEASE(s);
     return rc;
 }
+
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test009)(ACR_JNISTDARGS, jstring s)
+{
+    jint l;
+    wchar_t *str = ACR_GetJavaStringW(_E, s);
+
+    l = (jint)wcslen(str);
+    ACR_Free(_E, THROW_FMARK, str);
+    return l;
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test010)(ACR_JNISTDARGS, jstring s)
+{
+    jint l;
+    char *str = ACR_GetJavaStringU(_E, s);
+
+    l = (jint)strlen(str);
+    ACR_Free(_E, THROW_FMARK, str);
+    return l;
+}

Modified: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=765216&r1=765215&r2=765216&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
 Wed Apr 15 14:42:04 2009
@@ -48,6 +48,8 @@
     private static native int  test006(String msg);
     private static native int  test007(int d);
     private static native int  test008(String msg);
+    private static native int  test009(String msg);
+    private static native int  test010(String msg);
 
 
     protected void setUp()
@@ -154,4 +156,21 @@
         assertEquals("Result", 1, i);
     }
 
+    public void testCStrlen2()
+        throws Exception
+    {
+        int i = test009(longString);
+        assertEquals("Length", longString.length(), i);
+    }
+
+    public void testUtf8Strlen2()
+        throws Exception
+    {
+        String s = new String("\u2297\u2297");
+        int i = test010(s);
+        assertEquals("Length", 6, i);
+    }
+
+
+
 }


Reply via email to