PlasCom2  1.0
XPACC Multi-physics simluation application
primitive_utilities.C
Go to the documentation of this file.
1 #include <sstream>
7 #include <cstring>
8 
9 #include "primitive_utilities.H"
10 
11 
12 namespace ix {
13 
14  namespace util {
15 
16  std::string GetNextContent(std::istream &In)
17  {
18  std::string line;
19  std::string ret;
20  while(ret.empty() && std::getline(In,line)){
21  std::istringstream Istr(line);
22  std::string token;
23  Istr >> token;
24  if(!token.empty() && token[0] != '#')
25  ret.assign(line.substr(0,line.find("#"));
26  }
27  return(ret);
28  }
29 
30  void GetContentUntil(std::istream &In,
31  std::string ret,
32  const std::string &etag)
33  {
34  std::ostringstream Ostr;
35  std::string line;
36  while(std::getline(In,line) && ret.empty()){
37  std::istringstream Istr(line);
38  std::string token;
39  Istr >> token;
40  if(!token.empty()){
41  if(token == etag)
42  ret = Ostr.str();
43  else if (token[0] != '#'){
44  if(!Ostr.str().empty())
45  Ostr << "\n";
46  Ostr << line;
47  }
48  }
49  }
50  }
51 
52  int String2Buf(const std::string &instr,void **buf)
53  {
54  if(instr.empty())
55  return 0;
56  int rval = instr.size();
57  *buf = new char [rval];
58  void *resl = std::memcpy(*buf,instr.c_str(),rval);
59  if(!resl)
60  return(0);
61  return(rval);
62  }
63 
64  void Trim(std::string &instr,bool preserve_newline)
65  {
66  std::istringstream Istr(instr);
67  std::string line;
68  std::ostringstream Ostr;
69  while(std::getline(Istr,line)){
70  std::istringstream Line(line);
71  std::string token;
72  Line >> token;
73  Ostr << token;
74  while(Line >> token)
75  if(!token.empty())
76  Ostr << " " << token;
77  if(preserve_newline)
78  Ostr << "\n";
79  }
80  instr = Ostr.str();
81  }
82 
83  const std::string Trimmed(const std::string &instr,bool preserve_newline)
84  {
85  std::istringstream Istr(instr);
86  std::string line;
87  std::ostringstream Ostr;
88  while(std::getline(Istr,line)){
89  std::istringstream Line(line);
90  std::string token;
91  Line >> token;
92  Ostr << token;
93  while(Line >> token)
94  if(!token.empty())
95  Ostr << " " << token;
96  if(preserve_newline)
97  Ostr << "\n";
98  }
99  return(Ostr.str());
100  }
101 
102  const std::string
103  stripdirs(const std::string &pname)
104  {
105  std::string retval;
106  std::string::size_type x = pname.find("/");
107  if(x == std::string::npos)
108  return(pname);
109  return(pname.substr(pname.find_last_of("/")+1));
110  }
111 
112  void
113  TokenizeString(std::vector<std::string> &tokens,const std::string &source)
114  {
115  tokens.resize(0);
116  std::istringstream Istr(source);
117  std::string token;
118  while(Istr >> token)
119  tokens.push_back(token);
120  }
121 
122  void
123  TokenizeString(std::vector<std::string> &tokens,const std::string &source,const char delim)
124  {
125  tokens.resize(0);
126  std::string::size_type ssize = source.size();
127  std::string::size_type x = 0;
128  std::ostringstream Ostr;
129  std::string token;
130  while(x < ssize){
131  if(source[x] != delim)
132  token += source[x];
133  if((source[x] == delim) || (x == (ssize-1))){
134  tokens.push_back(token);
135  token.erase();
136  }
137  x++;
138  }
139  }
140 
141  int OpenFile(std::ifstream &Inf,const std::string &filename)
142  {
143  Inf.open(filename.c_str());
144  if(!Inf)
145  return(-1);
146  return 0;
147  }
148 
149  // Utilities
150  void
151  Vectorize(std::vector<std::string> &retVal,const char **in)
152  {
153  int i = 0;
154  while(in[i] != NULL)
155  retVal.push_back(in[i++]);
156  }
157 
158  void
159  Vectorize(std::vector<std::string> &retVal,const char **in,int n)
160  {
161  // retVal.resize(0);
162  if(n <= 0) return;
163  int i = 0;
164  while((in[i] != NULL) && i < n)
165  retVal.push_back(in[i++]);
166  }
167  };
168  };
169 };
void Trim(std::string &instr, bool preserve_newline=false)
Creates space delimited tokens in place.
void TokenizeString(std::vector< std::string > &tokens, const std::string &source)
Tokenize string.
Basic utility header.
int OpenFile(std::ifstream &Inf, const std::string &filename)
File opener.
void Vectorize(std::vector< std::string > &retVal, const char **in)
std::string GetNextContent(std::istream &In)
Defines MPI-specific parallel global and program classes.
void const size_t const size_t const size_t const double const double * x
const std::string Trimmed(const std::string &instr, bool preserve_newline=false)
Returns space delimited tokens.
int String2Buf(const std::string &instr, void **buf)
const std::string stripdirs(const std::string &pname)
Strip absolute path.
void GetContentUntil(std::istream &In, std::string ret, const std::string &etag)