-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathio_helper.cc
More file actions
112 lines (91 loc) · 2.6 KB
/
io_helper.cc
File metadata and controls
112 lines (91 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "io_helper.h"
/**
* @fn bool fline_tr(ifstream* fin, vector <string> fields, string delim)
* Retrieve a line from fin using delimiters in delim, and trim all strings retrieved.
*
* Removed Empty (after trimmed) spaces.
* @param fin File stream to read from
* @param fields Container to put results
* @param delim Delimiters to use
* @return if line retrieval was successful
*/
bool fline_tr(ifstream *fin, vector<string> *fields, string delim) {
fields->clear();
vector<string>::iterator p, q;
string full;
getline(*fin, full);
if (fin->fail() && fin->eof())
return false;
char *pt = strtok((char *) (full.c_str()), delim.c_str());
while (pt != NULL) {
fields->emplace_back(pt);
pt = strtok(NULL, delim.c_str());
}
for (p = fields->begin(); p != fields->end(); p++) {
while (trim(*p).compare("") == 0 && p != fields->end()) {
q = p;
fields->erase(q);
}
if (p == fields->end())
break;
*p = trim(*p);
}
return true;
}
/** @fn void split_tr(vector <string>* fields, string delim)
*
*/
void split_tr(string input, vector<string> *fields, string delim) {
vector<string>::iterator p, q;
fields->clear();
char *pt = strtok((char *) (input.c_str()), delim.c_str());
while (pt != NULL) {
fields->emplace_back(pt);
pt = strtok(NULL, delim.c_str());
}
for (p = fields->begin(); p != fields->end(); p++) {
while (trim(*p).compare("") == 0 && p != fields->end()) {
q = p;
fields->erase(q);
}
if (p == fields->end())
break;
*p = trim(*p);
}
}
/**
* @fn bool openFile(ifstream* fin, string filename)
*
* Open an ifstream with some basic checks
*
* @param fin ifstream to use to open file
* @param filename file to open
* @return if open successful
*/
bool openFile(ifstream *fin, string filename) {
fin->open(filename.c_str());
if (!(fin->is_open())) {
return false;
}
return true;
}
/**
* @fn bool openFileHarsh(ifstream* fin, string filename)
*
* Open an ifstream with some basic checks. If the file is not
* opened, the program is exited. If error handling is
* desired, use 'openFile'.
*
* @param fin ifstream to use to open file
* @param filename file to open
*/
void openFileHarsh(ifstream *fin, string filename) {
fin->open(filename.c_str());
if (!(fin->is_open())) {
FileError(filename);
exit(3);
}
}
void FileError(const string &filename) {
cerr << "Could not open file " << filename << endl;
}