Ascii Input: Accept dos/windows newlines.

The ascii reader now accepts \r\n newlines without complaining.
Furthermore, the reader was slightly rewritten in a more c++11-y way,
removing all raw pointers from the class.

Addresses BIT-1198
This commit is contained in:
Johanna Amann 2016-06-01 11:20:14 -07:00
parent 85213e6b55
commit e8418ad5b0
4 changed files with 107 additions and 36 deletions

View file

@ -1,6 +1,5 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include <fstream>
#include <sstream>
#include <sys/types.h>
@ -49,25 +48,15 @@ FieldMapping FieldMapping::subType()
Ascii::Ascii(ReaderFrontend *frontend) : ReaderBackend(frontend)
{
file = 0;
mtime = 0;
formatter = 0;
}
Ascii::~Ascii()
{
DoClose();
delete formatter;
}
void Ascii::DoClose()
{
if ( file != 0 )
{
file->close();
delete(file);
file = 0;
}
}
bool Ascii::DoInit(const ReaderInfo& info, int num_fields, const Field* const* fields)
@ -107,23 +96,19 @@ bool Ascii::DoInit(const ReaderInfo& info, int num_fields, const Field* const* f
Error("set_separator length has to be 1. Separator will be truncated.");
formatter::Ascii::SeparatorInfo sep_info(separator, set_separator, unset_field, empty_field);
formatter = new formatter::Ascii(this, sep_info);
formatter = unique_ptr<threading::formatter::Formatter>(new formatter::Ascii(this, sep_info));
file = new ifstream(info.source);
if ( ! file->is_open() )
file.open(info.source);
if ( ! file.is_open() )
{
Error(Fmt("Init: cannot open %s", info.source));
delete(file);
file = 0;
return false;
}
if ( ReadHeader(false) == false )
{
Error(Fmt("Init: cannot open %s; headers are incorrect", info.source));
file->close();
delete(file);
file = 0;
file.close();
return false;
}
@ -215,8 +200,11 @@ bool Ascii::ReadHeader(bool useCached)
bool Ascii::GetLine(string& str)
{
while ( getline(*file, str) )
while ( getline(file, str) )
{
if ( str.back() == '\r' ) // deal with \r\n by removing \r
str.pop_back();
if ( str[0] != '#' )
return true;
@ -258,24 +246,22 @@ bool Ascii::DoUpdate()
{
// dirty, fix me. (well, apparently after trying seeking, etc
// - this is not that bad)
if ( file && file->is_open() )
if ( file.is_open() )
{
if ( Info().mode == MODE_STREAM )
{
file->clear(); // remove end of file evil bits
file.clear(); // remove end of file evil bits
if ( !ReadHeader(true) )
return false; // header reading failed
break;
}
file->close();
delete file;
file = 0;
file.close();
}
file = new ifstream(Info().source);
if ( ! file->is_open() )
file.open(Info().source);
if ( ! file.is_open() )
{
Error(Fmt("cannot open %s", Info().source));
return false;
@ -296,7 +282,7 @@ bool Ascii::DoUpdate()
string line;
file->sync();
file.sync();
while ( GetLine(line) )
{