Commit 167c54a2 authored by Jakub Klinkovský's avatar Jakub Klinkovský Committed by Jakub Klinkovský
Browse files

Added File::ignore

parent 25975561
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -140,6 +140,18 @@ public:
   void
   save( const Type* buffer, std::streamsize elements = 1 );

   /**
    * \brief Extracts and discards characters from the file.
    *
    * Throws \ref std::ios_base::failure on failure.
    *
    * \tparam SourceType type of data stored on the file,
    * \param elements number of elements to be read and ignored.
    */
   template< typename SourceType >
   void
   ignore( std::streamsize elements = 1 );

protected:
   // implementation for all allocators which allocate data accessible from host
   template< typename Type,
+9 −0
Original line number Diff line number Diff line
@@ -234,6 +234,15 @@ File::save_impl( const Type* buffer, std::streamsize elements )
#endif
}

template< typename SourceType >
void
File::ignore( std::streamsize elements )
{
   // use seekg instead of ignore for efficiency
   // https://stackoverflow.com/a/31246560
   file.seekg( sizeof( SourceType ) * elements, std::ios_base::cur );
}

inline bool
fileExists( const String& fileName )
{
+4 −1
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ TEST( FileTest, WriteAndRead )
   File file;
   ASSERT_NO_THROW( file.open( String( TEST_FILE_NAME ), std::ios_base::out ) );

   int intData( 5 );
   int intData = 5;
   double doubleData[ 3 ] = { 1.0, 2.0, 3.0 };
   const int ignoredValue = 42;
   const double constDoubleData = 3.14;
   ASSERT_NO_THROW( file.save( &intData ) );
   ASSERT_NO_THROW( file.save( doubleData, 3 ) );
   ASSERT_NO_THROW( file.save( &ignoredValue ) );
   ASSERT_NO_THROW( file.save( &constDoubleData ) );
   ASSERT_NO_THROW( file.close() );

@@ -32,6 +34,7 @@ TEST( FileTest, WriteAndRead )
   double newConstDoubleData;
   ASSERT_NO_THROW( file.load( &newIntData, 1 ) );
   ASSERT_NO_THROW( file.load( newDoubleData, 3 ) );
   ASSERT_NO_THROW( file.ignore< decltype(ignoredValue) >( 1 ) );
   ASSERT_NO_THROW( file.load( &newConstDoubleData, 1 ) );

   EXPECT_EQ( newIntData, intData );