summaryrefslogtreecommitdiff
path: root/tests/libfetch
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-07-13 14:02:47 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-07-13 14:02:47 +0200
commit1f9e20cab94eb0e9bced570cbc425c804d7d32d2 (patch)
tree865937253540fe18adce7e101ad2dbec8ffbfdc9 /tests/libfetch
parent1454c6b21144b57dc556676313643baf5bc0f31a (diff)
downloadcrawler-1f9e20cab94eb0e9bced570cbc425c804d7d32d2.tar.gz
crawler-1f9e20cab94eb0e9bced570cbc425c804d7d32d2.tar.bz2
-
Diffstat (limited to 'tests/libfetch')
-rw-r--r--tests/libfetch/test2.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/libfetch/test2.cpp b/tests/libfetch/test2.cpp
new file mode 100644
index 0000000..41d7aae
--- /dev/null
+++ b/tests/libfetch/test2.cpp
@@ -0,0 +1,95 @@
+#include <iostream>
+#include <streambuf>
+#include <vector>
+#include <algorithm>
+#include <string>
+#include <cstring>
+
+using namespace std;
+
+#include "fetch.h"
+
+class libfetch_buffer : public streambuf
+{
+ public:
+ explicit libfetch_buffer( fetchIO *io, size_t bufSize = 256, size_t putBack = 1 );
+
+ private:
+ int_type underflow( );
+
+ private:
+ fetchIO *m_io;
+ const size_t m_putBack;
+ vector<char> m_buf;
+};
+
+libfetch_buffer::libfetch_buffer( fetchIO *io, size_t bufSize, size_t putBack )
+ : m_io( io ), m_putBack( max( putBack, size_t( 1 ) ) ),
+ m_buf( max( bufSize, putBack ) + putBack )
+{
+ char *end = &m_buf.front( ) + m_buf.size( );
+ setg( end, end, end );
+}
+
+streambuf::int_type libfetch_buffer::underflow( )
+{
+ // check if buffer is exhausted, if not, return current character
+ if( gptr( ) < egptr( ) )
+ return traits_type::to_int_type( *gptr( ) );
+
+ char *base = &m_buf.front( );
+ char *start = base;
+
+ // move put back away
+ if( eback( ) == base ) {
+ memmove( base, egptr( ) - m_putBack, m_putBack );
+ start += m_putBack;
+ }
+
+ // read from source
+ ssize_t n = fetchIO_read( m_io, start, m_buf.size( ) - ( start - base ) );
+ if( n == 0 ) {
+ return traits_type::eof( );
+ } else if( n < 0 ) {
+ // TODO handle error
+ }
+
+ // set pointers
+ setg( base, start, start + n );
+
+ return traits_type::to_int_type( *gptr( ) );
+}
+
+int main( int argc, char *argv[] )
+{
+ char *urlstring;
+ fetchIO *io;
+
+ if( argc != 2 ) {
+ cerr << "Usage: test2 <url>" << endl;
+ return 1;
+ }
+
+ urlstring = argv[1];
+
+ fetchTimeout = 2;
+
+ io = fetchGetURL( urlstring, "" );
+ if( io == NULL ) {
+ cerr << "ERROR: " << fetchLastErrString << endl;
+ return 1;
+ }
+
+ libfetch_buffer buf( io );
+ istream is( &buf );
+ string s;
+
+ while( !is.eof( ) ) {
+ getline( is, s );
+ cout << s << endl;
+ }
+
+ fetchIO_close( io );
+
+ return 0;
+}