Richards
Solving The Richards equation with temporally periodic surface boundary conditions
io.cc
Go to the documentation of this file.
1 
3 #include "io.h"
4 
5 void print_exit (const char *msg) {
6 
7  //print the given message
8  printf("\nFAILURE: %s\n\n", msg);
9  //cancel the program
10  exit(EXIT_FAILURE);
11 }
12 
13 //------------------------------------------------------------------------------
14 //string manipulation
15 
16 void strip_string (std::string &s) {
17  //do nothing if the string is empty
18  size_t idx;
19  //remove leading white space
20  idx = s.find_first_not_of(" \t\n\r");
21  s.erase(0, idx);
22  //remove trailing white space
23  idx = s.find_last_not_of(" \t\n\r");
24  s.erase(idx+1);
25 }
26 
27 void split_string (char c, std::string &s, std::string &s1, std::string &s2) {
28  size_t idx = s.find(c);
29  if (idx == std::string::npos) {
30  std::cout << "FAILURE: cannot split string" << s << "on character" << c << std::endl;
31  exit(EXIT_FAILURE);
32  }
33  s1 = s.substr(0, idx);
34  s2 = s.substr(idx+1, s.length());
35 }
36 
37 std::string int_to_string (long i) {
38 
39  // https://www.geeksforgeeks.org/converting-string-to-number-and-vice-versa-in-c/
40  std::ostringstream str;
41  str << i;
42  std::string out = str.str();
43  return(out);
44 }
45 
46 //------------------------------------------------------------------------------
47 //writing
48 
49 void check_file_write (const char *fn) {
50  FILE* ofile;
51  ofile = fopen(fn, "w");
52  if (ofile == NULL) {
53  std::cout << "cannot open file:" << fn << std::endl;
54  exit(EXIT_FAILURE);
55  }
56  fclose(ofile);
57 }
58 
59 //------------------------------------------------------------------------------
60 //reading
61 
62 void check_file_read (const char *fn) {
63  FILE* ofile;
64  ofile = fopen(fn, "r");
65  if (ofile == NULL) {
66  std::cout << "FAILURE: cannot open file " << fn << std::endl;
67  exit(EXIT_FAILURE);
68  }
69  fclose(ofile);
70 }
71 
72 std::vector< std::vector< std::string > > read_values (const char *fn) {
73 
74  std::vector< std::vector< std::string > > iset;
75 
76  //open the settings file
77  check_file_read(fn);
78  std::ifstream ifile(fn); //automatically closed
79  //read in settings
80  char readline[1000];
81  std::string line;
82  std::string::size_type idx;
83  std::string key, val;
84  std::vector< std::string > str;
85  while (ifile.getline(readline, 1000)) {
86  line = readline;
87  strip_string(line);
88  idx = line.find('#');
89  //ignore empty lines and whole comment lines
90  if ( (line.length() > 0) && (idx != 0) ) {
91  //remove partial comments
92  if (idx != std::string::npos) line = line.substr(0, idx);
93  //split on the equal sign
94  split_string('=', line, key, val);
95  //strip the key and val
96  strip_string(key);
97  strip_string(val);
98  //create a new "line" in the return object
99  str.clear();
100  str.push_back(key);
101  str.push_back(val);
102  iset.push_back(str);
103  }
104  }
105 
106  return(iset);
107 }
read_values
std::vector< std::vector< std::string > > read_values(const char *fn)
reads a settings file into a vector of vectors of strings
Definition: io.cc:72
print_exit
void print_exit(const char *msg)
prints a message before quitting the program
Definition: io.cc:5
int_to_string
std::string int_to_string(long i)
converts an integer into a string
Definition: io.cc:37
check_file_write
void check_file_write(const char *fn)
checks if a file can be written to
Definition: io.cc:49
check_file_read
void check_file_read(const char *fn)
checks if a file can be opened for reading
Definition: io.cc:62
io.h
strip_string
void strip_string(std::string &s)
removes white space at the beginning and end of a string
Definition: io.cc:16
split_string
void split_string(char c, std::string &s, std::string &s1, std::string &s2)
splits a string on the first instance of a specified character
Definition: io.cc:27