Commit 2c8f1be7 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

IOMode replaced with File::Mode.

parent 26562c2c
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -9,16 +9,16 @@ int main()
{
    File file;

    file.open( String("new-file.tnl"), IOMode::write );
    String title("Header");
    file.write( &title );
    file.open( String("new-file.tnl"), File::Mode::Out );
    String title("'string to file'");
    file << title;
    file.close();

    file.open( String("new-file.tnl"), IOMode::read );
    String title2;
    file.read( &title2, 4);
    file.open( String("new-file.tnl"), File::Mode::In );
    String restoredString;
    file >> restoredString;
    file.close();

    cout << "title2:" << title2 <<endl;
    cout << "restored string = " << restoredString <<endl;
}
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ int main(int argc, char* argv[]) {

	String inputFile = parameters.getParameter <String> ("input-file");
	File binaryFile;
	if(! binaryFile.open(inputFile, IOMode::read)) {
	if(! binaryFile.open(inputFile, File::Mode::In)) {
		cerr << "I am not able to open the file " << inputFile << "." << std::endl;
		return 1;
	}
+14 −28
Original line number Diff line number Diff line
@@ -20,25 +20,15 @@

namespace TNL {

/// \brief Supported modes for opening \ref TNL::File "files".
enum class IOMode
{
//   undefined = 0,
   read = 1,
   write = 2
};

/**
 * When we need to transfer data between the GPU and the CPU we use
 * 5 MB buffer. This size should ensure good performance -- see.
 * When we transfer data between the GPU and the CPU we use 5 MB buffer. This
 * size should ensure good performance -- see.
 * http://wiki.accelereyes.com/wiki/index.php/GPU_Memory_Transfer
 * Similar constant is defined in tnlLonegVectorCUDA
 */
static constexpr std::streamsize FileGPUvsCPUTransferBufferSize = 5 * 2<<20;


/**
 * \brief Class file is aimed mainly for saving and loading binary data.
 * \brief This class serves for binary IO. It allows to do IO even for data allocated on GPU
 *
 * \par Example
 * \include FileExample.cpp
@@ -49,11 +39,16 @@ class File
{
   public:

      /**
       * This enum defines mode for opening files.
       */
      enum class Mode
      {
         In = 1,
         Out = 2,
         Append = 4
         In = 1,       ///< Open for input.
         Out = 2,      ///< Open for output.
         Append = 4,   ///< Output operations are appended at the end of file.
         AtEnd = 8,    ///< Set the initial position at the end.
         Truncate = 16 ///< If the file is opened for ouptput, its previous content is deleted.
      };
      
      /**
@@ -62,7 +57,7 @@ class File
      File() = default;

      /**
       * \brief Attempts to open given file and returns \e true after the file is
       * \brief Open given file and returns \e true after the file is
       * successfully opened. Otherwise returns \e false.
       *
       * Opens file with given \e fileName and returns true/false based on the success in opening the file.
@@ -70,7 +65,7 @@ class File
       * \param mode Indicates what user needs to do with opened file.
       */
      bool open( const String& fileName,
                 const IOMode mode );
                 Mode mode = static_cast< Mode >( static_cast< int >( Mode::In ) | static_cast< int >( Mode::Out ) ) );

      /**
       * \brief Attempts to close given file and returns \e true when the file is
@@ -102,10 +97,6 @@ class File
      template< typename Type, typename Device = Devices::Host >
      bool read( Type* buffer, std::streamsize elements = 1 );

      // Toto je treba??
      //template< typename Type, typename Device = Devices::Host >
      //bool read( Type* buffer );

      /**
       * \brief Method that can write particular data type from CPU into given file. (Function that writes particular elements into given file.)
       *
@@ -122,10 +113,6 @@ class File
      template< typename Type, typename Device = Devices::Host >
      bool write( const Type* buffer, std::streamsize elements = 1 );

      // Toto je treba?
      //template< typename Type, typename Device = Devices::Host >
      //bool write( const Type* buffer );

   protected:
      template< typename Type,
                typename Device,
@@ -184,7 +171,6 @@ File& operator<<( File& file, const std::string& str );
 * \brief Deserialization of strings.
 */
File& operator>>( File& file, std::string& str );

} // namespace TNL

#include <TNL/File.hpp>
+25 −15
Original line number Diff line number Diff line
@@ -22,17 +22,29 @@

namespace TNL {

inline bool File::open( const String& fileName, const IOMode mode )
inline File::Mode operator|( File::Mode m1, File::Mode m2 );

inline bool operator&( File::Mode m1, File::Mode m2 );

inline bool File::open( const String& fileName, Mode mode )
{
   // enable exceptions
   file.exceptions( std::fstream::failbit | std::fstream::badbit | std::fstream::eofbit );

   close();

   if( mode == IOMode::read )
   auto ios_mode = std::ios::binary;
   if( mode & Mode::In ) ios_mode |= std::ios::in;
   if( mode & Mode::Out ) ios_mode |= std::ios::out;
   if( mode & Mode::Append ) ios_mode |= std::ios::app;
   if( mode & Mode::AtEnd ) ios_mode |= std::ios::ate;
   if( mode & Mode::Truncate ) ios_mode |= std::ios::trunc;
   file.open( fileName.getString(), ios_mode );
   
   /*if( mode == Mode::In )
      file.open( fileName.getString(), std::ios::binary | std::ios::in );
   else
      file.open( fileName.getString(), std::ios::binary | std::ios::out );
      file.open( fileName.getString(), std::ios::binary | std::ios::out );*/

   this->fileName = fileName;
   if( ! file.good() ) {
@@ -56,18 +68,6 @@ inline bool File::close()
   return true;
}

/*template< typename Type, typename Device >
bool File::read( Type* buffer )
{
   return read< Type, Device >( buffer, 1 );
}*/

/*template< typename Type, typename Device >
bool File::write( const Type* buffer )
{
   return write< Type, Device >( buffer, 1 );
}*/

template< typename Type, typename Device >
bool File::read( Type* buffer, std::streamsize elements )
{
@@ -274,4 +274,14 @@ inline File& operator>>( File& file, std::string& str )
   return file;
}

inline File::Mode operator|( File::Mode m1, File::Mode m2 )
{
   return static_cast< File::Mode >( static_cast< int >( m1 ) | static_cast< int >( m2 ) );
}

inline bool operator&( File::Mode m1, File::Mode m2 )
{
   return static_cast< bool >( static_cast< int >( m1 ) & static_cast< int >( m2 ) );
}

} // namespace TNL
+3 −3
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ class DistributedGridIO<
         newMesh->setOrigin(origin+TNL::Containers::Scale(spaceSteps,localBegin));

         File meshFile;
         if( ! meshFile.open( fileName+String("-mesh-")+distrGrid->printProcessCoords()+String(".tnl"),IOMode::write) )
         if( ! meshFile.open( fileName+String("-mesh-")+distrGrid->printProcessCoords()+String(".tnl"),File::Mode::Out ) )
         {
            std::cerr << "Failed to open mesh file for writing." << std::endl;
            return false;
@@ -84,7 +84,7 @@ class DistributedGridIO<
         CopyEntitiesHelper<MeshFunctionType>::Copy(meshFunction,newMeshFunction,localBegin,zeroCoord,localSize);

         File file;
         if( ! file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), IOMode::write ) )
         if( ! file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), File::Mode::Out ) )
         {
            std::cerr << "Failed to open file for writing." << std::endl;
            return false;
@@ -126,7 +126,7 @@ class DistributedGridIO<
        zeroCoord.setValue(0);        

        File file;
        if( ! file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), IOMode::read ) )
        if( ! file.open( fileName+String("-")+distrGrid->printProcessCoords()+String(".tnl"), File::Mode::In ) )
        {
            std::cerr << "Failed to open file for reading." << std::endl;
            return false;
Loading