[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 |
|
---|
| 11 | using namespace nv;
|
---|
| 12 |
|
---|
| 13 | c_stream::c_stream()
|
---|
| 14 | : m_file( nullptr )
|
---|
| 15 | , m_file_name( "" )
|
---|
| 16 | , m_file_size( size_t(-1) )
|
---|
| 17 | {
|
---|
| 18 |
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | c_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 |
|
---|
| 29 | c_stream::~c_stream()
|
---|
| 30 | {
|
---|
| 31 | if ( m_file )
|
---|
| 32 | {
|
---|
[406] | 33 | ::fclose( reinterpret_cast<FILE*>( m_file ) );
|
---|
[124] | 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[376] | 37 | nv::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] | 43 | nv::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 |
|
---|
[442] | 49 | bool c_stream::gets( char* buffer, size_t max_count )
|
---|
| 50 | {
|
---|
| 51 | NV_ASSERT( buffer != nullptr && max_count != 0, "Bad parameter passed to write!" );
|
---|
[487] | 52 | char* result = ::fgets( buffer, static_cast<int>( max_count ), reinterpret_cast<FILE*>( m_file ) );
|
---|
[442] | 53 | if ( !result ) return false;
|
---|
| 54 | return true;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[198] | 57 | bool c_stream::seek( long offset, origin orig )
|
---|
[124] | 58 | {
|
---|
[406] | 59 | return m_file != nullptr && ( ::fseek( reinterpret_cast<FILE*>( m_file ), offset, static_cast<int>(orig) ) == 0 );
|
---|
[124] | 60 | }
|
---|
| 61 |
|
---|
[376] | 62 | nv::size_t c_stream::tell()
|
---|
[124] | 63 | {
|
---|
[406] | 64 | return m_file != nullptr ? static_cast< nv::size_t >( ::ftell( reinterpret_cast<FILE*>( m_file ) ) ) : 0;
|
---|
[124] | 65 | }
|
---|
| 66 |
|
---|
[376] | 67 | nv::size_t c_stream::size()
|
---|
[124] | 68 | {
|
---|
[125] | 69 | if ( m_file == nullptr || m_file_name == nullptr )
|
---|
[124] | 70 | {
|
---|
| 71 | return 0;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if ( m_file_size == size_t(-1) )
|
---|
| 75 | {
|
---|
| 76 | struct stat fstat;
|
---|
[125] | 77 | int result = stat(m_file_name, &fstat );
|
---|
[124] | 78 | if ( result != 0 )
|
---|
| 79 | {
|
---|
| 80 | return 0;
|
---|
| 81 | }
|
---|
[406] | 82 | m_file_size = static_cast<size_t>(fstat.st_size);
|
---|
[124] | 83 | }
|
---|
| 84 |
|
---|
| 85 | return m_file_size;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void c_stream::flush()
|
---|
| 89 | {
|
---|
| 90 | if ( m_file != nullptr )
|
---|
| 91 | {
|
---|
[406] | 92 | ::fflush( reinterpret_cast<FILE*>( m_file ) );
|
---|
[124] | 93 | }
|
---|
| 94 | }
|
---|
[484] | 95 |
|
---|
| 96 | bool nv::c_stream::eof()
|
---|
| 97 | {
|
---|
| 98 | if ( m_file != nullptr )
|
---|
| 99 | {
|
---|
[486] | 100 | return ::feof( reinterpret_cast<FILE*>( m_file ) ) != 0;
|
---|
[484] | 101 | }
|
---|
[486] | 102 | return true;
|
---|
[484] | 103 | }
|
---|