source: trunk/src/io/c_stream.cc @ 406

Last change on this file since 406 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 1.9 KB
RevLine 
[395]1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
[124]6
[376]7#include "nv/io/c_stream.hh"
[124]8#include <cstdio>
9#include <sys/stat.h>
10
11using namespace nv;
12
13c_stream::c_stream()
14        : m_file( nullptr )
15        , m_file_name( "" )
16        , m_file_size( size_t(-1) )
17{
18
19}
20
21c_stream::c_stream( void* pfile, const char* filename )
[125]22        : m_file( pfile )
[124]23        , m_file_name( filename )
24        , m_file_size( size_t(-1) )
25{
26
27}
28
29c_stream::~c_stream()
30{
31        if ( m_file )
32        {
[406]33                ::fclose( reinterpret_cast<FILE*>( m_file ) );
[124]34        }
35}
36
[376]37nv::size_t c_stream::read( void* buffer, nv::size_t size, nv::size_t count )
[124]38{
39        NV_ASSERT( buffer != nullptr && size != 0 && count != 0, "Bad parameter passed to read!" );
[406]40        return m_file ? ::fread( buffer, size, count, reinterpret_cast<FILE*>( m_file ) ) : 0;
[124]41}
42
[376]43nv::size_t c_stream::write( const void* buffer, nv::size_t size, nv::size_t count )
[124]44{
45        NV_ASSERT( buffer != nullptr && size != 0 && count != 0, "Bad parameter passed to write!" );
[406]46        return m_file ? ::fwrite( buffer, size, count, reinterpret_cast<FILE*>( m_file ) ) : 0;
[124]47}
48
[198]49bool c_stream::seek( long offset, origin orig )
[124]50{
[406]51        return m_file != nullptr && ( ::fseek( reinterpret_cast<FILE*>( m_file ), offset, static_cast<int>(orig) ) == 0 );
[124]52}
53
[376]54nv::size_t c_stream::tell()
[124]55{
[406]56        return m_file != nullptr ? static_cast< nv::size_t >( ::ftell( reinterpret_cast<FILE*>( m_file ) ) ) : 0;
[124]57}
58
[376]59nv::size_t c_stream::size()
[124]60{
[125]61        if ( m_file == nullptr || m_file_name == nullptr )
[124]62        {
63                return 0;
64        }
65
66        if ( m_file_size == size_t(-1) )
67        {
68                struct stat fstat;
[125]69                int result = stat(m_file_name, &fstat );
[124]70                if ( result != 0 )
71                {
72                        return 0;
73                }
[406]74                m_file_size = static_cast<size_t>(fstat.st_size);               
[124]75        }
76
77        return m_file_size;
78}
79
80void c_stream::flush()
81{
82        if ( m_file != nullptr )
83        {
[406]84                ::fflush( reinterpret_cast<FILE*>( m_file ) );
[124]85        }
86}
Note: See TracBrowser for help on using the repository browser.