> sorry for not replying earlier ...
> here's what we have currently:
> http://www.gknw.net/mirror/curl/curl_java/
> please see the svn stuff, and work from there... (viewvc should be able
> to generate tarballs, but you should also be able to checkout anonymously.
> 
> Gün.

Hi Gün
My changes are in attached diff. I have implemented curl_getinfo bindings with 
respect to java types and template for CurlGlue class generation. Please look 
at it and let me know what do you think. I suggest to discuss future 
development in direct communication and post just release notifications in this 
list.

Evgeny
Index: curljava.c
===================================================================
--- curljava.c	(revision 45)
+++ curljava.c	(working copy)
@@ -64,8 +64,19 @@
 #define CurlGlue_getinfo	Java_net_haxx_curl_CurlGlue_getinfo
 #define CurlGlue_perform	Java_net_haxx_curl_CurlGlue_jni_1perform
 
+#define CurlGlue_getLongInfo						\
+        Java_net_haxx_curl_CurlGlue_jni_1getLongInfo
 
+#define CurlGlue_getDoubleInfo						\
+        Java_net_haxx_curl_CurlGlue_jni_1getDoubleInfo
 
+#define CurlGlue_getStringInfo						\
+        Java_net_haxx_curl_CurlGlue_jni_1getStringInfo
+
+#define CurlGlue_getStringListInfo						\
+        Java_net_haxx_curl_CurlGlue_jni_1getStringListInfo
+
+
 /*
  * CurlGlue.version()
  */
@@ -168,7 +179,7 @@
   return (jint) curl_easy_setopt(handle, (CURLoption) option, value);
 }
 
-static int curljava_write_callback(void *ptr,
+static size_t curljava_write_callback(void *ptr,
                                    size_t size,
                                    size_t nmemb,
                                    FILE  *stream)
@@ -265,7 +276,7 @@
 }
 
 
-static int curljava_read_callback(void *ptr,
+static size_t curljava_read_callback(void *ptr,
                                   size_t size,
                                   size_t nmemb,
                                   FILE  *stream)
@@ -380,3 +391,97 @@
   curl->read.java = java;
   return (jint)curl_easy_perform(curl->libcurl);
 }
+
+JNIEXPORT jlong JNICALL CurlGlue_getLongInfo
+(JNIEnv *java, jobject myself, jint jcurl, jint option)
+{
+	if (!(option & CURLINFO_LONG)) {
+		except(java, "IllegalArgumentException", "Invalid option specified for getLongInfo()");
+		return 0;
+	}
+
+	struct curljava *curl = (struct curljava *) jcurl;
+	long value;
+	curl_easy_getinfo(curl->libcurl, (CURLINFO) option, &value);
+	return value;
+}
+
+JNIEXPORT jdouble JNICALL CurlGlue_getDoubleInfo
+(JNIEnv *java, jobject myself, jint jcurl, jint option)
+{
+	if (!(option & CURLINFO_DOUBLE)) {
+		except(java, "IllegalArgumentException", "Invalid option specified for getDoubleInfo()");
+		return 0;
+	}
+
+	struct curljava *curl = (struct curljava *) jcurl;
+	double value;
+	curl_easy_getinfo(curl->libcurl, (CURLINFO) option, &value);
+	return value;
+}
+
+JNIEXPORT jstring JNICALL CurlGlue_getStringInfo
+(JNIEnv *java, jobject myself, jint jcurl, jint option)
+{
+	jstring jstr;
+	if (!(option & CURLINFO_STRING)) {
+		except(java, "IllegalArgumentException", "Invalid option specified for getStringInfo()");
+		return 0;
+	}
+	struct curljava *curl = (struct curljava *) jcurl;
+	char *value = NULL;
+	curl_easy_getinfo(curl->libcurl, (CURLINFO) option, &value);
+	jstr = (*java)->NewStringUTF(java,value);
+
+	return jstr;
+}
+
+JNIEXPORT jobjectArray JNICALL CurlGlue_getStringListInfo
+(JNIEnv *java, jobject myself, jint jcurl, jint option)
+{
+	jobjectArray jstrings;
+	if (!(option & CURLINFO_SLIST)) {
+		except(java, "IllegalArgumentException", "Invalid option specified for getStringListInfo()");
+		return 0;
+	}
+	struct curljava *curl = (struct curljava *) jcurl;
+	struct curl_slist *list = NULL;
+
+	CURLcode res = curl_easy_getinfo(curl->libcurl, (CURLINFO) option, &list);
+	if (res != CURLE_OK) {
+		///TODO: Throw exception
+		return 0;
+	}
+
+	struct curl_slist *cur = list;
+	int size = 0;
+	while (cur) {
+		size += 1;
+		cur = cur->next;
+	}
+//	fprintf(stderr, "%i items returned\n", size);
+
+	jclass string_class = (*java)->FindClass(java,"Ljava/lang/String;");
+	if (!string_class) {
+		return 0;
+	}
+
+	jstrings = (*java)->NewObjectArray(java,size, string_class, NULL);
+	if (!jstrings) {
+		return 0;
+	}
+
+	cur = list;
+	int index = 0;
+	while (cur) {
+		jstring jstr = (*java)->NewStringUTF(java,cur->data);
+		(*java)->SetObjectArrayElement(java,jstrings, index, jstr);
+		(*java)->DeleteLocalRef(java,jstr);
+		cur = cur->next;
+		index += 1;
+	}
+
+	curl_slist_free_all(list);
+
+	return jstrings;
+}
Index: test.java
===================================================================
--- test.java	(revision 45)
+++ test.java	(working copy)
@@ -73,6 +73,13 @@
             cg.setopt(CurlGlue.CURLOPT_COOKIEFILE, "cookie.txt");
             cg.perform();
 
