fileio.hpp File Reference
#include <string>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "stringtools.hpp"
#include "../exception/exception.hpp"
Include dependency graph for fileio.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

union  NRLib::NRLibPrivate::FloatAsInt
 
union  NRLib::NRLibPrivate::DoubleAsLongLong
 

Namespaces

namespace  NRLib
 
namespace  NRLib::NRLibPrivate
 

Enumerations

enum  NRLib::Endianess { NRLib::END_LITTLE_ENDIAN , NRLib::END_BIG_ENDIAN }
 
enum  NRLib::GridFileFormat {
  NRLib::UNKNOWN = -1 , NRLib::STORM_PETRO_BINARY = 0 , NRLib::STORM_PETRO_ASCII = 1 , NRLib::STORM_FACIES_BINARY = 2 ,
  NRLib::STORM_FACIES_ASCII = 3 , NRLib::SGRI = 4 , NRLib::SEGY = 5 , NRLib::PLAIN_ASCII = 6
}
 

Functions

void NRLib::OpenRead (std::ifstream &stream, const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
 Open file for reading. More...
 
void NRLib::OpenRead (std::fstream &stream, const std::string &filename, std::ios_base::openmode mode=std::ios_base::in)
 
void NRLib::OpenWrite (std::ofstream &stream, const std::string &filename, std::ios_base::openmode mode=std::ios_base::out, bool create_dir=true)
 Open file for writing. More...
 
void NRLib::OpenWrite (std::fstream &stream, const std::string &filename, std::ios_base::openmode mode=std::ios_base::out, bool create_dir=true)
 
void NRLib::CopyFile (const std::string &from_path, const std::string &to_path, bool allow_overwrite)
 
bool NRLib::FileExists (const std::string &filename)
 
void NRLib::CreateDirIfNotExists (const std::string &filename)
 
void NRLib::RemoveFile (const std::string &filename)
 
unsigned long long NRLib::FindFileSize (const std::string &filename)
 Finds the size of a file. Throws IOError if file not found. More...
 
int NRLib::FindGridFileType (const std::string &filename)
 Find type of file, for 3D grid files. More...
 
void NRLib::DiscardRestOfLine (std::istream &stream, int &line_num, bool throw_if_non_whitespace)
 Reads and discard all characters until and including next newline. More...
 
std::istream & NRLib::GetNextNonEmptyLine (std::istream &stream, int &line_num, std::string &line)
 Gets the next line that not only contains whitespace. Returns the stream. Stream will be bad if end-of-file is reached. More...
 
std::string NRLib::FindLastNonEmptyLine (std::istream &stream, const std::ios::pos_type &max_line_len=1000)
 Returns last non-empty line in file. Stream will be at end of file. More...
 
void NRLib::SkipComments (std::istream &stream, char comment_token, int &line_num)
 Returns next line that not only contains whitespace, or starts with the given comment token. More...
 
bool NRLib::CheckEndOfFile (std::istream &stream)
 Check if end of file is reached. Discards all whitespace before end of file or next token. More...
 
std::istream & NRLib::ReadNextToken (std::istream &stream, std::string &s, int &line)
 Read next white-space seperated token from file. Updates line number when new line is read in. If end of file is reached the state of stream is set to eof. If ReadNextToken is called with a stream that is not good, the state is set to fail. More...
 
template<typename T >
NRLib::ReadNext (std::istream &stream, int &line)
 Gets next token from file, and parses it as type T Might be relatively slow, due to typechecking, and counting line numbers. More...
 
void NRLib::ReadNextQuoted (std::istream &stream, char quote, std::string &s, int &line)
 Reads the next, possibly quoted, string. More...
 
template<typename I >
void NRLib::WriteAsciiArray (std::ostream &stream, I begin, I end, int n_per_line=6)
 Writes array. More...
 
template<typename I >
NRLib::ReadAsciiArray (std::istream &stream, I begin, size_t n, int &line)
 Gets sequence with elements of type T from input stream. Might be relatively slow, due to typechecking, and counting line numbers. More...
 
template<typename I >
NRLib::ReadAsciiArrayFast (std::istream &stream, I begin, size_t n)
 Gets sequence with elements of type T from input stream. Does no type checking, and does not count line numbers. More...
 
template<typename I >
NRLib::ReadAsciiArrayFastRestOfFile (std::istream &stream, I begin, size_t n)
 Gets sequence with elements of type T from input stream. Does no type checking, and does not count line numbers. Reads the rest of the file, and does no type checking at all. If a double is attempted read as an int, the first integer part of the double is read, while the fraction part is discarded. For int, float or double this function is 10x as fast as GetAsciiArrayFast on Windows. More...
 
void NRLib::WriteBinaryShort (std::ostream &stream, short s, Endianess number_representation=END_BIG_ENDIAN)
 Write a 2-byte integer to a binary file. More...
 
short NRLib::ReadBinaryShort (std::istream &stream, Endianess number_representation=END_BIG_ENDIAN)
 Read a 2-byte integer from a binary file. More...
 
template<typename I >
void NRLib::WriteBinaryShortArray (std::ostream &stream, I begin, I end, Endianess number_representation=END_BIG_ENDIAN)
 Write an array of binary 2-byte integers. More...
 
template<typename I >
NRLib::ReadBinaryShortArray (std::istream &stream, I begin, size_t n, Endianess number_representation=END_BIG_ENDIAN)
 Read an array of binary 2-byte integers. More...
 
void NRLib::WriteBinaryInt (std::ostream &stream, int i, Endianess number_representation=END_BIG_ENDIAN)
 Write a 4-byte integer to a binary file. More...
 
int NRLib::ReadBinaryInt (std::istream &stream, Endianess number_representation=END_BIG_ENDIAN)
 Read a 4-byte integer from a binary file. More...
 
template<typename I >
void NRLib::WriteBinaryIntArray (std::ostream &stream, I begin, I end, Endianess number_representation=END_BIG_ENDIAN)
 Write an array of binary 4-byte integers. More...
 
template<typename I >
NRLib::ReadBinaryIntArray (std::istream &stream, I begin, size_t n, Endianess number_representation=END_BIG_ENDIAN)
 Read an array of binary 4-byte integers. More...
 
void NRLib::WriteBinaryFloat (std::ostream &stream, float f, Endianess number_representation=END_BIG_ENDIAN)
 Write a 4-byte float on standard IEEE format. More...
 
float NRLib::ReadBinaryFloat (std::istream &stream, Endianess number_representation=END_BIG_ENDIAN)
 Read a 4-byte float on standard IEEE format. More...
 
template<typename I >
void NRLib::WriteBinaryFloatArray (std::ostream &stream, I begin, I end, Endianess number_representation=END_BIG_ENDIAN)
 Write an array of 4-byte floats on standard IEEE format. More...
 
template<typename I >
NRLib::ReadBinaryFloatArray (std::istream &stream, I begin, size_t n, Endianess number_representation=END_BIG_ENDIAN)
 Read an array of 4-byte floats on standard IEEE format. More...
 
void NRLib::WriteBinaryDouble (std::ostream &stream, double d, Endianess number_representation=END_BIG_ENDIAN)
 Write a 8-byte float on standard IEEE format. More...
 
double NRLib::ReadBinaryDouble (std::istream &stream, Endianess number_representation=END_BIG_ENDIAN)
 Read a 8-byte float on standard IEEE format. More...
 
template<typename I >
void NRLib::WriteBinaryDoubleArray (std::ostream &stream, I begin, I end, Endianess number_representation=END_BIG_ENDIAN)
 Write an array of 8-byte floats on standard IEEE format. More...
 
template<typename I >
NRLib::ReadBinaryDoubleArray (std::istream &stream, I begin, size_t n, Endianess number_representation=END_BIG_ENDIAN)
 Read an array of 8-byte floats on standard IEEE format. More...
 
void NRLib::WriteBinaryIbmFloat (std::ostream &stream, float f, Endianess number_representation=END_BIG_ENDIAN)
 Write a 4-byte float on standard IEEE format. More...
 
float NRLib::ReadBinaryIbmFloat (std::istream &stream, Endianess number_representation=END_BIG_ENDIAN)
 Read a 4-byte float on standard IEEE format. More...
 
template<typename I >
void NRLib::WriteBinaryIbmFloatArray (std::ostream &stream, I begin, I end, Endianess number_representation=END_BIG_ENDIAN)
 Write an array of 4-byte floats on standard IEEE format. More...
 
template<typename I >
NRLib::ReadBinaryIbmFloatArray (std::istream &stream, I begin, size_t n, Endianess number_representation=END_BIG_ENDIAN)
 Read an array of 4-byte floats on standard IEEE format. More...
 
bool NRLib::IgnoreComment (std::ifstream &file, char chin)
 
int NRLib::Seek (FILE *file, long long offset, int origin)
 Seek to given position in file, works for large files. More...
 
void NRLib::ParseInt16BE (const char *buffer, short &ui)
 Parse unsigned 32-bit integer from big-endian buffer. More...
 
void NRLib::ParseInt32BE (const char *buffer, int &ui)
 Parse unsigned 32-bit integer from big-endian buffer. More...
 
void NRLib::ParseUInt16BE (const char *buffer, unsigned short &ui)
 Parse unsigned 32-bit integer from big-endian buffer. More...
 
void NRLib::ParseUInt32BE (const char *buffer, unsigned int &ui)
 Parse unsigned 32-bit integer from big-endian buffer. More...
 
void NRLib::ParseIEEEFloatBE (const char *buffer, float &f)
 Parse IEEE single-precision float from big-endian buffer. More...
 
void NRLib::ParseIBMFloatBE (const char *buffer, float &f)
 Parse IEEE double-precision float from big-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseUInt16LE (const char *buffer, unsigned short &ui)
 Parse unsigned 32-bit integer from little-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteUInt16BE (char *buffer, unsigned short us)
 Write unsigned 32-bit integer to big-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteUInt16LE (char *buffer, unsigned short us)
 Write unsigned 32-bit integer to little-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseUInt32LE (const char *buffer, unsigned int &ui)
 Parse unsigned 32-bit integer from little-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteUInt32BE (char *buffer, unsigned int ui)
 Write unsigned 32-bit integer to big-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteUInt32LE (char *buffer, unsigned int ui)
 Write unsigned 32-bit integer to little-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseIEEEFloatLE (const char *buffer, float &f)
 Parse IEEE single-precision float from little-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIEEEFloatBE (char *buffer, float f)
 Write IEEE single-precision float to big-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIEEEFloatLE (char *buffer, float f)
 Write IEEE single-precision float to little-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseIEEEDoubleBE (const char *buffer, double &d)
 Parse IEEE double-precision float from big-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseIEEEDoubleLE (const char *buffer, double &d)
 Parse IEEE double-precision float from little-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIEEEDoubleBE (char *buffer, double d)
 Write IEEE double-precision float to big-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIEEEDoubleLE (char *buffer, double d)
 Write IEEE double-precision float to little-endian buffer. More...
 
void NRLib::NRLibPrivate::ParseIBMFloatLE (const char *buffer, float &f)
 Parse IEEE double-precision float from little-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIBMFloatBE (char *buffer, float f)
 Write IEEE double-precision float to big-endian buffer. More...
 
void NRLib::NRLibPrivate::WriteIBMFloatLE (char *buffer, float f)
 Write IEEE double-precision float to little-endian buffer. More...
 
template<>
std::string NRLib::ReadNext< std::string > (std::istream &stream, int &line)
 
static void Ibm2Ieee (unsigned int &in)
 Converts Ieee float (represented as 32-bit int) to IBM float. More...
 
static void Ieee2Ibm (unsigned int &in)
 Converts Ieee float (represented as 32-bit int) to IBM float. More...
 

Variables

static const unsigned short smasks [2] = {0x00ff, 0xff00}
 
static const unsigned int masks [4]
 
static const unsigned long long lmasks [8]
 
static unsigned int IEEEMAX = 0x7FFFFFFF
 
static unsigned int IEMAXIB = 0x611FFFFF
 
static unsigned int IEMINIB = 0x21200000
 

Function Documentation

◆ Ibm2Ieee()

static void Ibm2Ieee ( unsigned int &  in)
inlinestatic

Converts Ieee float (represented as 32-bit int) to IBM float.

References IEEEMAX, IEMAXIB, and IEMINIB.

Referenced by NRLib::ParseIBMFloatBE(), and NRLib::NRLibPrivate::ParseIBMFloatLE().

◆ Ieee2Ibm()

static void Ieee2Ibm ( unsigned int &  in)
inlinestatic

Converts Ieee float (represented as 32-bit int) to IBM float.

Referenced by NRLib::NRLibPrivate::WriteIBMFloatBE(), and NRLib::NRLibPrivate::WriteIBMFloatLE().

Variable Documentation

◆ IEEEMAX

unsigned int IEEEMAX = 0x7FFFFFFF
static

Referenced by Ibm2Ieee().

◆ IEMAXIB

unsigned int IEMAXIB = 0x611FFFFF
static

Referenced by Ibm2Ieee().

◆ IEMINIB

unsigned int IEMINIB = 0x21200000
static

Referenced by Ibm2Ieee().

◆ lmasks

const unsigned long long lmasks[8]
static
Initial value:
=
{0x00000000000000ffULL, 0x000000000000ff00ULL,
0x0000000000ff0000ULL, 0x00000000ff000000ULL,
0x000000ff00000000ULL, 0x0000ff0000000000ULL,
0x00ff000000000000ULL, 0xff00000000000000ULL}

Referenced by NRLib::NRLibPrivate::WriteIEEEDoubleBE(), and NRLib::NRLibPrivate::WriteIEEEDoubleLE().

◆ masks

◆ smasks

const unsigned short smasks[2] = {0x00ff, 0xff00}
static