From: Alexis Laferrière Date: Sat, 6 Jun 2015 18:08:40 +0000 (-0400) Subject: lib/curl: update style and add more space between properties X-Git-Tag: v0.7.6~52^2~29 X-Git-Url: http://nitlanguage.org lib/curl: update style and add more space between properties Signed-off-by: Alexis Laferrière --- diff --git a/lib/curl/curl_c.nit b/lib/curl/curl_c.nit index fe0a446..7d28842 100644 --- a/lib/curl/curl_c.nit +++ b/lib/curl/curl_c.nit @@ -63,6 +63,7 @@ in "C body" `{ } return count; } + // Callback method to read datas from buffer. size_t nit_curl_callback_read_func(void *buffer, size_t size, size_t count, CURLCallbackReadDatas *datas){ int len = datas->len - datas->pos; @@ -77,14 +78,17 @@ in "C body" `{ extern class CCurl `{ CURL * `} # Constructor, CURL low level initializer new easy_init `{ return curl_easy_init(); `} + # Check for correct initialization - fun is_init:Bool `{ return (self != NULL); `} + fun is_init: Bool `{ return (self != NULL); `} + # Easy Clean / Release CURL instance fun easy_clean `{ curl_easy_cleanup( self ); `} + # Perform the transfer described by setted options - fun easy_perform:CURLCode `{ return curl_easy_perform( self ); `} # Set options to tell CURL how to behave. Obj parameter type can be Int, Bool, String, OFile, CURLSList. - fun easy_setopt(opt: CURLOption, obj: Object):CURLCode + fun easy_perform: CURLCode `{ return curl_easy_perform( self ); `} + fun easy_setopt(opt: CURLOption, obj: Object): CURLCode do if obj isa Int then return i_setopt_int(opt, obj) if obj isa Bool and obj == true then return i_setopt_int(opt, 1) @@ -96,24 +100,29 @@ extern class CCurl `{ CURL * `} end # Internal method to set options to CURL using OFile parameter. private fun i_setopt_file(opt: CURLOption, fl: OFile):CURLCode `{ return curl_easy_setopt( self, opt, fl); `} + # Internal method to set options to CURL using Int parameter. - private fun i_setopt_int(opt: CURLOption, num: Int):CURLCode `{ return curl_easy_setopt( self, opt, num); `} + private fun i_setopt_int(opt: CURLOption, num: Int): CURLCode `{ return curl_easy_setopt( self, opt, num); `} + # Internal method to set options to CURL using CURLSList parameter. - private fun i_setopt_slist(opt: CURLOption, list: CURLSList):CURLCode `{ return curl_easy_setopt( self, opt, list); `} + private fun i_setopt_slist(opt: CURLOption, list: CURLSList): CURLCode `{ return curl_easy_setopt( self, opt, list); `} + # Internal method to set options to CURL using String parameter. - private fun i_setopt_string(opt: CURLOption, str: String):CURLCode import String.to_cstring `{ + private fun i_setopt_string(opt: CURLOption, str: String): CURLCode import String.to_cstring `{ char *rStr = String_to_cstring(str); return curl_easy_setopt( self, opt, rStr); `} + # Request Chars internal information from the CURL session - fun easy_getinfo_chars(opt: CURLInfoChars):nullable CURLInfoResponseString + fun easy_getinfo_chars(opt: CURLInfoChars): nullable CURLInfoResponseString do var answ = new CURLInfoResponseString if not i_getinfo_chars(opt, answ).is_ok then return null return answ end + # Internal method used to get String object information initially knowns as C Chars type - private fun i_getinfo_chars(opt: CURLInfoChars, res: CURLInfoResponseString):CURLCode import CURLInfoResponseString.response=, NativeString.to_s_with_copy `{ + private fun i_getinfo_chars(opt: CURLInfoChars, res: CURLInfoResponseString): CURLCode import CURLInfoResponseString.response=, NativeString.to_s_with_copy `{ char *r = NULL; CURLcode c = curl_easy_getinfo( self, opt, &r); if((c == CURLE_OK) && r != NULL){ @@ -122,15 +131,17 @@ extern class CCurl `{ CURL * `} } return c; `} + # Request Long internal information from the CURL session - fun easy_getinfo_long(opt: CURLInfoLong):nullable CURLInfoResponseLong + fun easy_getinfo_long(opt: CURLInfoLong): nullable CURLInfoResponseLong do var answ = new CURLInfoResponseLong if not i_getinfo_long(opt, answ).is_ok then return null return answ end + # Internal method used to get Int object information initially knowns as C Long type - private fun i_getinfo_long(opt: CURLInfoLong, res: CURLInfoResponseLong):CURLCode import CURLInfoResponseLong.response= `{ + private fun i_getinfo_long(opt: CURLInfoLong, res: CURLInfoResponseLong): CURLCode import CURLInfoResponseLong.response= `{ long *r = NULL; r = malloc(sizeof(long)); CURLcode c = curl_easy_getinfo( self, opt, r); @@ -138,15 +149,17 @@ extern class CCurl `{ CURL * `} free(r); return c; `} + # Request Double internal information from the CURL session - fun easy_getinfo_double(opt: CURLInfoDouble):nullable CURLInfoResponseDouble + fun easy_getinfo_double(opt: CURLInfoDouble): nullable CURLInfoResponseDouble do var answ = new CURLInfoResponseDouble if not i_getinfo_double(opt, answ).is_ok then return null return answ end + # Internal method used to get Int object information initially knowns as C Double type - private fun i_getinfo_double(opt: CURLInfoDouble, res: CURLInfoResponseDouble):CURLCode import CURLInfoResponseDouble.response= `{ + private fun i_getinfo_double(opt: CURLInfoDouble, res: CURLInfoResponseDouble): CURLCode import CURLInfoResponseDouble.response= `{ double *r = NULL; r = malloc(sizeof(double)); CURLcode c = curl_easy_getinfo( self, opt, r); @@ -154,8 +167,9 @@ extern class CCurl `{ CURL * `} free(r); return c; `} + # Request SList internal information from the CURL session - fun easy_getinfo_slist(opt: CURLInfoSList):nullable CURLInfoResponseArray + fun easy_getinfo_slist(opt: CURLInfoSList): nullable CURLInfoResponseArray do var answ = new CURLInfoResponseArray if not i_getinfo_slist(opt, answ).is_ok then return null @@ -163,29 +177,33 @@ extern class CCurl `{ CURL * `} answ.prim_response.destroy return answ end + # Internal method used to get Array[String] object information initially knowns as C SList type - private fun i_getinfo_slist(opt: CURLInfoSList, res: CURLInfoResponseArray):CURLCode import CURLInfoResponseArray.prim_response=`{ + private fun i_getinfo_slist(opt: CURLInfoSList, res: CURLInfoResponseArray): CURLCode import CURLInfoResponseArray.prim_response= `{ struct curl_slist* csl = NULL; CURLcode ce = curl_easy_getinfo( self, opt, &csl); CURLInfoResponseArray_prim_response__assign(res, csl); return ce; `} + # Register delegate to get callbacks about the CURL transfer performed - fun register_callback(delegate: CCurlCallbacks, cbtype: CURLCallbackType):CURLCode + fun register_callback(delegate: CCurlCallbacks, cbtype: CURLCallbackType): CURLCode do if once [new CURLCallbackType.header, new CURLCallbackType.body, new CURLCallbackType.stream, new CURLCallbackType.read].has(cbtype) then return i_register_callback(delegate, cbtype) end return once new CURLCode.unknown_option end + # Register delegate to read datas from given buffer - fun register_read_datas_callback(delegate: CCurlCallbacks, datas: String):CURLCode + fun register_read_datas_callback(delegate: CCurlCallbacks, datas: String): CURLCode do if datas.length > 0 then return i_register_read_datas_callback(delegate, datas, datas.length) return once new CURLCode.unknown_option end + # Internal method used to configure read callback - private fun i_register_read_datas_callback(delegate: CCurlCallbacks, datas: String, size: Int):CURLCode import String.to_cstring `{ + private fun i_register_read_datas_callback(delegate: CCurlCallbacks, datas: String, size: Int): CURLCode import String.to_cstring `{ CURLCallbackReadDatas *d = NULL; d = malloc(sizeof(CURLCallbackReadDatas)); d->data = (char*)String_to_cstring(datas); @@ -193,8 +211,9 @@ extern class CCurl `{ CURL * `} d->pos = 0; return curl_easy_setopt( self, CURLOPT_READDATA, d); `} + # Internal method used to configure callbacks in terms of given type - private fun i_register_callback(delegate: CCurlCallbacks, cbtype: CURLCallbackType):CURLCode is extern import CCurlCallbacks.header_callback, CCurlCallbacks.body_callback, CCurlCallbacks.stream_callback, NativeString.to_s_with_copy, NativeString.to_s `{ + private fun i_register_callback(delegate: CCurlCallbacks, cbtype: CURLCallbackType):CURLCode is extern import CCurlCallbacks.native_header_callback, CCurlCallbacks.native_body_callback, CCurlCallbacks.native_stream_callback `{ CURLCallbackDatas *d = malloc(sizeof(CURLCallbackDatas)); CCurlCallbacks_incr_ref(delegate); d->type = cbtype; @@ -219,8 +238,9 @@ extern class CCurl `{ CURL * `} } return e; `} + # Convert given string to URL encoded string - fun escape(url: String):String import String.to_cstring, NativeString.to_s_with_copy `{ + fun escape(url: String): String import String.to_cstring, NativeString.to_s_with_copy `{ char *orig_url, *encoded_url = NULL; orig_url = String_to_cstring(url); encoded_url = curl_easy_escape( self, orig_url, strlen(orig_url)); @@ -267,7 +287,8 @@ extern class CURLCallbackType `{ CURLcallbackType `} new body `{ return CURLcallbackTypeBody; `} new stream `{ return CURLcallbackTypeStream; `} new read `{ return CURLcallbackTypeRead; `} - fun to_i:Int `{ return self; `} + + fun to_i: Int `{ return self; `} end # CURL Code binding and helpers @@ -276,11 +297,12 @@ extern class CURLCode `{ CURLcode `} new unsupported_protocol `{ return CURLE_UNSUPPORTED_PROTOCOL; `} new ok `{ return CURLE_OK; `} new failed_init `{ return CURLE_FAILED_INIT; `} - fun code:Int `{ return self; `} - fun is_ok:Bool `{ return self == CURLE_OK; `} - fun is_valid_protocol:Bool `{ return self == CURLE_UNSUPPORTED_PROTOCOL; `} - fun is_valid_init:Bool `{ return self == CURLE_FAILED_INIT; `} - fun to_i:Int do return code end + + fun code: Int `{ return self; `} + fun is_ok: Bool `{ return self == CURLE_OK; `} + fun is_valid_protocol: Bool `{ return self == CURLE_UNSUPPORTED_PROTOCOL; `} + fun is_valid_init: Bool `{ return self == CURLE_FAILED_INIT; `} + fun to_i: Int do return code end redef fun to_s import NativeString.to_s_with_copy `{ char *c = (char*)curl_easy_strerror(self); return NativeString_to_s_with_copy(c); @@ -291,29 +313,37 @@ end extern class CURLSList `{ struct curl_slist * `} # Empty constructor which allow us to avoid the use of Nit NULLABLE type private new `{ return NULL; `} + # Constructor allow us to get list instancied by appending an element inside. new with_str(s: String) import String.to_cstring `{ struct curl_slist *l = NULL; l = curl_slist_append(l, String_to_cstring(s)); return l; `} + # Check for initialization - fun is_init:Bool `{ return (self != NULL); `} + fun is_init: Bool `{ return (self != NULL); `} + # Append an element in the linked list fun append(key: String) import String.to_cstring `{ char *k = String_to_cstring(key); curl_slist_append(self, (char*)k); `} + # Internal method to check for reachability of current data - private fun i_data_reachable(c: CURLSList):Bool `{ return (c != NULL && c->data != NULL); `} + private fun i_data_reachable(c: CURLSList): Bool `{ return (c != NULL && c->data != NULL); `} + # Internal method to check for reachability of next element - private fun i_next_reachable(c: CURLSList):Bool `{ return (c != NULL && c->next != NULL); `} + private fun i_next_reachable(c: CURLSList): Bool `{ return (c != NULL && c->next != NULL); `} + # Internal method to get current data - private fun i_data(c: CURLSList):String import NativeString.to_s `{ return NativeString_to_s(c->data); `} + private fun i_data(c: CURLSList): String import NativeString.to_s `{ return NativeString_to_s(c->data); `} + # Internal method to get next element - private fun i_next(c: CURLSList):CURLSList `{ return c->next; `} + private fun i_next(c: CURLSList): CURLSList `{ return c->next; `} + # Convert current low level List to an Array[String] object - fun to_a:Array[String] + fun to_a: Array[String] do var r = new Array[String] var cursor = self @@ -324,6 +354,7 @@ extern class CURLSList `{ struct curl_slist * `} end return r end + # Release allocated memory fun destroy `{ curl_slist_free_all(self); `} end @@ -347,23 +378,23 @@ end # Array Response type of CCurl.easy_getinfo method class CURLInfoResponseArray - var response:Array[String] = new Array[String] - private var prim_response:CURLSList = new CURLSList + var response: Array[String] = new Array[String] + private var prim_response: CURLSList = new CURLSList end # Long Response type of CCurl.easy_getinfo method class CURLInfoResponseLong - var response:Int=0 + var response = 0 end # Double Response type of CCurl.easy_getinfo method class CURLInfoResponseDouble - var response:Int=0 + var response = 0 end -# String Response type of CCurl:easy_getinfo method +# String Response type of CCurl::easy_getinfo method class CURLInfoResponseString - var response:String = "" + var response = "" end # Reproduce Enum of available CURL SList information, used for CCurl.easy_getinfo @@ -462,7 +493,7 @@ extern class CURLStatusCode `{ int `} new service_unavailable `{ return 503; `} new gateway_timeout `{ return 504; `} new http_version_not_supported `{ return 505; `} - fun to_i:Int `{ return self; `} + fun to_i: Int `{ return self; `} end # Reproduce Enum of CURL Options usable, used for CCurl.easy_setopt @@ -472,12 +503,16 @@ extern class CURLOption `{ CURLoption `} # Display verbose information. new verbose `{ return CURLOPT_VERBOSE; `} + # Include the header in the body output. new header `{ return CURLOPT_HEADER; `} + # Shut off the progress meter. new no_progress `{ return CURLOPT_NOPROGRESS; `} + # Do not install signal handlers. new no_signal `{ return CURLOPT_NOSIGNAL; `} + # Transfer multiple files according to a file name pattern. new wild_card_match `{ return CURLOPT_WILDCARDMATCH; `} @@ -485,6 +520,7 @@ extern class CURLOption `{ CURLoption `} # Callback for writing data. new write_function `{ return CURLOPT_WRITEFUNCTION; `} + # Data pointer to pass to the write callback. new write_data `{ return CURLOPT_WRITEDATA; `} @@ -576,8 +612,10 @@ extern class CURLOption `{ CURLoption `} # Accept-Encoding and automatic decompressing data. new accept_encoding `{ return CURLOPT_ACCEPT_ENCODING; `} + # Request Transfer-Encoding. new transfert_encoding `{ return CURLOPT_TRANSFER_ENCODING; `} + # Follow HTTP redirects. new follow_location `{ return CURLOPT_FOLLOWLOCATION; `} @@ -587,8 +625,10 @@ extern class CURLOption `{ CURLoption `} # Issue a HTTP PUT request. new put `{ return CURLOPT_PUT; `} + # Issue a HTTP POS request. new post `{ return CURLOPT_POST; `} + # Send a POST with this data. new postfields `{ return CURLOPT_POSTFIELDS; `} @@ -600,6 +640,7 @@ extern class CURLOption `{ CURLoption `} # User-Agent: header. new user_agent `{ return CURLOPT_USERAGENT; `} + # Custom HTTP headers. new httpheader `{ return CURLOPT_HTTPHEADER; `} @@ -624,6 +665,7 @@ extern class CURLOption `{ CURLoption `} # Address of the sender. new mail_from `{ return CURLOPT_MAIL_FROM; `} + # Address of the recipients. new mail_rcpt `{ return CURLOPT_MAIL_RCPT; `} @@ -642,6 +684,7 @@ extern class CURLOption `{ CURLoption `} # List only. new dir_list_only `{ return CURLOPT_DIRLISTONLY; `} + # Append to remote file. new append `{ return CURLOPT_APPEND; `}