4 * Copyright (c) 2007 Des Higgins, Julie Thompson and Toby Gibson.
7 * InFileStream subclasses std::ifstream, adding a check for the end-of-line
8 * character convention in the input file. This is then used by the getline()
9 * member as the line delimiter, unless the caller supplies an explicit
12 * Note: This is an ugly workaround; at present various operations repeatedly
13 * construct/destruct an instance and open/close a sequence file up to 12
14 * times! A cleaner class will probably derive this class from something like
15 * 'istream' aggregating a 'filebuf' under control of istream::seekg().
17 * Created: 09-02-07,Nigel Brown(EMBL)
20 * Mark Larkin 13-2-07: I removed the dynamic cast from the getline functions.
21 ***************************************************************************/
28 #include "InFileStream.h"
31 const char LF = 0x0a; //linefeed
32 const char CR = 0x0d; //carriage return
34 InFileStream::InFileStream() :
37 delim = '\n'; // default
38 //cout << "InFileStream() constructor 1" << endl;
41 InFileStream::InFileStream(const char *filename) :
42 ifstream(filename, ios::in), filename(filename)
44 //cout << "InFileStream(f) constructor 2" << endl;
45 delim = findDelimiter();
48 //- copy-constructor: can't copy superclass private members
49 //- InFileStream::InFileStream(const InFileStream ©) :
50 //- ifstream(static_cast<const ifstream&>(copy))
52 //- cout << "InFileStream() constructor 3" << endl;
53 //- delim = copy.delim;
56 void InFileStream::open(const char *filename)
59 this->filename = filename;
60 ifstream::open(filename, ios::in);
63 delim = findDelimiter();
66 //not necessary, but for symmetry to open()
67 void InFileStream::close()
73 //getline with stored delimiter
74 std::istream& InFileStream::getline(char *s, streamsize n)
76 return ifstream::getline(s, n, delim);
79 //getline with caller supplied delimiter
80 std::istream& InFileStream::getline(char *s, streamsize n, char delim)
82 return ifstream::getline(s, n, delim);
87 * Mark 24-1-2007. I added the function findDelimiter to determine if '\r' or
88 * '\n' will be used as the line delimiter when parsing the file.
90 * 25-01-07,Nigel Brown(EMBL): changed body of loop to check successive chars
91 * in case of DOS/Windows
93 * 09-02-07,Nigel Brown(EMBL): moved member into new InFileStream subclassed
94 * from std::ifstream, so this is called automatically for any file reader
95 * that uses InFileStream in place of std::ifstream. Replaced if/then/else
98 char InFileStream::findDelimiter()
103 in.open(filename.c_str(), ios::in);
107 in.seekg(0, ios::beg);
109 //look for CR or LF or CRLF (or LFCR)
125 //cout << "file is Mac System 9" << endl;
129 //cout << "file is UNIX" << endl;
133 //cout << "file is DOS" << endl;
136 default: //short or empty file
137 //cout << "file is UNIX (default)" << endl;