(#434) Added NRLib for LAS support. Created CMake file to build it.

Readme.txt describes how to update the source code contained in NRLib.
This commit is contained in:
Pål Hagen
2015-09-11 16:32:38 +02:00
parent 69019115a2
commit a9027ecefa
61 changed files with 18204 additions and 0 deletions

View File

@@ -0,0 +1,733 @@
//$Id: fileio.cpp 1068 2012-09-18 11:21:53Z perroe $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "fileio.hpp"
#include "../exception/exception.hpp"
#include "stringtools.hpp"
#define BOOST_FILESYSTEM_VERSION 2
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
using namespace NRLib::NRLibPrivate;
using namespace NRLib;
const std::string format_desc[5] = {"storm_petro_binary",
"storm_petro_ascii",
"storm_facies_binary",
"storm_facies_ascii",
"NORSAR"};
void NRLib::OpenRead(std::ifstream& stream,
const std::string& filename,
std::ios_base::openmode mode)
{
namespace fs = boost::filesystem;
try {
boost::filesystem::path file_path(filename);
if (!fs::exists(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for reading: " +
"File does not exist.");
}
if (fs::is_directory(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for reading: " +
" It is a directory.");
}
stream.open(file_path.file_string().c_str(), mode);
if (!stream) {
throw IOError("Failed to open " + file_path.file_string() + " for reading.");
}
}
catch (fs::filesystem_error& e) {
throw IOError("Failed to open " + filename + " for reading: " + e.what());
}
}
void NRLib::OpenRead(std::fstream& stream,
const std::string& filename,
std::ios_base::openmode mode)
{
namespace fs = boost::filesystem;
try {
boost::filesystem::path file_path(filename);
if (!fs::exists(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for reading: " +
"File does not exist.");
}
if (fs::is_directory(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for reading: " +
" It is a directory.");
}
stream.open(file_path.file_string().c_str(), mode);
if (!stream) {
throw IOError("Failed to open " + file_path.file_string() + " for reading.");
}
}
catch (fs::filesystem_error& e) {
throw IOError("Failed to open " + filename + " for reading: " + e.what());
}
}
void NRLib::OpenWrite(std::ofstream& stream,
const std::string& filename,
std::ios_base::openmode mode,
bool create_dir)
{
namespace fs = boost::filesystem;
try {
fs::path file_path(filename);
fs::path dir = file_path.parent_path();
if (fs::is_directory(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for writing: " +
" It is a directory.");
}
if (!dir.empty()) {
if (!fs::exists(dir) && create_dir) {
create_directories(dir);
}
else if (!fs::exists(dir)) {
throw IOError("Failed to open " + file_path.file_string() + " for writing: "
+ "Parent directory does not exist.");
}
}
stream.open(file_path.file_string().c_str(), mode);
if (!stream) {
throw IOError("Failed to open " + file_path.file_string() + " for writing.");
}
}
catch (fs::filesystem_error& e) {
throw IOError("Failed to open " + filename + " for writing: " + e.what());
}
}
void NRLib::OpenWrite(std::fstream& stream,
const std::string& filename,
std::ios_base::openmode mode,
bool create_dir)
{
namespace fs = boost::filesystem;
try {
fs::path file_path(filename);
fs::path dir = file_path.parent_path();
if (fs::is_directory(file_path)) {
throw IOError("Failed to open " + file_path.file_string() + " for writing: " +
" It is a directory.");
}
if (!dir.empty()) {
if (!fs::exists(dir) && create_dir) {
create_directories(dir);
}
else if (!fs::exists(dir)) {
throw IOError("Failed to open " + file_path.file_string() + " for writing: "
+ "Parent directory does not exist.");
}
}
stream.open(file_path.file_string().c_str(), mode);
if (!stream) {
throw IOError("Failed to open " + file_path.file_string() + " for writing.");
}
}
catch (fs::filesystem_error& e) {
throw IOError("Failed to open " + filename + " for writing: " + e.what());
}
}
void NRLib::CopyFile(const std::string & from_path,
const std::string & to_path,
bool allow_overwrite)
{
if (allow_overwrite)
RemoveFile(to_path);
else {
if (boost::filesystem::exists(to_path))
throw IOError("Failed to open " + to_path + " for writing: The file already exists.");
}
boost::filesystem::copy_file(from_path, to_path);
}
void NRLib::RemoveFile(const std::string & filename)
{
if (boost::filesystem::exists(filename))
boost::filesystem::remove(filename);
}
bool NRLib::FileExists(const std::string & filename)
{
return boost::filesystem::exists(filename);
}
void NRLib::CreateDirIfNotExists(const std::string & filename)
{
namespace fs = boost::filesystem;
fs::path file_path(filename);
fs::path dir = file_path.parent_path();
if (!dir.empty()){
if (!fs::exists(dir))
create_directories(dir);
}
}
std::istream& NRLib::ReadNextToken(std::istream & stream,
std::string & s,
int & line_num)
{
std::locale loc = std::locale();
char c;
if (!stream.good()) {
stream.setstate(std::ifstream::failbit);
s = "";
return stream;
}
while(stream.get(c) && std::isspace(c,loc) == true ) {
if (c == '\n')
line_num++;
}
if (stream.good()) {
s = c;
while(stream.get(c) && std::isspace(c, loc) == false) {
s = s+c;
}
}
if (stream.good() == true) // Do not insert anything at eof.
stream.putback(c); // Put back to make sure that line-numbers are OK.
return stream;
}
void NRLib::DiscardRestOfLine(std::istream& stream,
int& line_num,
bool throw_if_non_whitespace)
{
std::locale loc;
std::string s;
std::getline(stream, s);
line_num++;
if (throw_if_non_whitespace) {
std::string::iterator it = s.begin();
while(it != s.end() && std::isspace(*it, loc))
++it;
if (it != s.end())
throw IOError("Non-whitespace characters encountered.");
}
}
std::istream& NRLib::GetNextNonEmptyLine(std::istream & stream,
int & line_num,
std::string & line)
{
while (std::getline(stream, line)) {
++line_num;
if (line.find_first_not_of(Whitespace()) != std::string::npos) {
return stream;
}
}
line = "";
return stream;
}
std::string NRLib::FindLastNonEmptyLine(std::istream & stream, const std::ios::pos_type & max_line_len)
{
stream.seekg(0, std::ios::end);
std::ios::pos_type file_len = stream.tellg();
std::ios::pos_type offset = std::min(file_len, max_line_len);
stream.seekg(static_cast<std::ios::off_type>(file_len) - offset);
std::string line = "";
std::string last_line = "";
int dummy = 0;
while (GetNextNonEmptyLine(stream, dummy, line))
{
last_line = line;
}
return last_line;
}
void NRLib::SkipComments(std::istream & stream,
char comment_symbol,
int & line_num)
{
std::locale loc = std::locale();
std::string line;
bool comment = true;
while (comment == true && stream.good()) {
char c;
while(stream.get(c) && std::isspace(c, loc)) {
if (c == '\n')
line_num++;
}
if (c == comment_symbol) {
std::getline(stream, line);
line_num++;
}
else {
stream.unget();
comment = false;
}
}
}
bool NRLib::CheckEndOfFile(std::istream& stream)
{
char c;
stream >> c;
if (stream.eof()) {
return true;
}
else {
// printf("end of file? %s\n", c);
stream.putback(c);
return false;
}
}
unsigned long long
NRLib::FindFileSize(const std::string& filename)
{
if ( !boost::filesystem::exists(filename) ) {
throw IOError("File " + filename + " does not exist.");
}
return static_cast<unsigned long long>(boost::filesystem::file_size(filename));
}
int NRLib::FindGridFileType(const std::string& filename )
{
unsigned long long length = FindFileSize(filename);
std::ifstream file(filename.c_str(), std::ios::in | std::ios::binary);
if (!file) {
throw IOError("Error opening " + filename);
}
char buffer[3201];
if (length > 161) {
file.read(buffer, 3200);
buffer[3200] = '\0';
}
else {
file.read(buffer, static_cast<std::streamsize>(length));
buffer[length] = '\0';
}
std::string stringbuffer = std::string(buffer);
std::istringstream i(stringbuffer);
std::string token;
i>>token;
if (token == format_desc[STORM_PETRO_BINARY]) {
return STORM_PETRO_BINARY;
}
else if (token == format_desc[STORM_PETRO_ASCII]) {
return STORM_PETRO_ASCII;
}
else if (token == format_desc[STORM_FACIES_BINARY]) {
return STORM_FACIES_BINARY;
}
else if (token == format_desc[STORM_FACIES_ASCII]) {
return STORM_FACIES_ASCII;
}
else if (token == format_desc[SGRI]) {
//std::getline(i, token);
i>>token;
i>>token;
i>>token;
i>>token;
if(token == "v1.0" ||token == "v2.0" )
return SGRI;
}
else if (NRLib::IsType<double>(token)) {
return PLAIN_ASCII;
}
else if (length>3200) //Check SEGY: Looking for EBCDIC header.
{
unsigned char * buf = reinterpret_cast<unsigned char *>(buffer);
std::vector<int> frequency(256,0);
int i;
for(i=0;i<3200;i++)
frequency[buf[i]]++;
bool segy_ok = true;
int low_count = 0;
for(i=0;i<64;i++)
low_count += frequency[i];
if(low_count > 5)
segy_ok = false;
i++;
for(;(i<256) && (segy_ok == true);i++) {
if(frequency[i] > frequency[64]) //Assumption is that EBCDIC 64 (space) is most common in header.
segy_ok = false;
}
if(segy_ok == true)
{
return SEGY;
}
}
return(UNKNOWN);
}
void NRLib::WriteBinaryShort(std::ostream& stream,
short s,
Endianess file_format)
{
char buffer[2];
switch (file_format) {
case END_BIG_ENDIAN:
WriteUInt16BE(buffer, static_cast<unsigned short>(s));
break;
case END_LITTLE_ENDIAN:
WriteUInt16LE(buffer, static_cast<unsigned short>(s));
break;
default:
throw Exception("Invalid file format.");
}
if (!stream.write(buffer, 2)) {
throw Exception("Error writing to stream.");
}
}
short NRLib::ReadBinaryShort(std::istream& stream,
Endianess file_format)
{
unsigned short us;
char buffer[2];
if (!stream.read(buffer, 2)) {
if(stream.eof())
throw EndOfFile();
else
throw Exception("Error reading from stream (a).");
}
switch (file_format) {
case END_BIG_ENDIAN:
ParseUInt16BE(buffer, us);
break;
case END_LITTLE_ENDIAN:
ParseUInt16LE(buffer, us);
break;
default:
throw Exception("Invalid file format.");
}
return static_cast<short>(us);
}
void NRLib::WriteBinaryInt(std::ostream& stream,
int i,
Endianess file_format)
{
char buffer[4];
switch (file_format) {
case END_BIG_ENDIAN:
WriteUInt32BE(buffer, static_cast<unsigned int>(i));
break;
case END_LITTLE_ENDIAN:
WriteUInt32LE(buffer, static_cast<unsigned int>(i));
break;
default:
throw Exception("Invalid file format.");
}
if (!stream.write(buffer, 4)) {
throw Exception("Error writing to stream.");
}
}
int NRLib::ReadBinaryInt(std::istream& stream,
Endianess file_format)
{
unsigned int ui;
char buffer[4];
if (!stream.read(buffer, 4)) {
if(stream.eof())
throw EndOfFile();
else
throw Exception("Error reading from stream (b).");
}
switch (file_format) {
case END_BIG_ENDIAN:
ParseUInt32BE(buffer, ui);
break;
case END_LITTLE_ENDIAN:
ParseUInt32LE(buffer, ui);
break;
default:
throw Exception("Invalid file format.");
}
return static_cast<int>(ui);
}
void NRLib::WriteBinaryFloat(std::ostream& stream,
float f,
Endianess file_format)
{
char buffer[4];
switch (file_format) {
case END_BIG_ENDIAN:
WriteIEEEFloatBE(buffer, f);
break;
case END_LITTLE_ENDIAN:
WriteIEEEFloatLE(buffer, f);
break;
default:
throw Exception("Invalid file format.");
}
if (!stream.write(buffer, 4)) {
throw Exception("Error writing to stream.");
}
}
float NRLib::ReadBinaryFloat(std::istream& stream,
Endianess file_format)
{
float f;
char buffer[4];
if (!stream.read(buffer, 4)) {
if(stream.eof())
throw EndOfFile();
else
throw Exception("Error reading from stream (c).");
}
switch (file_format) {
case END_BIG_ENDIAN:
ParseIEEEFloatBE(buffer, f);
break;
case END_LITTLE_ENDIAN:
ParseIEEEFloatLE(buffer, f);
break;
default:
throw Exception("Invalid file format.");
}
return f;
}
void NRLib::WriteBinaryDouble(std::ostream& stream,
double d,
Endianess file_format)
{
char buffer[8];
switch (file_format) {
case END_BIG_ENDIAN:
WriteIEEEDoubleBE(buffer, d);
break;
case END_LITTLE_ENDIAN:
WriteIEEEDoubleLE(buffer, d);
break;
default:
throw Exception("Invalid file format.");
}
if (!stream.write(buffer, 8)) {
throw Exception("Error writing to stream.");
}
}
double NRLib::ReadBinaryDouble(std::istream& stream,
Endianess file_format)
{
double d;
char buffer[8];
if (!stream.read(buffer, 8)) {
if(stream.eof())
throw EndOfFile();
else
throw Exception("Error reading from stream (d).");
}
switch (file_format) {
case END_BIG_ENDIAN:
ParseIEEEDoubleBE(buffer, d);
break;
case END_LITTLE_ENDIAN:
ParseIEEEDoubleLE(buffer, d);
break;
default:
throw Exception("Invalid file format.");
}
return d;
}
void NRLib::WriteBinaryIbmFloat(std::ostream& stream,
float f,
Endianess file_format)
{
char buffer[4];
switch (file_format) {
case END_BIG_ENDIAN:
WriteIBMFloatBE(buffer, f);
break;
case END_LITTLE_ENDIAN:
WriteIBMFloatLE(buffer, f);
break;
default:
throw Exception("Invalid file format.");
}
if (!stream.write(buffer, 4)) {
throw Exception("Error writing to stream.");
}
}
float NRLib::ReadBinaryIbmFloat(std::istream& stream,
Endianess file_format)
{
float f;
char buffer[4];
if (!stream.read(buffer, 4)) {
if(stream.eof())
throw EndOfFile();
else
throw Exception("Error reading from stream (e).");
}
switch (file_format) {
case END_BIG_ENDIAN:
ParseIBMFloatBE(buffer, f);
break;
case END_LITTLE_ENDIAN:
ParseIBMFloatLE(buffer, f);
break;
default:
throw Exception("Invalid file format.");
}
return f;
}
void NRLib::ReadNextQuoted(std::istream& stream, char quote, std::string& s, int& line)
{
char c = 0;
int found = 0;
while( found == 0 && !stream.eof() ){
stream.get(c);
if (c == '\n'){
line++;
}
if (c == quote){
found++;
stream.get(c);
}
}
s ="";
while( found == 1 && !stream.eof() ) {
s = s+c;
stream.get(c);
if (c == '\n'){
line++;
}
if (c == quote){
found++;
}
}
if (s == "") //We are at end of file if nothing is read.
throw EndOfFile();
}
bool
NRLib::IgnoreComment(std::ifstream& file, char chin){
char ch;
do {
if(!file.get(ch)) {
throw Exception("Unexpected end of file.");
// NRLib::LogKit::LogMessage(NRLib::LogKit::Error, "Unexpected end of file. \n\n");
// Havana::Exit(EXIT_FAILURE);
}
} while(isspace(ch));
std::string dummy;
if (ch == chin){
getline(file, dummy);
return true;
}
else {
file.unget();
return false;
}
}
int NRLib::Seek(FILE * file, long long offset, int origin)
{
int ret;
#if defined(_MSC_VER)
ret = _fseeki64(file, offset, origin);
#else
ret = fseek(file, offset, origin);
#endif
return(ret);
}

1198
ThirdParty/NRLib/nrlib/iotools/fileio.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,299 @@
// $Id: logkit.cpp 1127 2012-12-04 12:54:33Z ulvmoen $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// • Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// • Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <fstream>
#include <iostream>
#include <stdarg.h>
#include "logkit.hpp"
#include "../exception/exception.hpp"
using namespace NRLib;
std::vector<LogStream*> LogKit::logstreams_(0);
int LogKit::screenLog_ = -1;
std::vector<BufferMessage *> * LogKit::buffer_ = NULL;
// Making a long table to allow direct access.
std::vector<int> LogKit::n_messages_(65, 0);
std::vector<std::string> LogKit::prefix_(65, "");
void
LogKit::SetFileLog(const std::string & fileName, int levels,
bool includeNRLibLogging)
{
std::ofstream * file = new std::ofstream(fileName.c_str());
if (!(*file)) {
//NBNB-PAL: Tmp fix pfga. manglende feilhåndtering (catch)
delete file;
printf("Could not open file %s\n",fileName.c_str());
throw IOError("Error opening " + fileName);
}
LogStream * curStream;
if (includeNRLibLogging == true)
curStream = new LogStream(file, levels);
else {
std::vector<int> phaseLevels;
phaseLevels.push_back(0); //No logging in NRLib, phase 0.
phaseLevels.push_back(levels); //This will be used for all other phases.
curStream = new LogStream(file, phaseLevels);
}
logstreams_.push_back(curStream);
DumpBuffer(curStream);
}
void
LogKit::SetFileLog(const std::string & fileName, const std::vector<int> & levels, bool ignore_general) {
std::ofstream * file = new std::ofstream(fileName.c_str());
if (!(*file)) {
delete file;
throw IOError("Error opening " + fileName);
}
LogStream * curStream = new LogStream(file, levels, ignore_general);
logstreams_.push_back(curStream);
DumpBuffer(curStream);
}
void
LogKit::SetFileLog(const std::string & fileName, int levels, int phase, bool ignore_general) {
std::ofstream * file = new std::ofstream(fileName.c_str());
if (!(*file)) {
delete file;
throw IOError("Error opening " + fileName);
}
std::vector<int> phaseLevels(1000,0);
phaseLevels[phase] = levels;
LogStream * curStream = new LogStream(file, phaseLevels, ignore_general);
logstreams_.push_back(curStream);
DumpBuffer(curStream);
}
void
LogKit::SetScreenLog(int levels, bool includeNRLibLogging)
{
LogStream * curStream;
if (includeNRLibLogging == true)
curStream = new LogStream(NULL, levels);
else {
std::vector<int> phaseLevels;
phaseLevels.push_back(0); //No logging in NRLib, phase 0.
phaseLevels.push_back(levels); //This will be used for all other phases.
curStream = new LogStream(NULL, phaseLevels);
}
if (screenLog_ < 0) {
screenLog_ = static_cast<int>(logstreams_.size());
logstreams_.push_back(curStream);
}
else {
delete logstreams_[screenLog_];
logstreams_[screenLog_] = curStream;
}
}
void
LogKit::SetScreenLog(const std::vector<int> & levels, bool ignore_general) {
LogStream * curStream = new LogStream(NULL, levels, ignore_general);
if (screenLog_ < 0) {
screenLog_ = static_cast<int>(logstreams_.size());
logstreams_.push_back(curStream);
}
else {
delete logstreams_[screenLog_];
logstreams_[screenLog_] = curStream;
}
}
void
LogKit::LogMessage(int level, const std::string & message) {
unsigned int i;
n_messages_[level]++;
std::string new_message = prefix_[level] + message;
for (i=0;i<logstreams_.size();i++)
logstreams_[i]->LogMessage(level, new_message);
SendToBuffer(level,-1,new_message);
}
void
LogKit::LogMessage(int level, int phase, const std::string & message) {
unsigned int i;
n_messages_[level]++;
std::string new_message = prefix_[level] + message;
for (i=0;i<logstreams_.size();i++)
logstreams_[i]->LogMessage(level, phase, new_message);
SendToBuffer(level,phase,new_message);
}
void
LogKit::LogFormatted(int level, std::string format, ...) {
va_list ap;
char message[5000];
va_start(ap, format);
vsprintf(message, format.c_str(), ap);
va_end(ap);
LogMessage(level, std::string(message));
}
void
LogKit::LogFormatted(int level, int phase, std::string format, ...) {
va_list ap;
char message[1000];
va_start(ap, format);
vsprintf(message, format.c_str(), ap);
va_end(ap);
LogMessage(level, phase, std::string(message));
}
void
LogKit::EndLog() {
unsigned int i;
for (i=0;i<logstreams_.size();i++)
delete logstreams_[i];
if (buffer_ != NULL)
EndBuffering(); //Also deletes buffer.
}
void
LogKit::StartBuffering() {
buffer_ = new std::vector<BufferMessage *>;
}
void
LogKit::EndBuffering() {
if (buffer_ != NULL) {
for (unsigned int i=0;i<buffer_->size();i++) {
delete (*buffer_)[i];
(*buffer_)[i] = NULL;
}
delete buffer_;
buffer_ = NULL;
}
}
void
LogKit::SendToBuffer(int level, int phase, const std::string & message) {
if (buffer_ != NULL) {
BufferMessage * bm = new BufferMessage;
bm->level_ = level;
bm->phase_ = phase;
bm->text_ = message;
buffer_->push_back(bm);
}
}
void
LogKit::DumpBuffer(LogStream *logstream) {
if (buffer_ != NULL) {
for (unsigned int i=0;i<buffer_->size();i++) {
if ((*buffer_)[i]->phase_ < 0)
logstream->LogMessage((*buffer_)[i]->level_, (*buffer_)[i]->text_);
else
logstream->LogMessage((*buffer_)[i]->level_, (*buffer_)[i]->phase_, (*buffer_)[i]->text_);
}
}
}
void
LogKit::SetPrefix(const std::string & prefix, int level) {
prefix_[level] = prefix;
}
void
LogKit::WriteHeader(const std::string & text,
MessageLevels logLevel)
{
int width = 100; // Total width of header
std::string ruler(width,'*');
std::string stars("*****");
LogFormatted(logLevel,"\n"+ruler+"\n");
int starLength = int(stars.length());
int textLength = int(text.length());
int blankLength = width - textLength - 2*starLength;
std::string blanks(blankLength/2,' ');
std::string center;
if(blankLength % 2)
center = stars + blanks + text + blanks + " " + stars;
else
center = stars + blanks + text + blanks +stars;
LogFormatted(logLevel,center+"\n");
LogFormatted(logLevel,ruler+"\n");
}
LogStream::LogStream(std::ostream * logstream, int level) {
fullLevel_ = level;
if (logstream != NULL) {
logstream_ = logstream;
deleteStream = true;
}
else {
logstream_ = &(std::cout);
deleteStream = false;
}
}
LogStream::LogStream(std::ostream * logstream, const std::vector<int> & levels, bool ignore_general) {
unsigned int i;
fullLevel_ = 0;
for (i=0;i<levels.size();i++) {
if(ignore_general == false)
fullLevel_ = (fullLevel_ | levels[i]);
levels_.push_back(levels[i]);
}
if (logstream != NULL) {
logstream_ = logstream;
deleteStream = true;
}
else {
logstream_ = &(std::cout);
deleteStream = false;
}
}
LogStream::~LogStream() {
if (deleteStream == true) {
delete logstream_;
}
}
void
LogStream::LogMessage(int level, const std::string & message) {
if ((level & fullLevel_) > 0) {
*logstream_ << message;
logstream_->flush();
}
}
void
LogStream::LogMessage(int level, int phase, const std::string & message) {
if (phase < static_cast<int>(levels_.size())) {
if ((level & levels_[phase]) > 0) {
*logstream_ << message;
logstream_->flush();
}
}
else if ((level & fullLevel_) > 0) {
*logstream_ << message;
logstream_->flush();
}
}

View File

@@ -0,0 +1,145 @@
// $Id: logkit.hpp 1072 2012-09-18 13:53:37Z perroe $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef NRLIB_LOGKIT_H
#define NRLIB_LOGKIT_H
#include<ostream>
#include<vector>
#include<string>
namespace NRLib {
class LogStream;
struct BufferMessage;
/// Kit for logging of messages from program.
class LogKit {
public:
///Philosophy:
///A message has a level, determining the type of message, and a phase,
///determining the stage in the program. Each stream has a level (which may
///be a combination of basic levels) for each phase. If phase and flag
///matches, the message is sent to the stream. A message without phase is
///default sent to all streams that would send it in at least one phase. If
///the ignore_general flag is set to true when generating a stream, that
///stream will ignore messages without phase.
///
///The system can be used without bothering with phases. All NRLib logging
///is phase 0 LOW.
///
///Symbols for use when sending message level and parsing exact levels.
enum MessageLevels {Error = 1, Warning = 2, Low = 4, Medium = 8, High = 16, DebugLow = 32, DebugHigh = 64};
///Symbols for use when parsing given level and lower.
enum LimitLevels {L_Error = 1, L_Warning = 3, L_Low = 7, L_Medium = 15,
L_High = 31, L_DebugLow = 63, L_DebugHigh = 127};
///Set a file that logs independent of phase.
static void SetFileLog(const std::string & fileName, int levels,
bool includeNRLibLogging = true);
///Set a full phase dependent file log
static void SetFileLog(const std::string & fileName,
const std::vector<int> & levels,
bool ignore_general = false);
///Set single-phase file log, useful for debugging given phase.
static void SetFileLog(const std::string & fileName,
int levels,
int phase,
bool ignore_general = false);
///Set a screen log independent of phase.
static void SetScreenLog(int levels, bool includeNRLibLogging = true);
///Set a full phase dependent screen log
static void SetScreenLog(const std::vector<int> & levels, bool ignore_general = false);
///Send message independent of phase
static void LogMessage(int level, const std::string & message);
///Send message in given phase
static void LogMessage(int level, int phase, const std::string & message);
///Send message as c-style format string and arguments.
// Sending format as reference fails.
static void LogFormatted(int level, std::string format, ...);
///Send message as c-style format string and arguments.
static void LogFormatted(int level, int phase, std::string format, ...);
///Close streams
static void EndLog();
///Buffering allows temporary storage of messages for sending to files
///opened later. When a file log is opened, the buffer is dumped to it.
///EndBuffering should be called once all files are opened.
static void StartBuffering();
static void EndBuffering();
static void SetPrefix(const std::string & prefix, int level);
static int GetNMessages(int level) { return n_messages_[level];}
static void WriteHeader(const std::string & text, MessageLevels logLevel = Low);
private:
static std::vector<LogStream *> logstreams_;
static int screenLog_; //Remembers which log is screen.
static std::vector<BufferMessage *> * buffer_;
static std::vector<int> n_messages_;
static std::vector<std::string> prefix_;
static void SendToBuffer(int level, int phase, const std::string & message);
static void DumpBuffer(LogStream * logstream);
};
///Class LogStream is for internal use in LogKit only.
class LogStream {
public:
///Convention: logstream = NULL means cout.
LogStream(std::ostream * logstream, int level);
LogStream(std::ostream * logstream, const std::vector<int> & levels, bool ignore_general = false);
~LogStream();
void LogMessage(int level, const std::string & message);
void LogMessage(int level, int phase, const std::string & message);
private:
std::ostream * logstream_;
std::vector<int> levels_;
int fullLevel_;
bool deleteStream;
};
struct BufferMessage {
std::string text_;
int phase_;
int level_;
};
}
#endif

View File

@@ -0,0 +1,6 @@
SRC += $(NRLIB_BASE_DIR)iotools/fileio.cpp \
$(NRLIB_BASE_DIR)iotools/logkit.cpp \
$(NRLIB_BASE_DIR)iotools/stringtools.cpp \
$(NRLIB_BASE_DIR)iotools/tabularfile.cpp
TEST_SRC += $(NRLIB_BASE_DIR)iotools/unittests/fileio_test.cpp

View File

@@ -0,0 +1,190 @@
// $Id: stringtools.cpp 1068 2012-09-18 11:21:53Z perroe $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "stringtools.hpp"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#define BOOST_FILESYSTEM_VERSION 2
#include <boost/filesystem.hpp>
using namespace NRLib;
template <>
std::string NRLib::ParseType<std::string>(const std::string& s)
{
return s; //Just to ensure that whitespace-containing strings survive.
}
std::string
NRLib::GetPath(const std::string& filename)
{
boost::filesystem::path path(filename);
return path.parent_path().file_string();
}
std::string
NRLib::GetExtension(const std::string& filename)
{
boost::filesystem::path path(filename);
return path.extension();
}
std::string
NRLib::RemovePath(const std::string& filename)
{
boost::filesystem::path path(filename);
return path.filename();
}
std::string
NRLib::PrependDir(const std::string& prefix,
const std::string& str)
{
boost::filesystem::path path(prefix);
path /= str;
return path.file_string();
}
std::string
NRLib::ReplaceExtension(const std::string& filename,
const std::string& extension)
{
boost::filesystem::path file(filename);
file.replace_extension(extension);
return file.file_string();
}
std::string
NRLib::AddExtension(const std::string& filename,
const std::string& extension)
{
std::string new_filename = filename + "." + extension;
return new_filename;
}
std::string
NRLib::GetStem(const std::string& filename)
{
boost::filesystem::path path(filename);
return path.stem();
}
std::vector<std::string>
NRLib::GetTokens(const std::string& s)
{
std::vector<std::string> v;
std::istringstream iss(s);
std::string tmp;
while(iss >> tmp) {
v.push_back(tmp);
}
return v;
}
std::vector<std::string>
NRLib::GetQuotedTokens(const std::string& s)
{
std::vector<std::string> v;
std::istringstream iss(s);
std::string tmp;
std::string char_tmp;
size_t quotation_last_position = 0;
while(iss >> tmp) {
char_tmp = tmp.substr(0, 1);
if (char_tmp != "\"")
v.push_back(tmp);
else{
quotation_last_position = s.find_first_of("\"", quotation_last_position + 1);
size_t first_quotation_mark = quotation_last_position;
quotation_last_position = s.find_first_of("\"", quotation_last_position + 1);
size_t second_quotation_mark = quotation_last_position;
std::string quotation = s.substr(first_quotation_mark + 1, (second_quotation_mark - first_quotation_mark - 1));
v.push_back(quotation);
if (char_tmp == tmp)
iss >> tmp;
while (tmp.substr((tmp.size() - 1), 1) != "\"")
iss >> tmp;
}
}
return v;
}
void
NRLib::Substitute(std::string & text,
const std::string & out,
const std::string & in)
{
std::string::size_type len = in.size();
std::string::size_type pos = text.find(out);
while (pos != std::string::npos) {
text.replace(pos, len, in);
pos = text.find(out);
}
}
std::string
NRLib::Uppercase(const std::string& text)
{
std::string out = text;
for (size_t i = 0; i < out.length(); ++i)
out[i] = static_cast<char>(std::toupper(out[i]));
return out;
}
bool
NRLib::IsNumber(const std::string & s)
{
std::istringstream inStream(s);
double inValue = 0.0;
if (inStream >> inValue)
return true;
else
return false;
}
std::string
NRLib::Chomp(const std::string& s)
{
std::string out = s;
size_t first, last;
first = out.find_first_not_of(" ");
last = out.find_last_not_of(" ");
out.erase(last + 1);
out.erase(0, first);
return out;
}

View File

@@ -0,0 +1,218 @@
// $Id: stringtools.hpp 1068 2012-09-18 11:21:53Z perroe $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef NRLIB_STRINGTOOLS_HPP
#define NRLIB_STRINGTOOLS_HPP
#include <stdlib.h> // For atoi and atof
#include <string>
#include <iomanip>
#include <vector>
#include <sstream>
#include <typeinfo>
#include "../exception/exception.hpp"
namespace NRLib {
std::vector<std::string> GetTokens(const std::string& s);
std::vector<std::string> GetQuotedTokens(const std::string& s);
/// OBS: Negative values are recognized as unsigned integers on Windows.
/// Works OK on Linux. (gcc 4.*)
template <typename T>
bool IsType(const std::string& s);
/// OBS: Negative values are recognized as unsigned integers on Windows.
/// Works OK on Linux. (gcc 4.*)
template <typename T>
T ParseType(const std::string& s);
/// If used in a template function, we may get string ot string parse.
/// Will not work with the version above, so handle special case.
template <>
std::string ParseType<std::string>(const std::string& s);
/// \todo Replace precision with a format object.
template <typename T>
std::string ToString(const T obj, int precision=-99999);
/// Not safe. Replaces whitespace in s with \0.
template <typename I>
I ParseAsciiArrayFast(std::string& s, I begin, size_t n);
/// Get the path from a full file name.
std::string GetPath(const std::string& filename);
/// Get the stem of the filename (filename without path and extension)
std::string GetStem(const std::string& filename);
/// Get filename extension
std::string GetExtension(const std::string& filename);
/// Get file name only (no path) from full file name.
std::string RemovePath(const std::string& filename);
/// Prepend prefix to str. Returns str if prefix is empty, if str is empty
/// or if str is a complete path starting with /.
/// Adds '/' as directory seperator if missing.
std::string PrependDir(const std::string& prefix,
const std::string& str);
/// Replace file extension.
std::string ReplaceExtension(const std::string& filename,
const std::string& extension);
/// Add an extension to the filename.
std::string AddExtension(const std::string& filename,
const std::string& extension);
/// In string text replace all occurences odf string "out" with string "in".
void Substitute(std::string & text,
const std::string & out,
const std::string & in);
/// Return uppercase of input string.
std::string Uppercase(const std::string& text);
bool IsNumber(const std::string & s);
std::string Chomp(const std::string& s);
/// String with different kinds of whitespace characters.
inline std::string Whitespace() { return " \t\n\r\f\v"; }
namespace NRLibPrivate {
template <class A>
class UnsafeParser
{
public:
static A ParseType(const char* s) {
return NRLib::ParseType<A>(s);
}
};
template <>
class UnsafeParser<int>
{
public:
static int ParseType(const char* s) {
return atoi(s);
}
};
template <>
class UnsafeParser<double>
{
public:
static double ParseType(const char* s) {
return atof(s);
}
};
template <>
class UnsafeParser<float>
{
public:
static double ParseType(const char* s) {
return static_cast<float>(atof(s));
}
};
} // namespace NRLibPrivate
} // namespace NRLib
/// @todo Use correct exceptions.
template <typename T>
bool NRLib::IsType(const std::string& s)
{
std::istringstream i(s);
T x;
char c;
if (!(i >> x) || i.get(c))
return false;
return true;
}
template <typename T>
T NRLib::ParseType(const std::string& s)
{
std::istringstream i(s);
T x;
char c;
if (!(i >> x))
throw Exception("Failed to convert \"" + s + "\" to " + typeid(T).name());
if (i.get(c))
throw Exception("Could not convert whole \"" + s + "\" to " + typeid(T).name());
return x;
}
template <typename T>
std::string NRLib::ToString(const T obj, int precision)
{
std::ostringstream o;
if (precision!=-99999) {
o << std::fixed << std::setprecision(precision);
}
if (!(o << obj)) {
throw Exception("Bad conversion.");
}
return o.str();
}
template <typename I>
I NRLib::ParseAsciiArrayFast(std::string& s, I begin, size_t n)
{
typedef typename std::iterator_traits<I>::value_type T;
std::string whitespace = " \n\r\f\t";
size_t pos = s.find_first_not_of(whitespace, 0);
size_t next_pos = s.find_first_of(whitespace, pos+1);
size_t i = 0;
while (i < n && pos != s.npos) {
if (next_pos != s.npos) {
s[next_pos] = '\0';
}
*begin = NRLibPrivate::UnsafeParser<T>::ParseType(&s[pos]);
++begin;
pos = s.find_first_not_of(whitespace, next_pos + 1);
next_pos = s.find_first_of(whitespace, pos + 1);
++i;
}
if (i != n) {
throw Exception("Not enough elements parsed from string.");
}
return begin;
}
#endif // NRLIB_STRINGTOOLS_HPP

View File

@@ -0,0 +1,135 @@
// $Id: tabularfile.cpp 1078 2012-09-25 11:13:53Z veralh $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "tabularfile.hpp"
#include <fstream>
#include "fileio.hpp"
#include "../exception/exception.hpp"
namespace NRLib {
TabularFile::TabularFile(const std::string& filename)
{
size_t first_data_line, n_columns;
bool read_last_line;
std::string last_line;
if (!CheckFile(filename, first_data_line, n_columns, read_last_line, last_line))
throw FileFormatError("The format of " + filename + " is not supported.");
ReadFromFile(filename, first_data_line, n_columns, read_last_line);
}
TabularFile::TabularFile(const std::string & filename,
size_t first_data_line,
size_t n_columns,
bool read_last_line)
{
ReadFromFile(filename, first_data_line, n_columns, read_last_line);
}
bool TabularFile::CheckFile(const std::string & filename,
size_t & first_data_line,
size_t & n_columns,
bool & read_last_line,
std::string & last_line)
{
// Check if the last line of the file consists of data
// and make initial guess if the last line shoud be read (not reading when equal to 0 or -999)
std::ifstream in_file0;
OpenRead(in_file0, filename);
last_line = FindLastNonEmptyLine(in_file0);
std::vector<std::string> tokens = GetTokens(last_line);
read_last_line = true;
if (!IsType<double>(tokens[0])) {
read_last_line = false;
}
else {
for (size_t i = 0; i < tokens.size(); ++i) {
if (!IsType<double>(tokens[i]))
read_last_line = false;
else {
if(atof(tokens[i].c_str()) == 0.0 || atof(tokens[i].c_str()) == -999.0)
read_last_line = false;
}
}
}
std::ifstream in_file;
OpenRead(in_file, filename);
int line_number = 0;
std::string line;
while (GetNextNonEmptyLine(in_file, line_number, line)) {
std::vector<std::string> tokens = GetTokens(line);
if (IsType<double>(tokens[0])) {
first_data_line = line_number;
n_columns = tokens.size();
for (size_t i = 0; i < n_columns; ++i) {
if (!IsType<double>(tokens[i]))
return false;
}
return true;
}
}
return false;
}
void TabularFile::ReadFromFile(const std::string & filename,
size_t first_data_line,
size_t n_columns,
bool read_last_line)
{
std::ifstream in_file;
OpenRead(in_file, filename);
std::string line;
columns_.resize(n_columns);
std::vector<double> data(n_columns);
// First line in file is line number 1. Read and discard up to first_data_line
for (size_t i = 1; i < first_data_line; ++i) {
std::getline(in_file, line);
}
std::string this_line, next_line;
std::getline(in_file, this_line);
while(std::getline(in_file, next_line)) {
ParseAsciiArrayFast(this_line, data.begin(), n_columns);
for (size_t i = 0; i < n_columns; ++i) {
columns_[i].push_back(data[i]);
}
this_line = next_line;
}
// Reading last line
if(read_last_line) {
ParseAsciiArrayFast(this_line, data.begin(), n_columns);
for (size_t i = 0; i < n_columns; ++i) {
columns_[i].push_back(data[i]);
}
}
}
} // namespace NRLib

View File

@@ -0,0 +1,70 @@
// $Id: tabularfile.hpp 1078 2012-09-25 11:13:53Z veralh $
// Copyright (c) 2011, Norwegian Computing Center
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// <20> Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// <20> Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef NRLIB_IOTOOLS_TABULARFILE_HPP
#define NRLIB_IOTOOLS_TABULARFILE_HPP
#include <cassert>
#include <string>
#include <vector>
namespace NRLib {
/// Class for files with tabular data, i.e. data values separated by a delimiter.
/// Currently only supports doubles separated by spaces with an optional text header.
class TabularFile
{
public:
explicit TabularFile(const std::string& filename);
TabularFile(const std::string& filename, size_t first_data_line, size_t n_columns, bool read_last_line = true);
/// Simple check of file. Just checks that the file contains tabular data.
/// \param[out] first_data_line Number of lines with header data.
static bool CheckFile(const std::string& filename,
size_t& first_data_line,
size_t& n_columns,
bool& read_last_line,
std::string& last_line);
void ReadFromFile(const std::string& filename,
size_t first_data_line,
size_t n_columns,
bool read_last_line = true);
size_t GetNColumns() const { return columns_.size(); }
inline const std::vector<double>& GetColumn(size_t i) const;
private:
std::vector<std::vector<double> > columns_;
};
// ========== INLINE FUNCTIONS =========
const std::vector<double>& TabularFile::GetColumn(size_t i) const
{
assert(i < columns_.size());
return columns_[i];
}
} // namespace NRLib
#endif // NRLIB_IOTOOLS_TABULARFILE_HPP