Canoe
Comprehensive Atmosphere N' Ocean Engine
fileio.cpp
Go to the documentation of this file.
1 // C/C++ header
2 #include <cctype> // isspace
3 #include <cstring>
4 #include <fstream>
5 #include <sstream>
6 #include <stdexcept>
7 #include <vector>
8 
9 // application
10 #include <application/exceptions.hpp>
11 
12 // utils header
13 #include "fileio.hpp"
14 #include "vectorize.hpp"
15 
16 bool FileExists(std::string fname) {
17  std::ifstream ifile(fname.c_str());
18  return ifile.is_open();
19 }
20 
21 bool IsBlankLine(char const* line) {
22  for (char const* cp = line; *cp; ++cp) {
23  if (!std::isspace(*cp)) return false;
24  }
25  return true;
26 }
27 
28 bool IsBlankLine(std::string const& line) { return IsBlankLine(line.c_str()); }
29 
30 std::string DecommentFile(std::string fname) {
31  std::stringstream msg;
32  if (!FileExists(fname)) {
33  throw NotFoundError("DecommentFile", fname);
34  }
35 
36  std::ifstream file(fname.c_str(), std::ios::in);
37  std::string ss;
38  char c;
39  while (file) {
40  file.get(c);
41  if (c == '#') {
42  while (c != '\n' && file) file.get(c);
43  continue;
44  }
45  ss += c;
46  }
47  return ss;
48 }
49 
50 int GetNumCols(std::string fname, char c) {
51  std::ifstream inp(fname.c_str(), std::ios::in);
52  std::string line;
53  std::getline(inp, line);
54  if (line.empty()) return 0;
55  int cols = line[0] == c ? 0 : 1;
56 
57  for (int i = 1; i < line.length(); ++i)
58  if (line[i - 1] == c && line[i] != c) cols++;
59  return cols;
60 }
61 
62 int GetNumRows(std::string fname) {
63  std::ifstream inp(fname.c_str(), std::ios::in);
64  std::string line;
65  int rows = 0;
66 
67  while (std::getline(inp, line)) ++rows;
68  return rows;
69 }
70 
71 void replaceChar(char* buf, char c_old, char c_new) {
72  int len = strlen(buf);
73  for (int i = 0; i < len; ++i)
74  if (buf[i] == c_old) buf[i] = c_new;
75 }
76 
77 char* StripLine(char* line) {
78  char* p = line;
79  int len = strlen(line);
80  // strip newline or carriage rtn
81  while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
82  line[--len] = 0;
83  // advance to first non-whitespace
84  while (isspace(*p)) p++;
85  // advance to first non-whitespace
86  // skip characters aftet '#'
87  char* pp = p;
88  while (*pp != '#' && *pp) pp++;
89  *pp = 0;
90  return p;
91 }
92 
93 char* NextLine(char* line, int num, FILE* stream) {
94  char* p;
95  while (fgets(line, num, stream) != NULL) {
96  p = StripLine(line);
97  if (strlen(p) > 0) break;
98  }
99  return p;
100 }
101 
102 DataVector read_data_vector(std::string fname) {
103  DataVector amap;
104 
105  std::ifstream input(fname.c_str(), std::ios::in);
106  std::stringstream ss, msg;
107  std::string line, sbuffer;
108  std::vector<std::string> field;
109 
110  if (!input.is_open()) {
111  throw NotFoundError("read_data_vector", fname);
112  }
113 
114  getline(input, line);
115  ss.str(line);
116  while (!ss.eof()) {
117  ss >> sbuffer;
118  field.push_back(sbuffer);
119  }
120  ss.clear();
121 
122  while (getline(input, line)) {
123  if (line.empty()) continue;
124  ss.str(line);
125  for (std::vector<std::string>::iterator f = field.begin(); f != field.end();
126  ++f) {
127  double value;
128  ss >> value;
129  amap[*f].push_back(value);
130  }
131  ss.clear();
132  }
133 
134  return amap;
135 }
int GetNumCols(std::string fname, char c)
get number of columns in a data table
Definition: fileio.cpp:50
std::string DecommentFile(std::string fname)
decomment a file
Definition: fileio.cpp:30
int GetNumRows(std::string fname)
get number of rows in a data table
Definition: fileio.cpp:62
bool FileExists(std::string fname)
test file existance
Definition: fileio.cpp:16
char * StripLine(char *line)
Definition: fileio.cpp:77
void replaceChar(char *buf, char c_old, char c_new)
replace a character in a string
Definition: fileio.cpp:71
char * NextLine(char *line, int num, FILE *stream)
Definition: fileio.cpp:93
bool IsBlankLine(char const *line)
test a blank line
Definition: fileio.cpp:21
DataVector read_data_vector(std::string fname)
Definition: fileio.cpp:102
std::map< std::string, std::vector< double > > DataVector
Definition: fileio.hpp:43
Real cp
Definition: straka.cpp:66