lib/curl: use File and NativeFile instead of the local OFile
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 6 Jun 2015 20:55:43 +0000 (16:55 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sun, 7 Jun 2015 19:41:45 +0000 (15:41 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/curl/curl.nit
lib/curl/curl_c.nit

index 027fd95..a47bcd4 100644 (file)
@@ -168,9 +168,8 @@ class CurlHTTPRequest
                        return answer_failure(0, "Unable to extract file name, please specify one")
                end
 
-               success_response.i_file = new OFile.open(opt_name.to_cstring)
-               if not success_response.i_file.is_valid then
-                       success_response.i_file.close
+               success_response.i_file = new FileWriter.open(opt_name)
+               if not success_response.i_file.is_writable then
                        return answer_failure(0, "Unable to create associated file")
                end
 
@@ -395,12 +394,12 @@ class CurlFileResponseSuccess
        var speed_download = 0
        var size_download = 0
        var total_time = 0
-       private var i_file: nullable OFile = null
+       private var i_file: nullable FileWriter = null
 
        # Receive bytes stream from request due to stream callback registering
        redef fun stream_callback(buffer)
        do
-               self.i_file.write(buffer, size, count)
+               i_file.write buffer
        end
 end
 
index d16c69b..374706a 100644 (file)
@@ -17,6 +17,9 @@
 # Binding of C libCurl which allow us to interact with network.
 module curl_c is pkgconfig("libcurl")
 
+intrude import standard::file
+import standard
+
 in "C header" `{
        #include <curl/curl.h>
 
@@ -78,20 +81,24 @@ extern class CCurl `{ CURL * `}
        fun easy_clean `{ curl_easy_cleanup( self ); `}
 
        # Perform the transfer described by setted options
-       # Set options to tell CURL how to behave. Obj parameter type can be Int, Bool, String, OFile, CURLSList.
        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, FileWriter, CURLSList.
        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)
                if obj isa Bool and obj == false then return i_setopt_int(opt, 0)
                if obj isa String then return i_setopt_string(opt, obj)
-               if obj isa OFile then return i_setopt_file(opt, obj)
+               if obj isa FileWriter then return i_setopt_file(opt, obj._file.as(not null))
                if obj isa CURLSList then return i_setopt_slist(opt, obj)
                return once new CURLCode.unknown_option
        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 NativeFile parameter.
+       private fun i_setopt_file(opt: CURLOption, file: NativeFile): CURLCode `{
+               return curl_easy_setopt( self, opt, file);
+       `}
 
        # 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); `}
@@ -253,30 +260,6 @@ extern class CCurl `{ CURL * `}
        `}
 end
 
-# FILE Extern type, reproduce basic FILE I/O
-extern class OFile `{ FILE* `}
-       # Open / Create a file from given name
-       new open(str: NativeString) `{ return fopen(str, "wb"); `}
-       # Check for File validity
-       fun is_valid:Bool `{ return self != NULL; `}
-       # Internal method to write to the current file
-       private fun n_write(buffer: NativeString, size: Int, count: Int):Int `{ return fwrite(buffer, size, count, self); `}
-       # Write datas to the current file
-       fun write(buffer: String, size: Int, count: Int):Int
-       do
-               if is_valid == true then return n_write(buffer.to_cstring, size, count)
-               return 0
-       end
-       # Internal method to close the current file
-       private fun n_close:Int `{ return fclose(self); `}
-       # Close the current file
-       fun close:Bool
-       do
-               if is_valid == true then return n_close == 0
-               return false
-       end
-end
-
 # Interface for internal information callbacks methods
 interface CCurlCallbacks
        fun header_callback(buffer: String) do end