mapgd  0.4
A program for the Maximum-likelihood analysis of population genomic data.
 All Data Structures Functions Variables Friends Groups Pages
stream_tools.h
1 #ifndef _STREAMTOOLS_H_
2 #define _STREAMTOOLS_H_
3 
4 #include <vector>
5 #include <iostream>
6 #include <fstream>
7 #include <sstream>
8 #include <algorithm>
9 #include <stdio.h>
10 
11 #ifdef POSIX
12 #include <sys/poll.h>
13 #endif
14 
16 /*
17  * For example, if the string "A,B,C , D" is given, then {"A", "B", "C "," D"} is returned.
18  * If the string "A,B,C,\nD,E,F" is given, then {"A", "B", "C"} is returned.
19  */
20 inline std::vector<std::string> split(std::istream &in, const char &delim)
21 {
22  std::vector<std::string> elements;
23  std::string line;
24  size_t last_delim=0, this_delim=0;
25  getline(in, line);
26  do {
27  this_delim=line.find(delim, last_delim);
28  elements.push_back( line.substr(last_delim, this_delim-last_delim) );
29  last_delim=this_delim+1;
30  } while ( this_delim!=std::string::npos );
31  return elements;
32 }
33 
35 inline std::vector<std::string> split(const std::string &s, const char &delim)
36 {
37  std::stringstream ss;
38  ss << s;
39  return split(ss, delim);
40 }
41 
43 /*inline std::vector<std::string> split(std::istream &in)
44 {
45  std::string line_in;
46  std::string line_out;
47  getline(in, line_in);
48  line_out.reserve(line_in.size() );
49  //TODO REMOVE WHITESPACE
50  return split(line_out, ' ');
51 }*/
52 
54 /* For example, if the string "A,B,C , D" is given, and a delimiter ',' is used
55  * to split the string, then {"A", "B,C , D"} is returned.
56  */
57 inline std::vector<std::string> split_first(std::istream &in, const char &delim)
58 {
59  std::string first, second;
60  std::getline(in, first, delim);
61  std::getline(in, second);
62  if (second.size()!=0) return std::vector <std::string> {first, second};
63  else return std::vector <std::string> {first};
64 }
65 
66 
68 inline std::vector<std::string> split_first(const std::string &s, const char &delim)
69 {
70  std::stringstream ss;
71  ss << s;
72  return split_first(ss, delim);
73 }
74 
76 /* For example, if the string "A,B,C , D" is given, and a delimiter ',' is used
77  * to split the string, then {"A,B,C "," D"} is returned.
78  */
79 inline std::vector<std::string> split_last(std::istream &in, const char &delim)
80 {
81  std::string line;
82  getline(in, line);
83  size_t pos=line.rfind(delim);
84  size_t end=line.size();
85  if (pos!= std::string::npos ) return std::vector <std::string> {line.substr(0, pos), line.substr(pos, end)};
86  else return std::vector <std::string> {line};
87 
88 }
89 
91 inline std::vector<std::string> split_last(const std::string &s, const char &delim)
92 {
93  std::stringstream ss;
94  ss << s;
95  return split_last(ss, delim);
96 }
97 
99 inline std::string sanitize (std::string &s){
100  std::replace( s.begin(), s.end(), '\t', ' ' );
101  std::replace( s.begin(), s.end(), '\'', '"' );
102  std::replace( s.begin(), s.end(), '>', ' ' );
103  std::replace( s.begin(), s.end(), '<', ' ' );
104  return s;
105 }
106 
108 inline std::string sanitize (const std::string &s){
109  std::string r=s;
110  return sanitize(r);
111 }
112 
113 #ifdef POSIX
114 bool check_stream(std::istream *);
116 #endif
117 
118 #endif