+			// get<type>Info tests:
+			String url = cg.getStringInfo(CurlGlue.CURLINFO_EFFECTIVE_URL);
+			System.out.println("Effective URL: "+url);
+
+			long code = cg.getLongInfo(CurlGlue.CURLINFO_RESPONSE_CODE);
+			System.out.println("Response code: "+code);
+
             // The cookie.txt file is actually created now
             cg.finalize();
 
Index: MakeCurlGlue.java
===================================================================
--- MakeCurlGlue.java	(revision 45)
+++ MakeCurlGlue.java	(working copy)
@@ -52,91 +52,43 @@
   throws IOException
   {
     String ls = System.getProperty( "line.separator" );
-    String s;
     BufferedReader fis = new BufferedReader(
                          new InputStreamReader( System.in ) );
 
-    s = "// Automatically created class with defines extracted from curl.h." + ls +
-        "// The curl class is a JNI wrapper for libcurl." + ls +
-        "// This is meant as a raw, crude and low-level interface to libcurl." + ls +
-        "// this list is up-to-date as of cURL " + sVersion + ls + ls +
-        "package net.haxx.curl;" + ls + ls +
-        "public class CurlGlue" + ls +
-        "{" + ls +
-        "  // start of generated list" + ls;
-    System.out.print( s );
+    Pattern opt = Pattern.compile( "(CURLOPT_[A-Z_]+)\\s+=\\s+(\\d+)\\s+\\+\\s+(\\d+)" );
+    Pattern info = Pattern.compile( "(CURLINFO_[A-Z_]+)\\s+=\\s+(0x\\d+)\\s+\\+\\s+(\\d+)" );
 
-    Pattern p = Pattern.compile( "(CURLOPT_[A-Z_]+)\\s+=\\s+(\\d+)\\s+\\+\\s+(\\d+)" );
+	String s;
+    String gen = "  // start of generated list" + ls;
     while( null != (s = fis.readLine()) )
     {
-      for (Matcher m = p.matcher( s ); m.find(); )
+      for (Matcher m = opt.matcher( s ); m.find(); )
       {
-        s = "  public static final int " + m.group( 1 ) + " = " +
+        gen += "  public static final int " + m.group( 1 ) + " = " +
             ( Integer.parseInt( m.group( 2 ) ) + Integer.parseInt( m.group( 3 ) ) ) + ";" + ls;
-        System.out.print( s );
       }
+
+      for (Matcher m = info.matcher( s ); m.find(); )
+      {
+        gen += "  public static final int " + m.group( 1 ) + " = " +
+            ( Integer.decode( m.group( 2 ) ) + Integer.parseInt( m.group( 3 ) ) ) + ";" + ls;
+      }
     }
     fis.close();
+	gen += "  // end of generated list" + ls;
 
-    s= "  // end of generated list" + ls + ls;
-    System.out.print( s );
-    s = "  public CurlGlue() {" + ls +
-        "    try {" + ls +
-        "      curljava_handle = jni_init();" + ls +
-        "    } catch (Exception e) {" + ls +
-        "      e.printStackTrace();" + ls +
-        "    }" + ls +
-        "  }" + ls + ls;
-    System.out.print( s );
-    s = "  public void finalize() {" + ls +
-        "    jni_cleanup(curljava_handle);" + ls +
-        "  }" + ls + ls +
-        "  private int curljava_handle;" + ls + ls +
-        "  // constructor and destructor for the libcurl handle" + ls +
-        "  private native int jni_init();" + ls +
-        "  private native void jni_cleanup(int curljava_handle);" + ls +
-        "  private native synchronized int jni_perform(int curljava_handle);" + ls + ls;
-    System.out.print( s );
-    s = "  // Instead of varargs, we have different functions for each" + ls +
-        "  // kind of type setopt() can take" + ls +
-        "  private native int jni_setopt(int libcurl, int option, String value);" + ls +
-        "  private native int jni_setopt(int libcurl, int option, int value);" + ls +
-        "  private native int jni_setopt(int libcurl, int option, CurlWrite value);" + ls +
-        "  private native int jni_setopt(int libcurl, int option, CurlRead value);" + ls +
-        "  private native int jni_setopt(int libcurl, int option, CurlIO value);" + ls + ls +
-        "  public native int getinfo();" + ls +
-        "  public static native String version();" + ls + ls;
-    System.out.print( s );
-    s = "  public int perform() {" + ls +
-        "    return jni_perform(curljava_handle);" + ls +
-        "  }" + ls +
-        "  public int setopt(int option, int value) {" + ls +
-        "    return jni_setopt(curljava_handle, option, value);" + ls +
-        "  }" + ls +
-        "  public int setopt(int option, String value) {" + ls +
-        "    return jni_setopt(curljava_handle, option, value);" + ls +
-        "  }" + ls;
-    System.out.print( s );
-    s = "  public int setopt(int option, CurlWrite value) {" + ls +
-        "    return jni_setopt(curljava_handle, option, value);" + ls +
-        "  }" + ls +
-        "  public int setopt(int option, CurlRead value) {" + ls +
-        "    return jni_setopt(curljava_handle, option, value);" + ls +
-        "  }" + ls +
-        "  public int setopt(int option, CurlIO value) {" + ls +
-        "    return jni_setopt(curljava_handle, option, value);" + ls +
-        "  }" + ls + ls;
-    System.out.print( s );
-    s = "  static {" + ls +
-        "    try {" + ls +
-        "      // Loading up the shared JNI" + ls +
-        "      System.loadLibrary(\"curljava\");" + ls +
-        "    } catch (Exception e) {" + ls +
-        "      e.printStackTrace();" + ls +
-        "    }" + ls +
-        "  }" + ls + ls +
-        "}" + ls + ls;
-    System.out.print( s );
+    fis = new BufferedReader(
+	      new FileReader("CurlGlue.template.java") );
+
+    while( null != (s = fis.readLine()) )
+	{
+		if (s.contains("<%_GENERATED_%>")) {
+			System.out.println(gen);
+		} else {
+			System.out.println(s);
+		}
+	}
+
   }
 }
 
