LibCurl and GCCXML

GCC-XML

GCC-XML, an XML output extension to GCC, is designated to generate an XML description of a C/C++ program from GCC's internal representation. The details could be found here.

libcurl

Libcurl is a free and easy-to-use client side URL transfer library, which supports most of transfer protocols even SSL certificates. It provides a straightforward scenario to init the URL link and parse the link as request. The command details can be found here.

DOM XML parser for KCML

By utilising Xerces XML DOM parser provided by KCML, we present a example on how to adopt libcurl library in KCML. The $DECLARE is used to create the external link for the specific libcurl functions, which can be found by the following link.

Passing the C/C++ source file to GCC-XML to generate the XML output. (P.S "/I" can be used to indicate the specific library location.)

The generated XML file include a few elements, which could be used for KCML:

Variable Types

Excluding PointerType, all the other types could simply be indicated as an appropriate data type. For getting the correct type from PointerType, it could be traced back into the following types.

Enumeration Types

For enumerations, DEFENUM is used to define a set of constants for each enumeration type. For instances:
DEFENUM URLversion
	DIM _CURLVERSION_FIRST=0
	DIM _CURLVERSION_SECOND=1
	DIM _CURLVERSION_THIRD=2
	DIM _CURLVERSION_FOURTH=3
	DIM _CURLVERSION_LAST=4
END ENUM
where URLversion is a enumeration type, which includes 5 constant values.

Return Type

The return type could also be traced back to the above correct variable types, which will be written in the end of the declared function followed by "TO". If there is no return type indicated, then INT() will be considered by default.

Link to Libcurl Function

Followed by return type for the specific function, "=" can be used to specify a function to be linked followed by the [libraryName.functionName]. To be noted the "*" can be put as prefix to force the $DECLARE function executed on the server rather than the client. Due to the calling convention issue, for program in Windows to invoke function exported with the standard C calling convention, such convention can be specified by appending the "!" to the function name.

Thus each libcurl function could be called by starting with $DECLARE and the function name and its input parameters. For instances, the function "CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);" could be called as

$DECLARE 'curl_easy_setopt(RETURN INT(), INT(), ...) TO INT()
If it is required to execute on server side in standard C calling convention in Windows, it will declared as follow:
$DECLARE 'curl_easy_setopt(RETURN INT(), INT(), ...) TO INT() = "*libcurl.curl_easy_setopt!"
For the above declared function, it could be called in KCML as follows:
DIM handle, curl_code
curl_code = 'curl_easy_setopt(BYREF handle, _CURLOPT_URL, "http://example.com")
where _CURLOPT_URL is a constant from the enumeration set of CURL_OPT.

The following table shows th relevant library to be called for each O/S.

Function/Library Windows Linux AIX
Libcurl libcurl libcurl libcurl.a(libcurl.so.4)
fopen, fwrite, fclose, free msvcrt libc.so.6 libc.a(shr.o)
dlsym, dlopen n/a libdl libdl

To be noted that in Linux/Unix, the above mentioned libraries might have been included in the system by default. Then the function name could be called directly instead of the library prefix. Currently, the program has been tweaked to accept "libcurl" itself to represent libcurl.a(libcurl.so.4) for AIX

Proxy

Here is the way to setup the proxy:
'curl_easy_setopt(handle, _CURLOPT_PROXY, "proxy:8080")
'curl_easy_setopt(handle, _CURLOPT_PROXYUSERPWD, "username:password")
For HTTPS link, where the server might require the authenticate certificate, it could be skipped by the following:
'curl_easy_setopt(handle, _CURLOPT_SSL_VERIFYPEER, 0)

Output

Since some of the curl functions return a string, which $DECLARE could not return a string type but a pointer to that string, for displaying the returned string, here is the solution by adopting msnprintf:
'curl_msnprintf(str$, str_size, str_pointer)
PRINT str$
By passing the string pointer "str_pointer" and indicate the size of the string, string variable "str$" will be assigned by its given string.

Misc

Here is the relevant example files to manipulate the XML output file and produce the KCML output file, which includes the Enumeration sets and function declarations.

curl_xml.src: source kcml program to process the XML output file

kcml -p curl_xml.src [XML source file] [1:Windows/Linux/UNIX, 2: AIX specific]

curl_xml.sh: script file to process the C/C++ file to XML and generate the KCML functions and enumeration file

./curl_xml.sh -f [Source file C/C++] -v [1:Windows/Linux/UNIX, 2: AIX specific]

The output file "curl_func_generic.src" is suitable for Windows/Linux/UNIX, "curl_func_aix.src" is generated by explicitly indicating the libcurl library

gccxml_config_gcc: gccxml config file to indicate the compiler and including library

Dynamic library for Windows

The related libcurl libaries have been packed as a msi, which can be found from here.

Sample files

simple.c: sample c source file calling libcurl functions

simple.xml: sample xml output file by adopting gccxml

curl_func.src: sample KCML output source file by calling "curl_xml.src" for parsing the sample XML output into KCML function declarations.

Sample program for different O/S

The provided sample program is capable of fetching the given file via the given link, the above programs have been tested on Windows, Linux and AIX on "HTTP", "HTTPS" and "FTP" links

See also: