comparison BEDTools-Version-2.14.3/src/utils/tabFile/tabFile.cpp @ 0:dfcd8b6c1bda

Uploaded
author aaronquinlan
date Thu, 03 Nov 2011 10:25:04 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:dfcd8b6c1bda
1 /*****************************************************************************
2 tabFile.cpp
3
4 (c) 2009 - Aaron Quinlan
5 Hall Laboratory
6 Department of Biochemistry and Molecular Genetics
7 University of Virginia
8 aaronquinlan@gmail.com
9
10 Licensed under the GNU General Public License 2.0 license.
11 ******************************************************************************/
12 #include "lineFileUtilities.h"
13 #include "tabFile.h"
14
15 /*******************************************
16 Class methods
17 *******************************************/
18
19 // Constructor
20 TabFile::TabFile(const string &tabFile)
21 : _tabFile(tabFile)
22 {}
23
24 // Destructor
25 TabFile::~TabFile(void) {
26 }
27
28
29 void TabFile::Open(void) {
30 if (_tabFile == "stdin") {
31 _tabStream = &cin;
32 }
33 else {
34 size_t foundPos;
35 foundPos = _tabFile.find_last_of(".gz");
36 // is this a GZIPPED TAB file?
37 if (foundPos == _tabFile.size() - 1) {
38 igzstream tabs(_tabFile.c_str(), ios::in);
39 if ( !tabs ) {
40 cerr << "Error: The requested file (" << _tabFile << ") could not be opened. Exiting!" << endl;
41 exit (1);
42 }
43 else {
44 // if so, close it (this was just a test)
45 tabs.close();
46 // now set a pointer to the stream so that we
47 // can read the file later on.
48 _tabStream = new igzstream(_tabFile.c_str(), ios::in);
49 }
50 }
51 // not GZIPPED.
52 else {
53
54 ifstream tabs(_tabFile.c_str(), ios::in);
55 // can we open the file?
56 if ( !tabs ) {
57 cerr << "Error: The requested file (" << _tabFile << ") could not be opened. Exiting!" << endl;
58 exit (1);
59 }
60 else {
61 // if so, close it (this was just a test)
62 tabs.close();
63 // now set a pointer to the stream so that we
64 // can read the file later on.
65 _tabStream = new ifstream(_tabFile.c_str(), ios::in);
66 }
67 }
68 }
69 }
70
71
72 // Close the TAB file
73 void TabFile::Close(void) {
74 if (_tabFile != "stdin") delete _tabStream;
75 }
76
77
78 TabLineStatus TabFile::GetNextTabLine(TAB_FIELDS &tabFields, int &lineNum) {
79
80 // make sure there are still lines to process.
81 // if so, tokenize, return the TAB_FIELDS.
82 if (_tabStream->good() == true) {
83 string tabLine;
84 tabFields.reserve(20);
85
86 // parse the tabStream pointer
87 getline(*_tabStream, tabLine);
88 lineNum++;
89
90 // split into a string vector.
91 Tokenize(tabLine, tabFields);
92
93 // parse the line and validate it
94 return parseTabLine(tabFields, lineNum);
95 }
96
97 // default if file is closed or EOF
98 return TAB_INVALID;
99 }