forked from zhangyuanqiao/curl-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefine2java.pl
109 lines (87 loc) · 2.63 KB
/
define2java.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
# Hack to create CurlGlue.java from curl.h
# Initial version: Daniel <[email protected]>
# Updated version: Guenter <[email protected]>
#
open(IN, "${ARGV[1]}");
while(<IN>) {
if(/^#define LIBCURL_VERSION \"(\d{1,3}\.\d{1,3}\.\d{1,3}.*)\"$/) {
$curl_ver = $1;
}
}
close(IN);
print <<EOTXT;
/*
* The curl class is a JNI wrapper for libcurl. Please bear with me, I'm no
* true java dude (yet). Improve what you think is bad and send me the updates!
*
* This is meant as a raw, crude and low-level interface to libcurl. If you
* want fancy stuff, build upon this.
*/
public class CurlGlue
{
// start of generated list - this list is up-to-date as of Curl $curl_ver
EOTXT
open(GCC, "gcc -E ${ARGV[0]}|");
while(<GCC>) {
if($_ =~ /(CURLOPT_(.*)) += (.*)/) {
$var= $1;
$expr = $3;
$f=$3;
if($expr =~ / *(\d+) *\+ *(\d+)/) {
$expr = $1+$2;
}
# nah, keep the CURL prefix to make them look like other
# languages' defines
# $var =~ s/^CURL//g;
$var =~ s/ $//g;
print " public static final int $var = $expr;\n";
}
}
close(GCC);
print <<EOTXT;
// end of generated list
public CurlGlue() {
try {
javacurl_handle = jni_init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void finalize() {
jni_cleanup(javacurl_handle);
}
private int javacurl_handle;
/* constructor and destructor for the libcurl handle */
private native int jni_init();
private native void jni_cleanup(int javacurl_handle);
private native synchronized int jni_perform(int javacurl_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);
public native int getinfo();
public int perform() {
return jni_perform(javacurl_handle);
}
public int setopt(int option, int value) {
return jni_setopt(javacurl_handle, option, value);
}
public int setopt(int option, String value) {
return jni_setopt(javacurl_handle, option, value);
}
public int setopt(int option, CurlWrite value) {
return jni_setopt(javacurl_handle, option, value);
}
static {
try {
// Loading up javacurl.dll
System.loadLibrary("javacurl");
} catch (Exception e) {
e.printStackTrace();
}
}
}
EOTXT