forked from JosephP91/curlcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a curl_writer used to change stream and writer callback f…
…unctions used by libcurl .Now you can choose your stream (for example a file) and write libcurl function output into it, using your custom function/method, or use a default one.
- Loading branch information
Showing
7 changed files
with
162 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// curl_writer.h | ||
// curlcpp | ||
// | ||
// Created by Giuseppe Persico on 03/07/14. | ||
// Copyright (c) 2014 Giuseppe Persico. All rights reserved. | ||
// | ||
|
||
#ifndef __curlcpp__curl_writer__ | ||
#define __curlcpp__curl_writer__ | ||
|
||
#include <iostream> | ||
|
||
using std::ostream; | ||
|
||
// Let's typedef this big boy to enheance code readability. | ||
typedef size_t (*curlcpp_writer_type)(void *, size_t, size_t, void *); | ||
|
||
namespace curl { | ||
/** | ||
* This class allows users to specify a stream where to put the | ||
* output returned by libcurl functions. curl_easy class | ||
* will set all the necessary options to make it easy. You just have | ||
* to specify your stream and your writer function. | ||
*/ | ||
class curl_writer { | ||
public: | ||
/** | ||
* The default constructor will use a default stream and a default | ||
* writer callback to perform the operations. | ||
*/ | ||
curl_writer(); | ||
/** | ||
* This overloaded constructor allows users to specify a stream | ||
* where to write, using the default writer callback. | ||
*/ | ||
curl_writer(ostream &); | ||
/** | ||
* This overloaed constructor allows users to specify a writer | ||
* callback function used specify how to write something on the | ||
* the default stream which is std::cout; | ||
*/ | ||
curl_writer(curlcpp_writer_type); | ||
/** | ||
* This overloaded constructor allows users to specify a custom | ||
* stream and a custom writer callback function. | ||
*/ | ||
curl_writer(ostream &, curlcpp_writer_type); | ||
/** | ||
* Simple getter method that returns the stream specifyed in the | ||
* constructor. | ||
*/ | ||
ostream *get_stream() const; | ||
/** | ||
* Simple getter method that returns the function specified (or not) | ||
* in the constructor. | ||
*/ | ||
curlcpp_writer_type get_function() const; | ||
protected: | ||
/** | ||
* Utility method used to validate the function pointer eventually | ||
* specified. | ||
*/ | ||
void set_writer_ptr(curlcpp_writer_type); | ||
private: | ||
curlcpp_writer_type _writer_ptr; | ||
ostream *_stream_ptr; | ||
}; | ||
} | ||
|
||
#endif /* defined(__curlcpp__curl_writer__) */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// curl_writer.cpp | ||
// curlcpp | ||
// | ||
// Created by Giuseppe Persico on 03/07/14. | ||
// Copyright (c) 2014 Giuseppe Persico. All rights reserved. | ||
// | ||
|
||
#include "curl_writer.h" | ||
|
||
using std::cout; | ||
using curl::curl_writer; | ||
|
||
// Default memory write callback. | ||
namespace { | ||
size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp) { | ||
const size_t realsize = size * nmemb; | ||
ostream* const mem = static_cast<ostream*>(userp); | ||
mem->write(static_cast<const char*>(contents), realsize); | ||
return realsize; | ||
} | ||
} | ||
|
||
// Implementation of constructor. | ||
curl_writer::curl_writer() { | ||
_stream_ptr = &cout; | ||
_writer_ptr = &write_memory_callback; | ||
} | ||
|
||
// Implementation of overloaded constructor. | ||
curl_writer::curl_writer(ostream &stream) { | ||
_stream_ptr = &stream; | ||
_writer_ptr = &write_memory_callback; | ||
} | ||
|
||
// Implementation of another overloaded constructor. | ||
curl_writer::curl_writer(curlcpp_writer_type writer_ptr) { | ||
_stream_ptr = &cout; | ||
this->set_writer_ptr(writer_ptr); | ||
} | ||
|
||
// Implementation of another overloaded constructor. | ||
curl_writer::curl_writer(ostream &stream, curlcpp_writer_type writer_ptr) { | ||
_stream_ptr = &stream; | ||
this->set_writer_ptr(writer_ptr); | ||
} | ||
|
||
// Implementation of get_stream method. | ||
ostream *curl_writer::get_stream() const { | ||
return _stream_ptr; | ||
} | ||
|
||
// Impementation of get_function method. | ||
curlcpp_writer_type curl_writer::get_function() const { | ||
return _writer_ptr; | ||
} | ||
|
||
// Implementation of set_writer_ptr method. | ||
void curl_writer::set_writer_ptr(curlcpp_writer_type writer_ptr) { | ||
_writer_ptr = (writer_ptr == nullptr) ? &write_memory_callback : writer_ptr; | ||
} |