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

Last change on this file since 374 was 374, checked in by epyon, 10 years ago
  • MASSIVE commit
  • common.hh - size_t, ptrdiff_t, nv:: namespace, NV_ALIGN_OF and basic template tools
  • STL - algorithm.hh, iterator.hh, limits.hh, numeric.hh and type_info.hh
  • STL - updates to memory, array and string
File size: 1.5 KB
RevLine 
[319]1// Copyright (C) 2012-2014 ChaosForge Ltd
[134]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
[135]6//
7// TODO: support for write operations, see http://www.mr-edd.co.uk/blog/beginners_guide_streambuf
[134]8
9#include "nv/io/std_stream.hh"
[374]10#include "nv/stl/math.hh"
11#include "nv/stl/utility.hh"
[134]12
13using namespace nv;
14
[138]15std_streambuf::std_streambuf( stream* source, bool owner /*= false*/, std::size_t bsize /*= 256*/, std::size_t put_back /*= 8 */ )
[134]16        : m_stream( source )
17        , m_owner( owner )
[374]18        , m_buffer( nv::max(bsize, put_back) + put_back )
19        , m_put_back( nv::max( put_back, std::size_t( 1 ) ) )
[134]20{
21        char *end = &m_buffer.front() + m_buffer.size();
22        setg( end, end, end );
23}
24
[138]25std_streambuf::~std_streambuf()
[134]26{
27        if ( m_owner )
28        {
29                delete m_stream;
30        }
31}
32
[138]33std_streambuf::int_type std_streambuf::underflow()
[134]34{
35        if (gptr() < egptr())
36        {
37                // buffer not exhausted
38                return traits_type::to_int_type(*gptr());
39        }
40
41        char *base = &m_buffer.front();
42        char *start = base;
43
44        if (eback() == base) // true when this isn't the first fill
45        {
46                // Make arrangements for putback characters
[138]47                std::copy( egptr() - m_put_back, egptr(), base );
[134]48                start += m_put_back;
49        }
50
51        // start is now the start of the buffer, proper.
[198]52        size_t n = m_stream->read( start, 1, m_buffer.size() - static_cast<size_t>(start - base) );
[134]53        if (n == 0)
54        {
55                return traits_type::eof();
56        }
57
58        // Set buffer pointers
59        setg(base, start, start + n);
60
61        return traits_type::to_int_type(*gptr());
62}
Note: See TracBrowser for help on using the repository browser.