Update of /cvsroot/boost/boost/boost/asio/impl
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv29856/boost/asio/impl

Modified Files:
        read_until.ipp 
Log Message:
Add overloads of read_until and async_read_until that take a string. Fix
bug in regex-based overload of async_read_until.


Index: read_until.ipp
===================================================================
RCS file: /cvsroot/boost/boost/boost/asio/impl/read_until.ipp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- read_until.ipp      5 Jul 2006 05:48:10 -0000       1.3
+++ read_until.ipp      9 Jul 2006 07:08:03 -0000       1.4
@@ -20,6 +20,8 @@
 #include <boost/asio/detail/push_options.hpp>
 #include <algorithm>
 #include <limits>
+#include <string>
+#include <utility>
 #include <boost/asio/detail/pop_options.hpp>
 
 #include <boost/asio/buffer.hpp>
@@ -82,6 +84,98 @@
 
 template <typename Sync_Read_Stream, typename Allocator>
 inline std::size_t read_until(Sync_Read_Stream& s,
+    boost::asio::basic_streambuf<Allocator>& b, const std::string& delim)
+{
+  return read_until(s, b, delim, throw_error());
+}
+
+namespace detail
+{
+  // Algorithm that finds a subsequence of equal values in a sequence. Returns
+  // (iterator,true) if a full match was found, in which case the iterator
+  // points to the beginning of the match. Returns (iterator,false) if a
+  // partial match was found at the end of the first sequence, in which case
+  // the iterator points to the beginning of the partial match. Returns
+  // (last1,false) if no full or partial match was found.
+  template <typename Iterator1, typename Iterator2>
+  std::pair<Iterator1, bool> partial_search(
+      Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
+  {
+    for (Iterator1 iter1 = first1; iter1 != last1; ++iter1)
+    {
+      Iterator1 test_iter1 = iter1;
+      Iterator2 test_iter2 = first2;
+      for (;; ++test_iter1, ++test_iter2)
+      {
+        if (test_iter2 == last2)
+          return std::make_pair(iter1, true);
+        if (test_iter1 == last1)
+        {
+          if (test_iter2 != first2)
+            return std::make_pair(iter1, false);
+          else
+            break;
+        }
+        if (*test_iter1 != *test_iter2)
+          break;
+      }
+    }
+    return std::make_pair(last1, false);
+  }
+} // namespace detail
+
+template <typename Sync_Read_Stream, typename Allocator, typename 
Error_Handler>
+std::size_t read_until(Sync_Read_Stream& s,
+    boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
+    Error_Handler error_handler)
+{
+  std::size_t next_search_start = 0;
+  for (;;)
+  {
+    // Determine the range of the data to be searched.
+    typedef typename boost::asio::basic_streambuf<
+      Allocator>::const_buffers_type const_buffers_type;
+    typedef boost::asio::detail::const_buffers_iterator<
+      const_buffers_type> iterator;
+    const_buffers_type buffers = b.data();
+    iterator begin(buffers, next_search_start);
+    iterator end(buffers, (std::numeric_limits<std::size_t>::max)());
+
+    // Look for a match.
+    std::pair<iterator, bool> result = boost::asio::detail::partial_search(
+        begin, end, delim.begin(), delim.end());
+    if (result.first != end)
+    {
+      if (result.second)
+      {
+        // Full match. We're done.
+        return result.first.position() + delim.length();
+      }
+      else
+      {
+        // Partial match. Next search needs to start from beginning of match.
+        next_search_start = result.first.position();
+      }
+    }
+    else
+    {
+      // No match. Next search can start with the new data.
+      next_search_start = end.position();
+    }
+
+    // Need more data.
+    typename Sync_Read_Stream::error_type error;
+    b.commit(s.read_some(b.prepare(512), boost::asio::assign_error(error)));
+    if (error)
+    {
+      error_handler(error);
+      return 0;
+    }
+  }
+}
+
+template <typename Sync_Read_Stream, typename Allocator>
+inline std::size_t read_until(Sync_Read_Stream& s,
     boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr)
 {
   return read_until(s, b, expr, throw_error());
@@ -262,6 +356,159 @@
 namespace detail
 {
   template <typename Async_Read_Stream, typename Allocator, typename Handler>
+  class read_until_delim_string_handler
+  {
+  public:
+    read_until_delim_string_handler(Async_Read_Stream& stream,
+        boost::asio::basic_streambuf<Allocator>& streambuf,
+        const std::string& delim, std::size_t next_search_start,
+        Handler handler)
+      : stream_(stream),
+        streambuf_(streambuf),
+        delim_(delim),
+        next_search_start_(next_search_start),
+        handler_(handler)
+    {
+    }
+
+    void operator()(const typename Async_Read_Stream::error_type& e,
+        std::size_t bytes_transferred)
+    {
+      // Check for errors.
+      if (e)
+      {
+        std::size_t bytes = 0;
+        handler_(e, bytes);
+        return;
+      }
+
+      // Commit received data to streambuf's get area.
+      streambuf_.commit(bytes_transferred);
+
+      // Determine the range of the data to be searched.
+      typedef typename boost::asio::basic_streambuf<
+        Allocator>::const_buffers_type const_buffers_type;
+      typedef boost::asio::detail::const_buffers_iterator<
+        const_buffers_type> iterator;
+      const_buffers_type buffers = streambuf_.data();
+      iterator begin(buffers, next_search_start_);
+      iterator end(buffers, (std::numeric_limits<std::size_t>::max)());
+
+      // Look for a match.
+      std::pair<iterator, bool> result = boost::asio::detail::partial_search(
+          begin, end, delim_.begin(), delim_.end());
+      if (result.first != end)
+      {
+        if (result.second)
+        {
+          // Full match. We're done.
+          std::size_t bytes = result.first.position() + delim_.length();
+          handler_(e, bytes);
+          return;
+        }
+        else
+        {
+          // Partial match. Next search needs to start from beginning of match.
+          next_search_start_ = result.first.position();
+        }
+      }
+      else
+      {
+        // No match. Next search can start with the new data.
+        next_search_start_ = end.position();
+      }
+
+      // No match. Start a new asynchronous read operation to obtain more data.
+      stream_.async_read_some(streambuf_.prepare(512), *this);
+    }
+
+  //private:
+    Async_Read_Stream& stream_;
+    boost::asio::basic_streambuf<Allocator>& streambuf_;
+    std::string delim_;
+    std::size_t next_search_start_;
+    Handler handler_;
+  };
+
+  template <typename Async_Read_Stream, typename Allocator, typename Handler>
+  inline void* asio_handler_allocate(std::size_t size,
+      read_until_delim_string_handler<Async_Read_Stream,
+        Allocator, Handler>* this_handler)
+  {
+    return boost_asio_handler_alloc_helpers::allocate(
+        size, &this_handler->handler_);
+  }
+
+  template <typename Async_Read_Stream, typename Allocator, typename Handler>
+  inline void asio_handler_deallocate(void* pointer, std::size_t size,
+      read_until_delim_string_handler<Async_Read_Stream,
+        Allocator, Handler>* this_handler)
+  {
+    boost_asio_handler_alloc_helpers::deallocate(
+        pointer, size, &this_handler->handler_);
+  }
+
+  template <typename Handler_To_Dispatch, typename Async_Read_Stream,
+      typename Allocator, typename Handler>
+  inline void asio_handler_dispatch(const Handler_To_Dispatch& handler,
+      read_until_delim_string_handler<Async_Read_Stream,
+        Allocator, Handler>* this_handler)
+  {
+    boost_asio_handler_dispatch_helpers::dispatch_handler(
+        handler, &this_handler->handler_);
+  }
+} // namespace detail
+
+template <typename Async_Read_Stream, typename Allocator, typename Handler>
+void async_read_until(Async_Read_Stream& s,
+    boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
+    Handler handler)
+{
+  // Determine the range of the data to be searched.
+  typedef typename boost::asio::basic_streambuf<
+    Allocator>::const_buffers_type const_buffers_type;
+  typedef boost::asio::detail::const_buffers_iterator<
+    const_buffers_type> iterator;
+  const_buffers_type buffers = b.data();
+  iterator begin(buffers, 0);
+  iterator end(buffers, (std::numeric_limits<std::size_t>::max)());
+
+  // Look for a match.
+  std::size_t next_search_start;
+  std::pair<iterator, bool> result = boost::asio::detail::partial_search(
+      begin, end, delim.begin(), delim.end());
+  if (result.first != end)
+  {
+    if (result.second)
+    {
+      // Full match. We're done.
+      typename Async_Read_Stream::error_type error;
+      std::size_t bytes = result.first.position() + delim.length();
+      s.io_service().post(detail::bind_handler(handler, error, bytes));
+      return;
+    }
+    else
+    {
+      // Partial match. Next search needs to start from beginning of match.
+      next_search_start = result.first.position();
+    }
+  }
+  else
+  {
+    // No match. Next search can start with the new data.
+    next_search_start = end.position();
+  }
+
+  // No match. Start a new asynchronous read operation to obtain more data.
+  s.async_read_some(b.prepare(512),
+      detail::read_until_delim_string_handler<
+        Async_Read_Stream, Allocator, Handler>(
+          s, b, delim, next_search_start, handler));
+}
+
+namespace detail
+{
+  template <typename Async_Read_Stream, typename Allocator, typename Handler>
   class read_until_expr_handler
   {
   public:
@@ -325,7 +572,6 @@
       }
 
       // No match. Start a new asynchronous read operation to obtain more data.
-      next_search_start_ = end.position();
       stream_.async_read_some(streambuf_.prepare(512), *this);
     }
 



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs

Reply via email to