Richards
Solving The Richards equation with temporally periodic surface boundary conditions
io.h
Go to the documentation of this file.
1 #ifndef IO_H_
2 #define IO_H_
3 
5 
6 #include <string>
7 #include <vector>
8 #include <cstdlib>
9 #include <fstream>
10 #include <sstream>
11 #include <iostream>
12 
14 
17 void print_exit (const char *msg);
18 
19 //------------------------------------------------------------------------------
20 //string manipulation
21 
23 
26 void strip_string (std::string &s);
27 
29 
35 void split_string (char c, std::string &s, std::string &s1, std::string &s2);
36 
38 std::string int_to_string (long i);
39 
40 //------------------------------------------------------------------------------
41 //writing
42 
44 void check_file_write (const char *fn);
45 
47 
52 template <class T>
53 void write_array (const char *fn, T *a, long size) {
54  FILE* ofile;
55  check_file_write(fn);
56  ofile = fopen(fn, "wb");
57  fwrite(a, sizeof(T), size, ofile);
58  fclose(ofile);
59 }
60 
62 
67 template <class T>
68 void write_array (const std::string &fn, T *a, long size) {
69  write_array(fn.c_str(), a, size);
70 }
71 
73 
77 template <class T>
78 void write_array (const char *fn, std::vector<T> a) {
79  write_array(fn, a.data(), long(a.size()));
80 }
81 
83 
87 template <class T>
88 void write_array (const std::string &fn, std::vector<T> a) {
89  write_array(fn.c_str(), a.data(), long(a.size()));
90 }
91 
92 //------------------------------------------------------------------------------
93 //reading
94 
96 
99 void check_file_read (const char *fn);
100 
102 
105 std::vector< std::vector< std::string > > read_values (const char *fn);
106 
107 #endif
strip_string
void strip_string(std::string &s)
removes white space at the beginning and end of a string
Definition: io.cc:16
int_to_string
std::string int_to_string(long i)
converts an integer into a string
Definition: io.cc:37
write_array
void write_array(const char *fn, T *a, long size)
writes an array to a binary file
Definition: io.h:53
check_file_read
void check_file_read(const char *fn)
checks if a file can be opened for reading
Definition: io.cc:62
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
check_file_write
void check_file_write(const char *fn)
checks if a file can be written to
Definition: io.cc:49
print_exit
void print_exit(const char *msg)
prints a message before quitting the program
Definition: io.cc:5
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