Index: CurlGlue.template.java
===================================================================
--- CurlGlue.template.java	(revision 0)
+++ CurlGlue.template.java	(revision 0)
@@ -0,0 +1,92 @@
+// Automatically created class with defines extracted from curl.h.
+// The curl class is a JNI wrapper for libcurl.
+// This is meant as a raw, crude and low-level interface to libcurl.
+// this list is up-to-date as of cURL 7.19.7
+
+package net.haxx.curl;
+
+public class CurlGlue
+{
+//  <%_GENERATED_%>
+
+  public CurlGlue() {
+    try {
+      curljava_handle = jni_init();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+  @Override
+  public void finalize() {
+    jni_cleanup(curljava_handle);
+  }
+
+  private int curljava_handle;
+
+  // constructor and destructor for the libcurl handle
+  private native int jni_init();
+  private native void jni_cleanup(int curljava_handle);
+  private native synchronized int jni_perform(int curljava_handle);
+
+  // Instead of varargs, we have different functions for each
+  // kind of type setopt() can take
+  private native int jni_setopt(int libcurl, int option, String value);
+  private native int jni_setopt(int libcurl, int option, int value);
+  private native int jni_setopt(int libcurl, int option, CurlWrite value);
+  private native int jni_setopt(int libcurl, int option, CurlRead value);
+  private native int jni_setopt(int libcurl, int option, CurlIO value);
+
+  public native int getinfo();
+  public static native String version();
+
+  public int perform() {
+    return jni_perform(curljava_handle);
+  }
+  public int setopt(int option, int value) {
+    return jni_setopt(curljava_handle, option, value);
+  }
+  public int setopt(int option, String value) {
+    return jni_setopt(curljava_handle, option, value);
+  }
+  public int setopt(int option, CurlWrite value) {
+    return jni_setopt(curljava_handle, option, value);
+  }
+  public int setopt(int option, CurlRead value) {
+    return jni_setopt(curljava_handle, option, value);
+  }
+  public int setopt(int option, CurlIO value) {
+    return jni_setopt(curljava_handle, option, value);
+  }
+
+  private native long jni_getLongInfo(int libcurl, int option);
+  private native double jni_getDoubleInfo(int libcurl, int option);
+  private native String jni_getStringInfo(int libcurl, int option);
+  private native String[] jni_getStringListInfo(int libcurl, int option);
+
+  public long getLongInfo(int option) {
+	return jni_getLongInfo(curljava_handle,option);
+  }
+  public double getDoubleInfo(int option) {
+	return jni_getDoubleInfo(curljava_handle,option);
+  }
+
+  public String getStringInfo(int option) {
+	return jni_getStringInfo(curljava_handle,option);
+  }
+
+  public String[] getStringListInfo(int option) {
+	return jni_getStringListInfo(curljava_handle,option);
+  }
+  
+  static {
+    try {
+      // Loading up the shared JNI
+      System.loadLibrary("curljava");
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+}
+
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to