source: trunk/src/io/std_stream.cc @ 134

Last change on this file since 134 was 134, checked in by epyon, 12 years ago
  • io/std_stream adapter added
  • TODO: test, and substitute memmove for copy
File size: 1.5 KB
RevLine 
[134]1// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/io/std_stream.hh"
8#include <algorithm>
9#include <cstring> // TODO: remove, see below
10
11using namespace nv;
12
13std_stream::std_stream( stream* source, bool owner /*= false*/, std::size_t bsize /*= 256*/, std::size_t put_back /*= 8 */ )
14        : m_stream( source )
15        , m_owner( owner )
16        , m_put_back( std::max( put_back, std::size_t( 1 ) ) )
17        , m_buffer( std::max(bsize, put_back) + put_back )
18{
19        char *end = &m_buffer.front() + m_buffer.size();
20        setg( end, end, end );
21}
22
23std_stream::~std_stream()
24{
25        if ( m_owner )
26        {
27                delete m_stream;
28        }
29}
30
31std_stream::int_type std_stream::underflow()
32{
33        if (gptr() < egptr())
34        {
35                // buffer not exhausted
36                return traits_type::to_int_type(*gptr());
37        }
38
39        char *base = &m_buffer.front();
40        char *start = base;
41
42        if (eback() == base) // true when this isn't the first fill
43        {
44                // Make arrangements for putback characters
45                // TODO: std::copy( egptr() - m_put_back, egptr(), base ); instead?
46                std::memmove(base, egptr() - m_put_back, m_put_back);
47                start += m_put_back;
48        }
49
50        // start is now the start of the buffer, proper.
51        size_t n = m_stream->read( start, 1, m_buffer.size() - (start - base) );
52        if (n == 0)
53        {
54                return traits_type::eof();
55        }
56
57        // Set buffer pointers
58        setg(base, start, start + n);
59
60        return traits_type::to_int_type(*gptr());
61}
Note: See TracBrowser for help on using the repository browser.